Redis GET 与 SQL SELECT [英] Redis GET vs. SQL SELECT

查看:53
本文介绍了Redis GET 与 SQL SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 NoSQL 很陌生,但我一直很喜欢它的想法.我查看了 Redis,并得到了一些关于存储和接收多个 哈希的最佳方法的问题.

I am pretty new to NoSQL, but I always liked the idea of it. I took a look at Redis, and got a few questions about the best ways of storing and recieving multiple hashes.

假设以下场景:

Store a list of objects (redis 'Hashes') and select them by their timestamp.

要在 SQL 中存档,需要一张表和两个简单​​的查询(INSERT & SELECT).

To archive this in SQL, it would require one table and two simple queries (INSERT & SELECT).

尝试在 Redis 中执行此操作,我最终创建了以下结构:

Trying to do this in Redis, I ended up creating the following structure:

  1. Key object:$id (hash) 包含 object
  2. Key index:timestamp:$id (排序集)score 等于 timestampvalue 包括 id
  1. Key object:$id (hash) containing the object
  2. Key index:timestamp:$id (sorted set) score equals timestamp and value includes id

虽然我可以忍受两个键而不是一个表的额外维护工作 (SQL),但我对选择多个对象的过程感到好奇:

While I can live with the additional maintenance work of two keys instead of one table (SQL), I am curious about the process of selecting multiple objects:

ZRANGEBYSCORE index:timestamp:$id timestampStart timestampEnd

这将返回在 timestampStarttimestampEnd 之间创建的所有 ID 的 array.为了获取对象本身,我通过以下方式请求每个对象:

This returns an array of all IDs which got created between timestampStart and timestampEnd. To get the object itself I am requesting every single one by:

GET object:$id 

  • 这是正确的做法吗?
  • 与 SQL 数据库相比:它是否仍然明显更快,或者由于 GET 的数量过多,它甚至可能变得更慢?
    • Is this the right way of doing it?
    • In comparison with an SQL Database: Is it still appreciably faster or might it even become slower caused by the high number of GETs?
    • 推荐答案

      A ZRANGEBYSCORE 成本 O(log(N) + M) where N=|您的集合中的项目|M=|您选择的项目|.因此,执行 ZRANGEBYSCORE 然后 M GET 操作只是 O(long(N)+M+M) = O(log(N)+M) 并且最多会慢两倍.网络来回可能会严重减慢,但由于您的每个 get 都是独立的操作,因此您可以将它们流水线化.您也可以将整个内容放在 Lua 脚本中,然后来回进行一次,这将是最佳的.我可以 99% 肯定地说,这比在 SQL 中做同样的事情要快.

      A ZRANGEBYSCORE costs O(log(N) + M) where N=|items in your set| and M=|items you're selecting|. So, doing the ZRANGEBYSCORE and then M GET operations is just O(long(N)+M+M) = O(log(N)+M) and would at most be twice as slow. The network back and forth could have been a major slow down, but since each of your gets is an independent operation, you can just pipeline them. You can also put the whole thing in a Lua script and just have one back and forth, which would be the most optimal. I'd say with 99% certainty this would be faster than doing the same thing in SQL.

      此外,如果这对您来说是一个非常频繁的操作,您可以通过将整个对象存储在排序集中而不是仅存储 id 来提高速度.您将 key = object 编码为 jsonscore = timestamp.就不需要执行任何 GET 操作而言,这将为您的操作节省 <​​code>O(M).

      Also, if this is a very frequent operation for you, you can get even more speed up by just storing the entire object in your sorted set instead of just the id. You'd have key = object encoded as json, score = timestamp. This would save you O(M) on your operation in terms of not needing to do any GETs.

      这是否是一种好的做事方式实际上取决于您的用例.您真正需要多少速度,传统数据库的其他功能对您来说有多重要?请记住,与传统数据库相比,Redis 不仅仅是客户端可访问的数据结构,它必须将所有内容存储在 RAM 中.要了解这是否适合您,我们需要更多信息.

      Whether or not this is a good way of doing things really depends on your use case. How much speed do you really need, and how important are other features of a traditional database to you? Remember, Redis is much more just datastructures accessible by clients than a traditional database, and it must store everything in RAM. To know whether it's the right thing for you, we'd need more information.

      这篇关于Redis GET 与 SQL SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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