A simple database migration method from MySQL to Redis _redis

Source: Internet
Author: User
Tags bulk insert mysql query redis mysql command line

Moving a large table from MySQL to Redis, you'll find it hard to tolerate the slow pace of fetching, converting, or loading a row of data. Here I'm going to tell you a little trick that frees you up. Use pipe output to pass the contents of the MySQL command line directly to Redis-cli to bypass the "middleware" approach so that the two can achieve the best possible speed in data manipulation.

A MySQL table of about 8 million rows of data, originally imported into the Redis takes 90 minutes, after using this method, only need two minutes. Whether you believe it or not, I believe it anyway.

MySQL to Redis data protocol

The Redis-cli command-line tool has a bulk insert pattern designed specifically for batch execution commands. This first step is to format the contents of the MySQL query into REDIS-CLI available data formats. Here we go!


My statistics:

CREATE TABLE events_all_time (
 ID int (one) unsigned not NULL auto_increment,
 action varchar (255) NOT NULL,
 cou NT Int (one) not NULL DEFAULT 0,
 PRIMARY key (ID),
 UNIQUE key uniq_action (action)
;

The Redis command to be executed in each row of data is as follows:

Hset Events_all_time [Action] [count]
Follow the Redis command rules above to create a Events_to_redis.sql file that contains the SQL used to generate the Redis Data protocol format:

--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

OK, execute with the following command:

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

Very important MySQL parameter description:

    • --raw: Make MySQL do not convert newline characters in field values.
    • --skip-column-names: Do not include column names in each row of the MySQL output.
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.