目的

把单表近5千万的某单个字段导入到Redis,作为一个list存储。

方案一: 使用sqlcmd工具(sqlserver自带),直接生成命令在Redis-cli中执行。

方案一. 使用sqlcmd把打印结果输出在文本中,然后用redis-cli逐行执行文本中的命令。

redis写入list的命令。

LPUSH openids xxxx
  1. 用sqlserver拼接处这个结果
SET NOCOUNT ON;
SELECT 'LPUSH openids ' +'"' + openID +'"'
FROM [dbo].[table1]

  1. 使用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

结论

  1. 能勉强成功。
  2. 导入速度慢,大概2w/s ~ 3w/s。远低于Redis的写入理想值。
  3. 容易出错,如果value中含有意外字符,会导致命令执行失败,结束任务。
  4. 别用这个方法。

官网支撑:

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万条呢?因为我分两次运行了)

结论:

  1. 不会受value的影响,中断运行。
  2. 运行速度快,差不多13w/s。接近理想值。
  3. 亲测,第一次在写入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的更多相关文章

  1. Mysql百万数据量级数据快速导入Redis

    前言 随着系统的运行,数据量变得越来越大,单纯的将数据存储在mysql中,已然不能满足查询要求了,此时我们引入Redis作为查询的缓存层,将业务中的热数据保存到Redis,扩展传统关系型数据库的服务能 ...

  2. 54.超大数据快速导入MySQL

    超大数据快速导入MySQL  ----千万级数据只需几十分钟本地测试方法1.首先需要修改本地mysql的编码和路径,找到my.ini.2.在里面添加或修改 character-set-server=u ...

  3. [DJANGO] excel十几万行数据快速导入数据库研究

    先贴原来的导入数据代码: 8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.setting ...

  4. excel十几万行数据快速导入数据库研究(转,下面那个方法看看还是可以的)

    先贴原来的导入数据代码: 8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.setting ...

  5. 将mysql表数据批量导入redis zset结构中

    工作中有这样一个需求,要将用户的魅力值数据做排行,生成榜单展示前40名,每隔5分钟刷新一次榜单.这样的需求用redis的zset是很方便实现的.但是数据存在mysql的表中,有400多万条,怎么将其快 ...

  6. Mysql --学习:大量数据快速导入导出

    声明:此文供学习使用,原文:https://blog.csdn.net/xiaobaismiley/article/details/41015783 [实验背景] 项目中需要对数据库中一张表进行重新设 ...

  7. HBase数据快速导入之ImportTsv&Bulkload

    导入数据最快的方式,可以略过WAL直接生产底层HFile文件 (环境:centos6.5.Hadoop2.6.0.HBase0.98.9) 1.SHELL方式 1.1 ImportTsv直接导入 命令 ...

  8. Mysql 千万数据快速导入

    最近碰到个项目,需要 千万条数据入库的问题,有原本的 类 csv 文件导入, 统计了下  数据行大概有 1400W 行之多 二话不说, 建表,直接 load LOAD DATA LOCAL INFIL ...

  9. 使用MySQL的SELECT INTO OUTFILE ,Load data file,Mysql 大量数据快速导入导出

    使用MySQL的SELECT INTO OUTFILE .Load data file LOAD DATA INFILE语句从一个文本文件中以很高的速度读入一个表中.当用户一前一后地使用SELECT ...

随机推荐

  1. Android 计算器制作 1.布局

    1.activity_main.xml文件布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr ...

  2. 8.2.ZooKeeper应用场景

    7.ZooKeeper应用举例 为了方便大家理解ZooKeeper,在此就给大家举个例子,看看ZooKeeper是如何实现的他的服务的,我以ZooKeeper提供的基本服务分布式锁为例. 7.1 分布 ...

  3. iOS 环形进度条

    .h文件 #import <UIKit/UIKit.h> @interface YTProgressView : UIView@property (nonatomic, copy) NSS ...

  4. NoSQL数据库技术实战-第1章 NoSQL与大数据简介 NoSQL产生的原因

    NoSQL产生的原因: 关系型数据库不擅长的操作,是NoSQL应运而生的原因: 大量的数据写入操作书上写的是“大量数据的写入操作“,我理解的应该是“大量的数据写入操作”,因为大量的数据写入操作才会引起 ...

  5. BZOJ1601 [Usaco2008 Oct]灌水[最小生成树]

    显然分析可知这个图最后连起来是一个森林,每棵树有一个根再算一个代价.那么这些跟需要连向某一点一个建立水库的代价,且根可以有多个但不能没有,则考虑用超级源点0向所有点连虚边,Prim跑MST即可保证有至 ...

  6. Python之windows锁屏

    简单粗暴,三行代码搞定 from ctypes import * user32 = windll.LoadLibrary('user32.dll') user32.LockWorkStation() ...

  7. AngularJs-变量

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. 题解 【POJ1934】 Trip

    题目意思: 有两个字符串(长度\(<=80\)),按字典序输出它们的最长公共子串的所有情况. 解析 最长公共子序列的长度应该都没问题了吧...有问题请自行百度 但关键是要求出每种情况,还要按字典 ...

  9. 9种纯CSS3人物信息卡片动态展示效果

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  10. 配置https证书

    官网: https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx ssl安装检测工具: https://www.myssl.cn/tools/ch ...