Home > Database > Mysql Tutorial > 一步完成MySQL向Redis迁移_MySQL

一步完成MySQL向Redis迁移_MySQL

WBOY
Release: 2016-06-01 13:37:00
Original
952 people have browsed it

Redis

在把一个大表从 MySQL 迁移到 Redis 时,你可能会发现,每次提取、转换、导入一条数据是让人难以忍受的慢!这里有一个技巧,你可以通过使用管道把 MySQL 的输出直接输入到 redis-cli输入端,这可以使两个数据库都能以他们的最顶级速度来运行。

使用了这个技术,我把 800 万条 MySQL 数据导入到 Redis 的时间从 90 分钟缩短到了两分钟。

Mysql到Redis的数据协议

redis-cli命令行工具有一个批量插入模式,是专门为批量执行命令设计的。这第一步就是把Mysql查询的内容格式化成redis-cli可用的数据格式。here we go!

我的统计表:

<ol class="dp-sql">
<li class="alt"><span><span class="keyword">CREATE</span><span> </span><span class="keyword">TABLE</span><span> events_all_time (  </span></span></li>
<li>
<span>  id </span><span class="keyword">int</span><span>(11) unsigned </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span> AUTO_INCREMENT,  </span>
</li>
<li class="alt">
<span>  </span><span class="keyword">action</span><span> </span><span class="keyword">varchar</span><span>(255) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span>,  </span>
</li>
<li>
<span>  </span><span class="func">count</span><span> </span><span class="keyword">int</span><span>(11) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span> </span><span class="keyword">DEFAULT</span><span> 0,  </span>
</li>
<li class="alt">
<span>  </span><span class="keyword">PRIMARY</span><span> </span><span class="keyword">KEY</span><span> (id),  </span>
</li>
<li>
<span>  </span><span class="keyword">UNIQUE</span><span> </span><span class="keyword">KEY</span><span> uniq_action (</span><span class="keyword">action</span><span>)  </span>
</li>
<li class="alt"><span>); </span></li>
</ol>
Copy after login

准备在每行数据中执行的redis命令如下:

<ol class="dp-sql"><li class="alt"><span><span>HSET events_all_time [</span><span class="keyword">action</span><span>] [</span><span class="func">count</span><span>] </span></span></li></ol>
Copy after login

按照以上redis命令规则,创建一个events_to_redis.sql文件,内容是用来生成redis数据协议格式的SQL:

<ol class="dp-sql">
<li class="alt"><span><span class="comment">-- events_to_redis.sql </span><span> </span></span></li>
<li><span> </span></li>
<li class="alt">
<span class="keyword">SELECT</span><span> CONCAT(  </span>
</li>
<li>
<span>  </span><span class="string">"*4\r\n"</span><span>,  </span>
</li>
<li class="alt">
<span>  </span><span class="string">'</span>
</li>
</ol>
Copy after login

ok, 用下面的命令执行:

<ol class="dp-sql"><li class="alt"><span><span>mysql stats_db </span><span class="comment">--skip-column-names --raw <span> </span></span></span></li></ol>
Copy after login

很重要的mysql参数说明:

--raw: 使mysql不转换字段值中的换行符。

--skip-column-names: 使mysql输出的每行中不包含列名。

原文出处:http://www.oschina.net/translate/mysql-to-redis-in-one-step

英文原文:MySQL to Redis in One Step

, LENGTH(redis_cmd), '\r\n',
  • redis_cmd, '\r\n',
  • '

    ok, 用下面的命令执行:

    ___FCKpd___3
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    很重要的mysql参数说明:

    --raw: 使mysql不转换字段值中的换行符。

    --skip-column-names: 使mysql输出的每行中不包含列名。

    原文出处:http://www.oschina.net/translate/mysql-to-redis-in-one-step

    英文原文:MySQL to Redis in One Step

    , LENGTH(redis_key), '\r\n',
  • redis_key, '\r\n',
  • '

    ok, 用下面的命令执行:

    ___FCKpd___3
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    很重要的mysql参数说明:

    --raw: 使mysql不转换字段值中的换行符。

    --skip-column-names: 使mysql输出的每行中不包含列名。

    原文出处:http://www.oschina.net/translate/mysql-to-redis-in-one-step

    英文原文:MySQL to Redis in One Step

    , LENGTH(hkey), '\r\n',
  • hkey, '\r\n',
  • '

    ok, 用下面的命令执行:

    ___FCKpd___3
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    很重要的mysql参数说明:

    --raw: 使mysql不转换字段值中的换行符。

    --skip-column-names: 使mysql输出的每行中不包含列名。

    原文出处:http://www.oschina.net/translate/mysql-to-redis-in-one-step

    英文原文:MySQL to Redis in One Step

    , LENGTH(hval), '\r\n',
  • hval, '\r'
  • )
  • FROM (
  • SELECT
  • 'HSET' as redis_cmd,
  • 'events_all_time' AS redis_key,
  • action AS hkey,
  • count AS hval
  • FROM events_all_time
  • ) AS t
  • ok, 用下面的命令执行:

    ___FCKpd___3
    Copy after login
    Copy after login
    Copy after login
    Copy after login

    很重要的mysql参数说明:

    --raw: 使mysql不转换字段值中的换行符。

    --skip-column-names: 使mysql输出的每行中不包含列名。

    原文出处:http://www.oschina.net/translate/mysql-to-redis-in-one-step

    英文原文:MySQL to Redis in One Step

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template