SQLSERVER大批量数据快速导入Redis
目的
把单表近5千万的某单个字段导入到Redis,作为一个list存储。

方案一: 使用sqlcmd工具(sqlserver自带),直接生成命令在Redis-cli中执行。
方案一. 使用sqlcmd把打印结果输出在文本中,然后用redis-cli逐行执行文本中的命令。
redis写入list的命令。
LPUSH openids xxxx
- 用sqlserver拼接处这个结果
SET NOCOUNT ON;
SELECT 'LPUSH openids ' +'"' + openID +'"'
FROM [dbo].[table1]

- 使用sqlcmd,推送到redis-cli执行
# -S:服务器地址,-U:账号名 -P:密码 -d:数据库 -h:列标题之间打印的行数
sqlcmd -S . -U sa -P xxxx -d DataImport -h -1 -i d:\openid.sql | redis-cli -h 192.168.xx.xx -a xxxx -p 6379
结论
- 能勉强成功。
- 导入速度慢,大概2w/s ~ 3w/s。远低于Redis的写入理想值。
- 容易出错,如果value中含有意外字符,会导致命令执行失败,结束任务。
- 别用这个方法。
官网支撑:
Redis is a TCP server using the client-server model and what is called a Request/Response protocol.
This means that usually a request is accomplished with the following steps:
The client sends a query to the server, and reads from the socket, usually in a blocking way, for the server response.
The server processes the command and sends the response back to the client.
方案二: 使用sqlcmd工具(sqlserver自带),直接生成redis协议在Redis-cli中执行。(可以直接看这个方案)
使用sqlserver直接生成协议命令
--*3:由三个参数组成:LPUSH,key,value。
--$5: 'LPUSH'的字符串长度。
--$7: 'openids'的字符串长度。
--$n: {value}的长度
--char(13): /r
--char(10): /n
SET NOCOUNT ON;
SELECT '*3'+char(13)+CHAR(10) + '$5'+char(13)+CHAR(10) + 'LPUSH'+char(13)+CHAR(10) + '$7'+char(13)+CHAR(10)
+ 'openids'+char(13)+CHAR(10) +'$'+ CAST(DATALENGTH(openID) as varchar(255)) +char(13)+CHAR(10)+openID+char(13)+CHAR(10)
FROM [dbo].[table1]
# --pipe 使用管道模式
sqlcmd -S . -U sa -P xxxx -d DataImport -h -1 -i d:\openid.sql | redis-cli -h 192.168.xx.xx -a xxxx -p 6379 --pipe
执行效果:

(为什么只有2660万条呢?因为我分两次运行了)

结论:
- 不会受value的影响,中断运行。
- 运行速度快,差不多13w/s。接近理想值。
- 亲测,第一次在写入2800万的时候,我的128G内存撑不住了,Redis直接崩了。所以,我选择rownumber() over()从中断的地方,继续写入。

