When migrating a large table from MySQL to Redis, you may find that every time you extract, convert, and import a single piece of data, it's unbearable to be slow! Here's a tip that you can use the pipeline to input MySQL output directly to the REDIS-CLI input, which allows two databases to run at their top speed.
Using this technique, I've reduced the time it takes to import 8 million MySQL data into Redis from 90 minutes to two minutes.
MySQL-to-redis data protocol
The REDIS-CLI command-line tool has a bulk insert mode that is specifically designed for batch execution commands. The first step is to format the contents of the MySQL query into REDIS-CLI available data formats. Here we go!
My Table of statistics:
- CREATE TABLE events_all_time (
- ID int (one) unsigned not NULL auto_increment,
- Action varchar (255) is not NULL,
- Count Int (one) not NULL DEFAULT 0,
- PRIMARY KEY (id),
- UNIQUE KEY uniq_action (action)
- );
The Redis commands that are prepared to execute in each row of data are as follows:
- hset Events_all_time [action] [count]
Follow the Redis command rules above to create a events_to_redis.sql file that is used to generate the SQL for 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
Important MySQL Parameter description:
--raw: Make MySQL do not convert the line break in field values.
--skip-column-names: Does not include the column name in each line of MySQL output.
Redis Data Bulk Import Export
one way to do this is to export and import Redis data from one server to a new server for work, but he needs to install the Ruby environment, and there may be unexpected errors during the installation of the REDIS-DUMP environment. So we have to choose other methods. A moment is a few ideas for reference.
1, data export, do not write their own, and do not use third-party scripts,
echo "Hgetall xxx" | redis-cli-h localhost-p 6379
echo "Hgetall xxx" | redis-cli-h localhost-p 6379 >> wordlist.raw
2. So get the result that you can go up to the server to view the Wordlist.raw file
3. Organize the file format into the input required
$f = ' xx.oo ';
$lines = 0;
$inkey = 0;
$rawfile = ' xx.oo ';
$hashkey = ' xx ';
$buff = ";
$fp = fopen ($rawfile, ' W ');
$fps = fopen ($f, ' R ');
While ($line = fgets ($fps)) {
$inkey =! $inkey;
if ($inkey) {
$f = ' Bayes_wordlist.raw ';
$lines = 0;
$inkey = 0;
$rawfile = ' Bayes_wordlist.3.raw ';
$hashkey = ' bayes_wordlist ';
$buff = ";
$fp = fopen ($rawfile, ' W ');
$fps = fopen ($f, ' R ');
While ($line = fgets ($fps)) {
$inkey =! $inkey;
if ($inkey) {
$line = sprintf (' "%s" ', Trim ($line));
$buff = "Hset $hashkey". Trim ($line);
}
Else
{
$buff. = ' "'. Trim ($line). '";
fwrite ($fp, $buff. " \ r \ n ");
}
}
$buff = "Hset $hashkey". Trim ($line);
}
Else
{
$buff. = ' "'. Trim ($line). '";
fwrite ($fp, $buff. " \ r \ n ");
}
}
If you choose which library to write to select X in the first row
4. Using REDIS-CLI for import
echo ' Date ' > Pipe.log && cat xx.oo | redis-cli >> pipe.log && echo ' Date ' >> pipe.log< /c5>
5. Add a time record, and record the import results, the actual execution of the imported statement is
Cat Wordlist.raw | redis-cli, of course, strict redis-cli also add-h localhost-p 6379 parameters.