Improve the efficiency of data migration from MySQL to Redis

Source: Internet
Author: User

The scenario is to import data from MySQL into the hash structure of Redis. Of course, the most straightforward way to do this is to iterate through MySQL data and write a single piece to Redis. There may be nothing wrong with this, but the speed is very slow. It may be much easier to make the MySQL query output data directly match the input data protocol of the Redis command line.
The time is reduced from 90 minutes to 2 minutes based on the data migration of the test 800w.
The specific cases are as follows:
MySQL data table structure:

CREATE TABLE Events_all_time (
ID int (one) unsigned not NULL auto_increment,
Action varchar (255) is not NULL,
Count Int (one) is not NULL DEFAULT 0,
PRIMARY KEY (ID),
UNIQUE KEY uniq_action (action)
);

REDIS Storage structure:

Hset Events_all_time [Action] [count]

Here's the point, you can change the MySQL output directly to the REDIS-CLI acceptable format by following the SQL statement:

–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

You can then redirect the output with the pipe symbol:

MySQL Stats_db–skip-column-names–raw < Events_to_redis.sql | REDIS-CLI--pipe

Improve the efficiency of data migration from MySQL to Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.