redis哈希与字符串内存成本
我做了一些实验,看起来redis散列几乎总是比字符串更节省空间,即使散列只包含一个字段redis哈希与字符串内存成本,redis,Redis,我做了一些实验,看起来redis散列几乎总是比字符串更节省空间,即使散列只包含一个字段 import redis rc = redis.Redis(host='127.0.0.1',port=1234) rc.flushdb() pipe = rc.pipeline() for i in range(1000000): pipe.set('stream:'+str(i)+':target',True) f = pipe.execute() # uses 236 MB rc.f
import redis
rc = redis.Redis(host='127.0.0.1',port=1234)
rc.flushdb()
pipe = rc.pipeline()
for i in range(1000000):
pipe.set('stream:'+str(i)+':target',True)
f = pipe.execute()
# uses 236 MB
rc.flushdb()
pipe = rc.pipeline()
for i in range(1000000):
pipe.hset('stream:'+str(i),'target',True)
f = pipe.execute()
# uses 170 MB
rc.flushdb()
pipe = rc.pipeline()
for i in range(500000):
pipe.set('stream:'+str(i)+':target',True)
pipe.set('stream:'+str(i)+':ws',True)
f = pipe.execute()
# uses 238 MB
rc.flushdb()
pipe = rc.pipeline()
for i in range(500000):
pipe.hset('stream:'+str(i),':target',True)
pipe.hset('stream:'+str(i),':ws',True)
f = pipe.execute()
# uses 113 MB
哈希和字符串都有O(1)摊销的写/读成本。如果我不需要使用像APPEND、GETBIT、SETBIT、RANGE等最奇特的操作,而只使用SET/GET语义,那么散列不是更可取吗?我错过了什么疯狂的东西吗?另外,我很想知道为什么哈希更节省空间。本文讨论了您提出的一些问题
redis的建议是,如果可以将数据表示为散列,则使用散列。“小散列是在非常小的空间中编码的,因此您应该尝试在每次可能的情况下使用散列来表示数据”
如果redis可以将散列打包到一个数组中,并且仍然以O(1)进行查询,则redis认为散列很小。在连续的内存区域中存储数据也有助于提高性能,特别是当阵列的相邻元素是在机箱上缓存线的帧内读取时
在redis配置中,您可以找到以下设置:
# Hashes are encoded using a memory efficient data structure when they have a
# small number of entries, and the biggest entry does not exceed a given
# threshold. These thresholds can be configured using the following directives.
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
您需要在上述阈值的两侧重复测试。总的来说,最好让您的测试尽可能地模拟真实数据和访问模式。您的问题的前提是应该始终使用散列,但请记住,您所依赖的优化启发法对您作为用户来说并不完全透明。您能否在“如果它们很小”中澄清“它们”?散列键?散列字段?相应的值?ps优化页面主要描述将键拆分为整型组件字段时的优化(此处并非如此)@proteneer我在答案中展开了讨论。