简体   繁体   English

在redis中创建中到大型列表/ set / zset / hash的最有效方法是什么?

[英]What's the most efficient way to create a medium to large list/set/zset/hash in redis?

Using redis , there are a number of commands to retrieve entire data structures ( LRANGE for lists, SMEMBERS for sets, ZRANGE for sorted sets, and HGETALL for hashes). 使用redis ,有许多命令可以检索整个数据结构( 列表LRANGE ,集合为SMEMBERS ,排序集为ZRANGE ,哈希为HGETALL )。

Only the hash has a method ( HMSET ) to insert multiple items with a single command. 只有散列有一个方法( HMSET )才能用一个命令插入多个项目。

All of the examples I've seen have shown just adding a single item at a time to a list (through RPUSH or LPUSH ) or a set (through SADD / ZADD ). 我看到的所有示例都显示只是一次添加一个项目到列表(通过RPUSHLPUSH )或一组(通过SADD / ZADD )。

The more specific problem that I want to solve is to create lists and sorted sets containing database ids, these lists are unique to each user and contain a few hundred to a few thousand ids. 我想解决的更具体的问题是创建包含数据库ID的列表和排序集,这些列表对每个用户都是唯一的,并且包含几百到几千个ID。

They are normally gathered from a database query, massaged a little bit in memory and then stored in redis for either paginating through (lists) or doing set based operations on to retrieve subsets (sets and sorted sets). 它们通常从数据库查询中收集,在内存中按摩一点,然后存储在redis中,用于分页(列表)或执行基于集合的操作以检索子集(集合和排序集合)。

Currently, I'm iterating through the list and calling the appropriate add method for each element. 目前,我正在遍历列表并为每个元素调用适当的add方法。 This has the drawbacks of making multiple requests over the wire and repeating the key every time. 这具有通过线路发出多个请求并且每次都重复密钥的缺点。

redis> RPUSH employee:ids 1000
(integer) 1
redis> RPUSH employee:ids 1001
(integer) 2
redis> RPUSH employee:ids 1002
(integer) 3
redis> RPUSH employee:ids 1003
(integer) 4
redis> del employee:ids
(integer) 1

I'm thinking that using a transaction with MULTI and EXEC could help to turn it into a single request, but that wouldn't help with the repeated key. 我认为使用具有MULTIEXEC事务可以帮助将其转换为单个请求,但这对重复的密钥没有帮助。

redis> MULTI
OK
redis> RPUSH employee:ids 1000
QUEUED
redis> RPUSH employee:ids 1001
QUEUED
redis> RPUSH employee:ids 1002
QUEUED
redis> RPUSH employee:ids 1003
QUEUED
redis> RPUSH employee:ids 1004
QUEUED
redis> EXEC
1. (integer) 1
2. (integer) 2
3. (integer) 3
4. (integer) 4
5. (integer) 5

Is there something that I'm missing that would let me add elements to lists/sets in a single command, or without repeating the key every time? 是否有一些我缺少的东西可以让我在单个命令中添加元素到列表/集合,或者每次都不重复键?

I'm also using the jedis client library if that matters (or if there's another library that I can use from the JVM that'd be better). 我也使用jedis客户端库,如果这很重要(或者如果有另一个我可以从JVM使用的库更好)。

Setting up a transaction won't help here. 设置交易在这里没有用。 The purpose of transactions is to ensure the commands get executed at the same time so you never have half a list on the server. 事务的目的是确保命令同时执行,这样您就不会在服务器上有半个列表。 They still get sent one at a time. 他们仍然会一次发送一个。

Are the multiple commands really causing performance issues? 多个命令真的会导致性能问题吗? 1000 items isn't actually that many, and as long as you aren't doing something like opening a new connection for each item there shouldn't be any latency issues. 1000个项目实际上并不多,只要您没有为每个项目打开新连接,就不会出现任何延迟问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从RDMS提取大列表数据的最有效方法是什么? - What's the most efficient way to pull data for a large list from an RDMS? 在OpenGL中管理大量行的最有效方法是什么? - What is the most efficient way to manage a large set of lines in OpenGL? 在python中生成集合组合的最有效内存方法是什么? - What's the most memory efficient way to generate the combinations of a set in python? 组合两个List(Of String)的最有效方法是什么? - What's the most efficient way to combine two List(Of String)? 将大型整数列表与较小的整数列表进行比较的最有效方法是什么? - What is the Most Efficient way to compare large List of integers to smaller List of integers? 清除数组的最有效方法是什么? - What's the most efficient way to clear an array? 什么是最有效的Python睡眠方式? - What's the most efficient way to sleep in Python? 编写此方法最有效的方法是什么? - What's the most efficient way to write this method? 什么是存储数千个中型文档的最有效的面向文档的数据库引擎? - What's the most efficient document-oriented database engine to store thousands of medium sized documents? 在iOS中追踪时间的最有效/最精确的方法是什么? - What's the most efficient / most precise way to track time in iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM