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