减少Redis内存使用

减少Redis内存使用,redis,Redis,我正在将现有的调度数据集移动到redis。此数据包含计划、用户和用户。这是一种多对多的关系 我将完整的计划列表存储在一个计分zset中,其中计分是计划日期的时间戳。我这样存储它,这样我就可以很容易地找到所有已经过去的时间表,并根据这些时间表采取行动 我还需要能够找到属于某个用户的所有计划,这样每个用户都有自己的包含重复信息的zset 因此,数据可能如下所示: s_1000: [ (100, "{..}"), (101, "{..}") ] # the

我正在将现有的调度数据集移动到redis。此数据包含计划、用户和用户。这是一种多对多的关系

我将完整的计划列表存储在一个计分zset中,其中计分是计划日期的时间戳。我这样存储它,这样我就可以很容易地找到所有已经过去的时间表,并根据这些时间表采取行动

我还需要能够找到属于某个用户的所有计划,这样每个用户都有自己的包含重复信息的zset

因此,数据可能如下所示:

s_1000: [ (100, "{..}"), (101, "{..}") ]   # the schedules key
us_abc: [ (100, "{..}"), ]                 # a users schedules key
us_efg: [ (100, "{..}"), ]                 # another users schedules key
"{\"di\":10000,\"ci\":10000,\"si\":10000,\"p\":\"M14IB5A2830TE4KSSEGY0ZDX37V93FYX\",\"sse\":false}"
实际记录如下所示:

s_1000: [ (100, "{..}"), (101, "{..}") ]   # the schedules key
us_abc: [ (100, "{..}"), ]                 # a users schedules key
us_efg: [ (100, "{..}"), ]                 # another users schedules key
"{\"di\":10000,\"ci\":10000,\"si\":10000,\"p\":\"M14IB5A2830TE4KSSEGY0ZDX37V93FYX\",\"sse\":false}"
我已经缩短了密钥,甚至可以将它们连同json格式一起删除,以获得真正最小的负载,但是所有的数据都需要在那里

仅此字符串就只有85个字符。因为每个记录都有一个副本,所以这个记录总共有170个字符。这一点的关键是另外42个字符的
usm14ib5a2830te4kssegy0zdx37v93fyx_yymmd
。总共,我只看到存储此数据所需的255个字节

我已经按照我描述的方式插入了10万条记录,就像这条一样。据我计算,这应该只需要25mb,但我发现它的存储空间远远超过了200mb

该有效负载的
内存信息为344字节(x100k=33mb)
schedules键的
内存信息为18108652字节(18mb)

时间表mem的用法看起来是正确的

以下是
内存统计信息
s:

memory stats
 1) "peak.allocated"
 2) (integer) 3343080744
 3) "total.allocated"
 4) (integer) 201656296
 5) "startup.allocated"
 6) (integer) 3668896
 7) "replication.backlog"
 8) (integer) 0
 9) "clients.slaves"
10) (integer) 0
11) "clients.normal"
12) (integer) 1189794
13) "aof.buffer"
14) (integer) 0
15) "lua.caches"
16) (integer) 0
17) "db.0"
18) 1) "overhead.hashtable.main"
    2) (integer) 5850304
    3) "overhead.hashtable.expires"
    4) (integer) 4249632
19) "overhead.total"
20) (integer) 14958626
21) "keys.count"
22) (integer) 100036
23) "keys.bytes-per-key"
24) (integer) 1979
25) "dataset.bytes"
26) (integer) 186697670
27) "dataset.percentage"
28) "94.297752380371094"
29) "peak.percentage"
30) "6.0320491790771484"
31) "allocator.allocated"
32) (integer) 202111512
33) "allocator.active"
34) (integer) 204464128
35) "allocator.resident"
36) (integer) 289804288
37) "allocator-fragmentation.ratio"
38) "1.011640191078186"
39) "allocator-fragmentation.bytes"
40) (integer) 2352616
41) "allocator-rss.ratio"
42) "1.4173845052719116"
43) "allocator-rss.bytes"
44) (integer) 85340160
45) "rss-overhead.ratio"
46) "0.98278516530990601"
47) "rss-overhead.bytes"
48) (integer) -4988928
49) "fragmentation"
50) "1.4126673936843872"
51) "fragmentation.bytes"
52) (integer) 83200072
看起来每个键的字节数高达1977字节

为什么每个键使用344字节?可以告诉redis每个字符只使用1字节吗? 为什么redis每个密钥使用这么多字节


有没有一种方法可以更好地组织我的数据,这样我就不会在如此低的数据量(我需要100ms的记录)上使用redis。

只是一个猜测:你有没有尝试使用哈希而不是字符串来存储记录?我没有。问题是需要在同一个密钥中存储哈希列表。