加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 数据库 > MySql > 正文

MySQL到Redis – 导入和模型

发布时间:2020-05-22 22:13:00 所属栏目:MySql 来源:互联网
导读:我正在考虑使用Redis来缓存一些用户数据快照,以加快对该数据的访问(其中一个原因是因为我的MySQL表遭受锁争用)而我正在寻找一步导入这样一个表的最佳方法(可能包含几条记录到数百万条记录):mysql select * from mytable where snapshot = 1133; +------+----

我正在考虑使用Redis来缓存一些用户数据快照,以加快对该数据的访问(其中一个原因是因为我的MySQL表遭受锁争用)而我正在寻找一步导入这样一个表的最佳方法(可能包含几条记录到数百万条记录):

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

进入Redis键值存储区.

我可以有很多“快照”加载到Redis中,基本的访问模式是(SQL语法)

> select * from mytable where snapshot =?和id =?

这些快照也可以来自其他表,因此“每个快照的全局唯一ID”是列快照,例如:

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

加载到redis的快照永远不会改变,它们只能通过TTL一周使用

>有一种方法可以将这种数据(行和列)一步加载到Redis中,结合redis-cli –pipe和HMSET?
>为了存储/获取这些数据,在redis中使用的最佳模型是什么(考虑访问模式)?

我找到了redis-cli –pipe Redis Mass Insertion(以及MySQL to Redis in One Step),但是我无法找到实现我的要求的最佳方法(使用HMSET从一步中加载所有行/列,最佳redis模型)

提前致谢

克里斯蒂安.

最佳答案 模型

能够以与以下相同的方式从Redis查询数据:

select * from mytable where snapshot = ?
select * from mytable where id = ?

你需要下面的模型.

注意:从mytable中选择*,其中snapshot =?和id =?这里没有多大意义,因为它与select * from mytable相同,其中id = ?.

密钥类型和命名

[Key Type] [Key name pattern]
HASH       d:{id}
ZSET       d:ByInsertionDate
SET        d:BySnapshot:{id}

注意:我使用d:作为命名空间,但您可能希望使用域模型的名称重命名它.

数据插入

从Mysql插入一个新行到Redis:

hmset d:2989 id 2989 email example-2989@example.com name fake-name-2989 ... snapshot 1134
zadd d:ByInsertionDate {current_timestamp} d:2989
sadd d:BySnapshot:1134 d:2989

另一个例子:

hmset d:2990 id 2990 email example-2990@example.com name fake-name-2990 ... snapshot 1134
zadd d:ByInsertionDate {current_timestamp} d:2990
sadd d:BySnapshot:1134 d:2990

克龙

以下是必须每天或每周运行的算法,具体取决于您的要求:

for key_name in redis(ZREVRANGEBYSCORE d:ByInsertionDate -inf {timestamp_one_week_ago})

 // retrieve the snapshot id from d:{id}
 val snapshot_id = redis(hget {key_name} snapshot)

 // remove the hash (d:{id})
 redis(del key_name)

 // remove the hash entry from the set
 redis(srem d:BySnapshot:{snapshot_id} {key_name})

// clean the zset from expired keys
redis(zremrangebyscore d:ByInsertionDate -inf {timestamp_one_week_ago})

用法

select * from my_other_table,其中snapshot = 1134;将是:

{snapshot_id} = 1134
for key_name in redis(smembers d:BySnapshot:{snapshot_id})
  print(redis(hgetall {keyname}))

或写一个lua脚本直接在redis端执行此操作.最后:

select * from my_other_table,其中id = 2989;将会:

{id} = 2989
print(redis(hgetall d:{id}))

进口

这部分很简单,只需阅读表格并按照上面的模型.根据您的要求,您可能希望以每小时/每日/每周cron导入所有(或部分)数据.

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读