官网支撑:
Using a normal Redis client to perform mass insertion is not a good idea for a few reasons: the naive approach of sending one command after the other is slow because you have to pay for the round trip time for every command. It is possible to use pipelining, but for mass insertion of many records you need to write new commands while you read replies at the same time to make sure you are inserting as fast as possible.
Only a small percentage of clients support non-blocking I/O, and not all the clients are able to parse the replies in an efficient way in order to maximize throughput. For all this reasons the preferred way to mass import data into Redis is to generate a text file containing the Redis protocol, in raw format, in order to call the commands needed to insert the required data.
附:
# sqlcmd导出到文本
sqlcmd -S . -U sa -P xxx -d DataImport -h -1 -i d:\openid.sql > d:\tt.txt
# 从文本推到redis-cli执行。
type protocol-commands.txt | redis-cli -h 192.168.xx.xx -a xxxx -p 6379 --pipe
把redis的正常命令转成协议格式的批处理脚本
亲测,近5千万数据的文本,是动都动不了。
while read CMD; do
# each command begins with *{number arguments in command}\r\nW
XS=($CMD); printf "*${#XS[@]}\r\n"WWW
# for each argument, we append ${length}\r\n{argument}\r\n
for X in $CMD; do printf "\$${#X}\r\n$X\r\n"; done
done < origin-commands.txt
协议样本:
*3
$5
LPUSH
$3
arr
$4
AAAA
*3
$5
LPUSH
$3
arr
$4
BBBB
SQLSERVER大批量数据快速导入Redis的更多相关文章
- Mysql百万数据量级数据快速导入Redis
前言 随着系统的运行,数据量变得越来越大,单纯的将数据存储在mysql中,已然不能满足查询要求了,此时我们引入Redis作为查询的缓存层,将业务中的热数据保存到Redis,扩展传统关系型数据库的服务能 ...
- 54.超大数据快速导入MySQL
超大数据快速导入MySQL ----千万级数据只需几十分钟本地测试方法1.首先需要修改本地mysql的编码和路径,找到my.ini.2.在里面添加或修改 character-set-server=u ...
- [DJANGO] excel十几万行数据快速导入数据库研究
先贴原来的导入数据代码: 8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.setting ...
- excel十几万行数据快速导入数据库研究(转,下面那个方法看看还是可以的)
先贴原来的导入数据代码: 8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.setting ...
- 将mysql表数据批量导入redis zset结构中
工作中有这样一个需求,要将用户的魅力值数据做排行,生成榜单展示前40名,每隔5分钟刷新一次榜单.这样的需求用redis的zset是很方便实现的.但是数据存在mysql的表中,有400多万条,怎么将其快 ...
- Mysql --学习:大量数据快速导入导出
声明:此文供学习使用,原文:https://blog.csdn.net/xiaobaismiley/article/details/41015783 [实验背景] 项目中需要对数据库中一张表进行重新设 ...
- HBase数据快速导入之ImportTsv&Bulkload
导入数据最快的方式,可以略过WAL直接生产底层HFile文件 (环境:centos6.5.Hadoop2.6.0.HBase0.98.9) 1.SHELL方式 1.1 ImportTsv直接导入 命令 ...
- Mysql 千万数据快速导入
最近碰到个项目,需要 千万条数据入库的问题,有原本的 类 csv 文件导入, 统计了下 数据行大概有 1400W 行之多 二话不说, 建表,直接 load LOAD DATA LOCAL INFIL ...
- 使用MySQL的SELECT INTO OUTFILE ,Load data file,Mysql 大量数据快速导入导出
使用MySQL的SELECT INTO OUTFILE .Load data file LOAD DATA INFILE语句从一个文本文件中以很高的速度读入一个表中.当用户一前一后地使用SELECT ...
随机推荐
- 开源Android 恶意软件Radio Balouch
安全研究机构 ESET 首次发现了开源 Android 间谍软件在 Google Play 上的恶意信息窃取行为,并且在被删除后仍在Google Play 重复出现.据悉,第一个间谍软件是基于开源间 ...
- JS批量绑定事件
,,,,] for(var j in a){ $("#" + j).click(function () { // 前提是先动态生成id是j的标签 var id_cm = $(thi ...
- javascript基本语法点
1. getElementById例:<input type="text" id="user" />则getElementById('user'). ...
- TF_RNNCell
参考:链接. RNNCell BasicRNNCell GRUCell BasicLSTMCell LSTMCell MultiRNNCell 抽象类RNNCell 所有的rnncell均继承于RNN ...
- Python学习笔记----数据类型 运算符 循环 条件判断
1. Python安装 在官网www.python.org下载安装程序,可以支持的操作系统linux,windows,mac. Python版本:2.x和3.x,分别有x86和x64. 在Window ...
- NoSQL数据库技术实战-第1章 NoSQL的数据一致性 传统关系型数据库的ACID
在看着章节的时候,我简单的回顾了一下关系型数据库的事务处理的ACID原则,其中原子性和持久性比较好理解.由于以前没有深入去研究.关于一致性和隔离性上我产生了疑问,在整理后分析如下: 一致性:书中所 ...
- Python3之Django框架搭建详细步骤
安装Django 自行下载的pip,可执行如下命令: pip install django 下载python3版本可以自带pip3 ,命令如下: pip3 install django 此命令会下载d ...
- kafka的maxPollIntervalMs设置太小引发的惨案 (转)
本地启动kafka后,不断报一下信息: 表示本地consumer节点在不断的重新加入group,并且不断伴随着offset commit失败. 具体原因是因为ConsumerCoordinator没有 ...
- RAID的多种实现
RAID的诞生 由加利福尼亚大学伯克利分校(University of California-Berkeley)在1988年,发表的文章:"A Case for Redundant Arra ...
- Laravel 引入第三方类库及自定义函数
1.新建一个目录放第三方类库 2.找到composer.json文件打开,在里面autoload 下classmap下面加入类库路径 3根目录下运行composer dumpautoload 4.使用 ...