按数据库分段Redis [英] Segmenting Redis By Database

查看:187
本文介绍了按数据库分段Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Redis配置有16个数据库,编号为0-15.这仅仅是名称间隔的一种形式,还是按数据库隔离会对性能产生影响?

By default, Redis is configured with 16 databases, numbered 0-15. Is this simply a form of name spacing, or are there performance implications of segregating by database ?

例如,如果我使用默认数据库(0),并且有1000万个密钥,则最佳实践建议使用 keys 命令按通配符模式查找密钥将效率低下.但是,如果我存储我的主键,也许是8个段键的前4个段,导致在单独的数据库(例如数据库3)中存储的键子集要小得多,该怎么办? Redis是将它们视为较小的一组密钥,还是将所有数据库中的所有密钥都显示为一个巨大的密钥索引?

For example, if I use the default database (0), and I have 10 million keys, best practices suggest that using the keys command to find keys by wildcard patterns will be inefficient. But what if I store my major keys, perhaps the first 4 segments of 8 segment keys, resulting in a much smaller subset of keys in a separate database (say database 3). Will Redis see these as a smaller set of keys, or do all keys across all databases appear as one giant index of keys ?

就时间复杂度而言,如果我的数据库看起来像这样,则更明确地说:

More explicitly put, in terms of time complexity, if my databases look like this:

  • 数据库0:10,000,000个键
  • 数据库3:10,000个键

针对数据库3的调用的时间复杂度是 O(10m) 还是 O (10k) ?

will the time complexity of keys calls against Database 3 be O(10m) or will it be O(10k) ?

感谢您的时间.

推荐答案

Redis为每个数据库都有一个单独的字典.在您的示例中,针对数据库3的键调用将为O(10K)

Redis has a separate dictionary for each database. From your example, the keys call against database 3 will be O(10K)

也就是说,使用keys违反最佳实践.此外,对同一应用程序使用多个数据库也违反最佳实践.如果要遍历键,则应以特定于应用程序的方式对其进行索引. SortedSet是建立索引的好方法.

That said, using keys is against best practice. Additionally, using multiple databases for the same application is against best practices as well. If you want to iterate over keys, you should index them in an application specific way. A SortedSet is a good way way to build an index.

参考文献:

  1. 结构redisServer具有一个redisDB数组. 请参阅redis.h中的redisServer.
  2. 每个redisDB都有其自己的字典对象. 请参阅redis.h中的redisDB.
  3. keys命令在当前数据库的字典上运行
  1. The structure redisServer has an array of redisDB. See redisServer in redis.h
  2. Each redisDB has its own dictionary object. See redisDB in redis.h
  3. keys command operates on the dictionary for the current database

这篇关于按数据库分段Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