将 MySQL 数据高效导入 Redis

embedded/2025/3/6 3:41:01/

目录

1. RESP 协议

(1)RESP 协议的优点

(2)RESP 支持的 5 种数据类型

(3)RESP 的用途

(4)RESP 协议示例

redis-cli%20%E7%9A%84%20pipe%20%E6%A8%A1%E5%BC%8F-toc" name="tableOfContents" style="margin-left:40px">2. redis-cli 的 pipe 模式

(1)pipe 模式的作用

(2)pipe 模式的使用场景

(3)pipe 模式的使用方法

(4)pipe 模式的输出

(5)pipe 模式的优点

3. 生成测试数据

4. 使用 SQL 构造导入文件

5. 编写脚本导入 Redis

参考:


1. RESP 协议

        RESP(REdis Serialization Protocol)是 Redis 的通信协议,用于客户端和服务器之间的数据传输。它简单、高效,支持多种数据类型。

(1)RESP 协议的优点

  • 高效:RESP 是二进制安全的,支持高效的数据传输。
  • 简单:格式简单,易于实现和解析。
  • 灵活:支持多种数据类型,适用于各种场景。

(2)RESP 支持的 5 种数据类型

  • 简单字符串

格式:+<字符串>\r\n
示例:+OK\r\n(表示字符串 OK)

  • 错误

格式:-<错误信息>\r\n
示例:-ERR unknown command\r\n(表示错误信息)

  • 整数

格式::<整数>\r\n
示例::1000\r\n(表示整数 1000)

  • 批量字符串

格式:$<长度>\r\n<数据>\r\n
示例:$6\r\nfoobar\r\n(表示字符串 foobar)

  • 数组

格式:*<元素数量>\r\n<元素1><元素2>...<元素N>
示例:*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n(表示数组 ["foo", "bar"])

(3)RESP 的用途

  • Redis 客户端与服务器通信:客户端发送命令,服务器返回响应。
  • 数据导入导出:用于 Redis 的数据备份和恢复。
  • 主从复制:主节点通过 RESP 协议将数据同步到从节点。

(4)RESP 协议示例

        客户端发送一个 SET 命令:

*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n

        解析:

  • *3:表示一个包含 3 个元素的数组。
  • $3\r\nSET\r\n:第一个元素是字符串 SET,长度为 3。
  • $5\r\nmykey\r\n:第二个元素是字符串 mykey,长度为 5。
  • $7\r\nmyvalue\r\n:第三个元素是字符串 myvalue,长度为 7。

        服务器返回一个成功响应:

+OK\r\n

        服务器返回一个错误响应:

-ERR unknown command\r\n

redis-cli%20%E7%9A%84%20pipe%20%E6%A8%A1%E5%BC%8F" name="2.%20redis-cli%20%E7%9A%84%20pipe%20%E6%A8%A1%E5%BC%8F">2. redis-cli 的 pipe 模式

        redis-cli 的 --pipe 模式是一种高效批量处理数据的功能,特别适合处理大规模数据。

(1)pipe 模式的作用

  • 高效批量处理:一次性发送大量命令或数据到 Redis 服务器,减少网络往返时间(RTT,Round-Trip Time),提升性能。
  • 减少开销:Redis 服务器会批量处理接收到的命令,减少每条命令的解析和响应开销。

(2)pipe 模式的使用场景

  • 大规模数据导入:适合导入数百万甚至数千万条数据。
  • 数据迁移:将数据从一个 Redis 实例迁移到另一个实例。
  • 批量命令执行:批量执行大量 Redis 命令(如 SET、INCR 等)。

(3)pipe 模式的使用方法

  • 命令格式:将 Redis 命令写入文件,每行一个命令。如示例文件 f1:
    SET key1 value1
    SET key2 value2
    INCR counter

  • RESP 格式:将 RESP 协议格式的数据写入文件。如示例文件 f2:
    *3\r\n$3\r\nSET\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n
    *3\r\n$3\r\nSET\r\n$4\r\nkey2\r\n$6\r\nvalue2\r\n

  • 通过 pipe 导入数据:使用 cat 命令将文件内容通过管道传递给 redis-cli --pipe,例如:
    cat f1 | redis-cli --pipe
    cat f2 | redis-cli --pipe

(4)pipe 模式的输出

        导入完成后,redis-cli 会显示统计信息,例如:

All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1000

        errors 是错误数量,replies 是成功执行的命令数量。

(5)pipe 模式的优点

  • 高性能:批量发送数据,减少网络和解析开销。
  • 简单易用:只需将数据写入文件并通过管道传递即可。
  • 支持多种格式:支持命令格式和 RESP 格式。

3. 生成测试数据

use test;
create table t_book
(id int, name varchar(50), descrition varchar(50));drop procedure if exists sp_insert;
delimiter //
create procedure sp_insert(cnt int)
begindeclare s int default 1;set session autocommit=0;insert into t_book select s, concat('book',s), concat('book_description',s);while s<=cnt doinsert into t_book select n, concat('book',n), concat('book_description',n) from (select id+s n from t_book where id+s <=cnt) t;set s=s*2;end while;commit;
end;
//
delimiter ;call sp_insert(1000000);

