Redis 数据结构空间需求 [英] Redis Data Structure Space Requirements

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

问题描述

redis 中有序集合和列表的空间有什么区别?我的猜测是排序集是某种平衡的二叉树,而列表是一个链表.这意味着在我为它们中的每一个编码的三个值之上,键、分数、值,尽管我会将链表的分数和值放在一起,但开销是链表需要跟踪一个另一个节点,二叉树需要跟踪两个,所以使用一个有序集合的空间开销是 O(N).

What is the difference in space between sorted sets and lists in redis? My guess is that sorted sets are some kind of balanced binary tree, and lists are a linked list. This means that on top of the three values that I'm encoding for each of them, key, score, value, although I'll munge together score and value for the linkedlist, the overhead is that the linkedlist needs to keep track of one other node, and the binary tree needs to keep track of two, so that the space overhead to using a sorted set is O(N).

如果我的value和score都是longs,并且指向其他节点的指针也是longs,那么在64位计算机上,单个节点的空间开销从3个longs变成4个longs,就是空间增加了 33%.

If my value, and score are both longs, and the pointers to the other nodes are also longs, it seems like the space overhead of a single node goes from 3 longs to 4 longs on a 64-bit computer, which is a 33% increase in space.

这是真的吗?

推荐答案

这远远超出您的估计.假设未使用 ziplists(即您有大量项目).

It is much more than your estimation. Let's suppose ziplists are not used (i.e. you have a significant number of items).

Redis 列表是一个经典的双链表:每个项目有 3 个指针(prev、next、value).

A Redis list is a classical double-linked list: 3 pointers (prev,next,value) per item.

排序集是一个字典加上一个跳过列表.在字典中,项目也将用 3 个指针存储(键、值、下一个).跳过列表内存占用的评估更复杂:每个节点需要 1 个双精度(分数),2 个指针(obj,向后),加上 n 对(指针,跨度值),其中 n 介于 1 和 32 之间.大多数项目将只需要 1或 2 对.

A sorted set is a dictionary plus a skip list. In the dictionary, items will be stored with 3 pointers as well (key,value,next). The skip list memory footprint is more complex to evaluate: each node takes 1 double (score), 2 pointers (obj,backward), plus n couples (pointer,span value) with n between 1 and 32. Most items will take only 1 or 2 couples.

换句话说,当它不表示为 ziplist 时,排序集是迄今为止开销最大的 Redis 数据结构.与列表相比,内存开销超过 200%(即 3 倍).

In other words, when it is not represented as a ziplist, a sorted set is by far the Redis data structure with the most overhead. Compared to a list, the memory overhead is more than 200% (i.e. 3 times).

注意:使用 Redis 评估内存消耗的最佳方法是尝试使用伪数据构建一个大列表或排序集,并使用 INFO 获取内存占用.

Note: the best way to evaluate memory consumption with Redis is to try to build a big list or sorted set with pseudo-data and use INFO to get the memory footprint.

这篇关于Redis 数据结构空间需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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