从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

AI Hentai Generator
Generate AI Hentai for free.

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 is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

To improve the performance of PostgreSQL database in Debian systems, it is necessary to comprehensively consider hardware, configuration, indexing, query and other aspects. The following strategies can effectively optimize database performance: 1. Hardware resource optimization memory expansion: Adequate memory is crucial to cache data and indexes. High-speed storage: Using SSD SSD drives can significantly improve I/O performance. Multi-core processor: Make full use of multi-core processors to implement parallel query processing. 2. Database parameter tuning shared_buffers: According to the system memory size setting, it is recommended to set it to 25%-40% of system memory. work_mem: Controls the memory of sorting and hashing operations, usually set to 64MB to 256M

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
