从MySQL到Redis,提升数据迁移的效率
做开发的同学都知道,一旦设计到底层存储优化,数据结构甚至数据库的变更,通常都会进行数据迁移的工作。如果系统运行时间过长,数据迁移的数量可能非常庞大。这时候,如何进行高效的数据迁移,实际也是上线质量的直接影响因素之一。 下面内容是转载的一个小
做开发的同学都知道,一旦设计到底层存储优化,数据结构甚至数据库的变更,通常都会进行数据迁移的工作。如果系统运行时间过长,数据迁移的数量可能非常庞大。这时候,如何进行高效的数据迁移,实际也是上线质量的直接影响因素之一。
下面内容是转载的一个小技巧(原文),无法适用于各种变化的场景,仅供大家参考。
场景是从MySQL中将数据导入到Redis的Hash结构中。当然,最直接的做法就是遍历MySQL数据,一条一条写入到Redis中。这样可能没什么错,但是速度会非常慢。而如果能够使MySQL的查询输出数据直接能够与Redis命令行的输入数据协议相吻合,可能就省事多了。
根据什么都测试,他800w的数据迁移,时间从90分钟缩短到2分钟。
废话说了一堆,下面是具体案例。
MySQL数据表结构:
CREATE TABLE events_all_time ( id int(11) unsigned NOT NULL AUTO_INCREMENT, action varchar(255) NOT NULL, count int(11) NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY uniq_action (action) );
Redis存储结构:
HSET events_all_time [action] [count]
下面是重点,能过下面SQL语句将MySQL输出直接变更成redis-cli可接收的格式:
-- events_to_redis.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, 'events_all_time' AS redis_key, action AS hkey, count AS hval FROM events_all_time ) AS t
然后用管道符重定向输出即可:
mysql stats_db --skip-column-names --raw <p style="margin-top:20px;margin-left:70px;line-height:24px;border:1px solid #ccc;text-align:center;width:545px;background:#fff"> </p><p style="font-size: 16px; font-family: Verdana;background:#d20; color:#fff;float:left;margin-left:-70px; border-radius: 10px 0 10px 0; padding: 3px 12px 4px;line-height:32px;margin-top:14px;">42区 VPS</p> <p class="42quad" style="padding: 23px; font-size:14px;font-family: Trebuchet MS;text-align;center;"> 42qu.com 云主机 , 卖给创业的你 。 点击这里 , 查看详情 </p> <p style="margin:0;padding:0;height:1px;overflow:hidden;"> <img src="/static/imghw/default1.png" data-src="http://www.68idc.cn/help/uploads/allimg/150130/06061115c-0.png" class="lazy" alt="无觅相关文章插件,快速提升流量" style="max-width:90%"></p> <img src="/static/imghw/default1.png" data-src="http://www1.feedsky.com/t1/723917543/nosqlfan/feedsky/s.gif?r=http://blog.nosqlfan.com/html/4144.html" class="lazy" border="0" style="max-width:90%" style="position:absolute" style="max-width:90%" alt="从MySQL到Redis,提升数据迁移的效率" > <br> <b>Warning</b>: call_user_func_array() expects parameter 1 to be a valid callback, function 'embed_rssfooter' not found or invalid function name in <b>/home/b55/htdocs/blog.nosqlfan.com/wp-includes/plugin.php</b> on line <b>166</b><br> <img src="/static/imghw/default1.png" data-src="http://www1.feedsky.com/t1/723917543/nosqlfan/feedsky/s.gif?r=http://blog.nosqlfan.com/html/4144.html" class="lazy" border="0" style="max-width:90%" style="position:absolute" style="max-width:90%" alt="从MySQL到Redis,提升数据迁移的效率" >

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...