4. 使用 SQL 构造导入文件

        将下面这个构造 RESP 内容的查询语句保存到文件 RESP.sql

select concat("*4\r\n","$",length(redis_cmd),"\r\n",redis_cmd,"\r\n","$",length(redis_key),"\r\n",redis_key,"\r\n","$",length(hkey),"\r\n",hkey,"\r\n","$",length(hval),"\r\n",hval,"\r")from (select "hset" as redis_cmd, id as redis_key, name as hkey, descrition as hvalfrom t_book) as t;

5. 编写脚本导入 Redis

        一百万数据,命令格式文件的数据导入用时 4 秒,RESP 格式文件的数据导入用时 3 秒:

# date;mysql -uroot -p123456 -h127.0.0.1 -P3306 -Dtest --default-character-set=utf8mb4 --skip-column-names --raw -e "select concat('hset ',id,' ',name,' ',descrition) from t_book" | \
> redis-cli -p 6379 -a 123456 --pipe;date;
Tue Mar  4 16:33:51 CST 2025
mysql: [Warning] Using a password on the command line interface can be insecure.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1000000
Tue Mar  4 16:33:55 CST 2025
#
# redis-cli -p 6379 -a 123456 flushdb
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
#
# date;mysql -uroot -p123456 -h127.0.0.1 -P3306 -Dtest --default-character-set=utf8mb4 --skip-column-names --raw < RESP.sql | \
> redis-cli -p 6379 -a 123456 --pipe;date;
Tue Mar  4 16:34:19 CST 2025
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
mysql: [Warning] Using a password on the command line interface can be insecure.
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1000000
Tue Mar  4 16:34:22 CST 2025
#

参考:

MySQL百万级数据高效导入Redis


http://www.ppmy.cn/embedded/170378.html

相关文章

DeepSeek R1 训练策略4个阶段解析

DeepSeek R1 训练策略解析 DeepSeek R1 训练策略解析1. 冷启动监督微调&#xff08;Cold Start SFT&#xff09;**该阶段的主要目标**&#xff1a; 2. 面向推理的强化学习&#xff08;RL for Reasoning&#xff09;**该阶段的主要目标**&#xff1a; 3. 拒绝采样和监督微调&…

第二十七天:Scrapy 框架-模拟登录

文章目录 一、Scrapy-Request和Response&#xff08;请求和响应&#xff09; 二、Request对象 三、请求子类 FormRequest对象 1. 请求使用示例 四、响应对象 五、模拟登录 一、Scrapy-Request和Response&#xff08;请求和响应&#xff09; Scrapy的Request和Response对…

DeepSeek 开源周(2025/0224-0228)进度全分析:技术亮点、调用与编程及潜在影响

DeepSeek 技术开源周期间所有开放下载资源的目录及简要说明: 1. FlashMLA 描述:针对 Hopper GPU 优化的高效 MLA 解码内核,专为处理可变长度序列设计,显著提升大语言模型(LLM)的解码效率。性能:内存受限配置下可达 3000 GB/s 带宽,计算受限配置下可达 580 TFLOPS 算力(…

物联网桥梁监测设备集成GPS和红外

桥梁监测设备是确保桥梁结构安全、延长使用寿命的重要工具。集成 GPS 和 红外 技术的桥梁监测设备&#xff0c;可以提供更全面的监测功能&#xff0c;包括结构变形、温度变化、环境因素等。以下是集成GPS和红外的桥梁监测设备的顶级功能&#xff1a; 一、核心功能 集成GPS和红…

FFmpeg 命令详解(完整详细版)

FFmpeg 是一个强大的多媒体处理工具&#xff0c;支持音视频转换、剪辑、合并、滤镜、压缩等操作。本文整理了 常见 FFmpeg 命令&#xff0c;并配有 详细说明&#xff0c;帮助你高效使用 FFmpeg。 1. FFmpeg 基础命令 1.1 查看 FFmpeg 版本 ffmpeg -version查看 FFmpeg 版本、…

23种设计模式之《备忘录模式(Memento)》在c#中的应用及理解

程序设计中的主要设计模式通常分为三大类&#xff0c;共23种&#xff1a; 1. 创建型模式&#xff08;Creational Patterns&#xff09; 单例模式&#xff08;Singleton&#xff09;&#xff1a;确保一个类只有一个实例&#xff0c;并提供全局访问点。 工厂方法模式&#xff0…

国科大——数据挖掘(0812课程)——考试真题

前沿&#xff1a; 此文章记录了国科大数据挖掘&#xff08;0812&#xff09;课程的考试真题。 注&#xff1a; 考试可以携带计算器&#xff0c;毕竟某些题需要计算log&#xff0c;比如&#xff1a;决策树等。 2016年 1. Suppose a hospital tested the age and body fat for …

ECharts中yAxisIndex的作用

yAxisIndex 通常在数据可视化库&#xff08;如 ECharts 等&#xff09;中使用&#xff0c;用于指定系列数据对应的 Y 轴索引。下面为你详细介绍其作用和使用场景&#xff1a; 作用 在一个图表中&#xff0c;可能会有多个 Y 轴&#xff08;比如双 Y 轴图表&#xff09;&#x…