title: MySQL to Redis - Import and Model
body: <p>I'm thinking to use <a href="http://redis.io/" rel="noreferrer">Redis</a> to cache some user data snapshot(s) in order to speed up the access to that data (one of the reasons is because my MySQL table(s) suffer of lock contention) and I'm looking for the best way to import in one step a table like this(which may contain from a few record to millions of records):</p>
  
  <pre><code>mysql&gt; select * from mytable where snapshot = 1133;
  +------+--------------------------+----------------+-------------------+-----------+-----------+
  | id   | email                    | name           | surname           | operation | snapshot  |
  +------+--------------------------+----------------+-------------------+-----------+-----------+
  | 2989 | example-2989@example.com | fake-name-2989 | fake-surname-2989 |         2 |      1133 |
  | 2990 | example-2990@example.com | fake-name-2990 | fake-surname-2990 |        10 |      1133 |
  | 2992 | example-2992@example.com | fake-name-2992 | fake-surname-2992 |         5 |      1133 |
  | 2993 | example-2993@example.com | fake-name-2993 | fake-surname-2993 |         5 |      1133 |
  | 2994 | example-2994@example.com | fake-name-2994 | fake-surname-2994 |         9 |      1133 |
  | 2995 | example-2995@example.com | fake-name-2995 | fake-surname-2995 |         7 |      1133 |
  | 2996 | example-2996@example.com | fake-name-2996 | fake-surname-2996 |         1 |      1133 |
  +------+--------------------------+----------------+-------------------+-----------+-----------+
  </code></pre>
  
  <p>into the Redis key-value store.</p>
  
  <p>I can have many "snapshots" to load into Redis, and <strong>the basic access pattern</strong> is (SQL like syntax) </p>
  
  <ul>
  <li><code>select * from mytable where snapshot = ? and id = ?</code></li>
  </ul>
  
  <p><strong>these snapshots can also coming from others table</strong>, so the "<strong>global unique ID per snapshot</strong>" is the column <code>snapshot</code>, ex:</p>
  
  <pre><code>mysql&gt; select * from my_other_table where snapshot = 1134;
  +------+--------------------------+----------------+-------------------+-----------+-----------+
  | id   | email                    | name           | surname           | operation | snapshot  |
  +------+--------------------------+----------------+-------------------+-----------+-----------+
  | 2989 | example-2989@example.com | fake-name-2989 | fake-surname-2989 |         1 |      1134 |
  | 2990 | example-2990@example.com | fake-name-2990 | fake-surname-2990 |         8 |      1134 |
  | 2552 | example-2552@example.com | fake-name-2552 | fake-surname-2552 |         5 |      1134 |
  +------+--------------------------+----------------+-------------------+-----------+-----------+
  </code></pre>
  
  <p><em>The loaded snapshot into redis never change, they are available only for a week via TTL</em></p>
  
  <ul>
  <li><p>There is a way to load in one step this kind of data(rows and columns) into redis combining <code>redis-cli --pipe</code> and <code>HMSET</code>? </p></li>
  <li><p>What is the best model to use in redis in order to store/get this data (thinking at the access pattern)?</p></li>
  </ul>
  
  <p>I have found the <code>redis-cli --pipe</code> <a href="http://redis.io/topics/mass-insert" rel="noreferrer">Redis Mass Insertion</a> (and also <a href="http://dcw.ca/blog/2013/01/02/mysql-to-redis-in-one-step/" rel="noreferrer">MySQL to Redis in One Step</a>) but I can't figure out the best way to achieve my requirements (load from mysql in one step all rows/colums, best redis model for this) using <a href="http://redis.io/commands/hmset" rel="noreferrer">HMSET</a></p>
  
  <p>Thanks in advance</p>
  
  <p>Cristian.</p>