当前位置:Gxlcms > mysql > 关于Redis的几种数据库设计方案的内存占用测试

关于Redis的几种数据库设计方案的内存占用测试

时间:2021-07-01 10:21:17 帮助过:12人阅读

最近在做一个项目,数据库使用的是 Redis。在设计数据结构时,不知道哪种实现是最优的,于是做了下测试。 测试环境如下: OS X10.8.3 Redis 2.6.12 Python 2.7.4 redis-py 2.7.2 hiredis 0.1.1 ujson 1.30 MessagePack 0.3.0 注意: 因为是拿 Python 测试的

最近在做一个项目,数据库使用的是 Redis。在设计数据结构时,不知道哪种实现是最优的,于是做了下测试。

测试环境如下:
OS X10.8.3
Redis 2.6.12
Python 2.7.4
redis-py 2.7.2
hiredis 0.1.1
ujson 1.30
MessagePack 0.3.0
注意:
  1. 因为是拿 Python 测试的,所以可能对其他语言并不完全适用。
  2. 使用的测试数据是特定的,可能对更小或更大的数据并不完全适用。

测试结果就不列出了,直接说结论吧。
  1. 最差的存储方式就是用一个 hash 来存储一个实体(即一条记录)。时间上比其他方案慢 1 ~ 2 倍,空间占用较大。
    更重要的是拿出来的字段类型是字符串,还得自己转换类型。
    唯一的好处就是可以单独操作一个字段。
  2. 使用 string 类型来存储也是不推荐的,不过稍好于前一种方式。在单个实体较小时,会暴露出 key 占用内存较多的缺点。
  3. 用一个 hash 来存储一个类型的所有实体(即一张表),在实现上比较简单,内存占用尚可。
  4. 用多个 hash 来存储一个类型的所有实体(即分表),在实现上稍微复杂点,但占用的内存最小。
    如果单个字段值较小(缺省值是 64 字节),单个 hash 存储的字段数不多(缺省值是 512 个)时,会采用 hash zipmap 来存储,内存占用会显著减小。
    单个 hash 存储的字段数建议为 2 的次方,例如 1024。略微超过这个值,会导致内存占用和延迟时间都增加。
    Instagram 的工程师认为,使用 hash zipmap 时,最佳的字段数为 1000 左右。不过据我测试,基本都是随字段数增加而变慢,而内存占用从 128 直到 1024 的变化基本可以忽略。
  5. 存储为 JSON 格式是种不错的选择。对包含中文的内容来说,设置 ensure_ascii=False 可以节省大量内存。
    ujson 比 json 性能好很多,后者在设置 ensure_ascii=False 后性能急剧下降。
  6. cPickle 比 ujson 的性能要差,不过支持更多类型(如 datetime)。
  7. MessagePack 比 ujson 有一点不太明显的性能优势,不过丧失了可读性,且取回 unicode 需要自己 decode。
    号称比 Protocol Buffer 快 4 倍应该可以无视了,至少其 Python 库没有明显优势。
  8. 使用 zlib 压缩可以节省更多内存,不过性能变慢 1 ~ 2 倍。
看这个测试结果,感觉还不如用 MongoDB 省事……

最后附上测试代码:
  1. # -*- coding: utf-8 -*-
  2. import cPickle
  3. import json
  4. import time
  5. import zlib
  6. import msgpack
  7. import redis
  8. import ujson
  9. class Timer:
  10. def __enter__(self):
  11. self.start = time.time()
  12. return self
  13. def __exit__(self, *args):
  14. self.end = time.time()
  15. self.interval = self.end - self.start
  16. def test(function):
  17. def wrapper(*args, **kwargs):
  18. args_list = []
  19. if args:
  20. args_list.append(','.join((str(arg) for arg in args)))
  21. if kwargs:
  22. args_list.append(','.join('%s=%s' % (key, value) for key, value in kwargs.iteritems()))
  23. print 'call %s(%s):' % (function.func_name, ', '.join(args_list))
  24. redis_client.flushall()
  25. print 'memory:', redis_client.info()['used_memory_human']
  26. with Timer() as timer:
  27. result = function(*args, **kwargs)
  28. print 'time:', timer.interval
  29. print 'memory:', redis_client.info()['used_memory_human']
  30. print
  31. return result
  32. return wrapper
  33. redis_client = redis.Redis()
  34. pipe = redis_client.pipeline(transaction=False)
  35. articles = [{
  36. 'id': i,
  37. 'title': u'团结全世界正义力量痛击日本',
  38. 'content': u'近期日本社会有四种感觉极度高涨,即二战期间日本军国主义扩张战争的惨败在日本右翼势力内心留下的耻辱感;被美国长期占领和控制的压抑感;经济长期停滞不前的焦虑感;对中国快速崛起引发的失落感。为此,日本为了找到一个发泄口,对中国采取了一系列挑衅行为,我们不能听之任之。现在全国13亿人要万众一心,团结起来,拿出决心、意志和能力,果断实施对等反击。在这场反击日本右翼势力的反攻倒算中,中国不是孤立的,我们要团结全世界一切反法西斯战争的正义力量,痛击日本对国际正义的挑战。',
  39. 'source_text': u'环球时报',
  40. 'source_url': 'http://opinion.huanqiu.com/column/mjzl/2012-09/3174337.html',
  41. 'time': '2012-09-13 09:23',
  42. 'is_public': True
  43. } for i in xrange(10000)]
  44. @test
  45. def test_hash():
  46. for article in articles:
  47. pipe.hmset('article:%d' % article['id'], article)
  48. pipe.execute()
  49. @test
  50. def test_json_hash():
  51. for article in articles:
  52. pipe.hset('article', article['id'], json.dumps(article))
  53. pipe.execute()
  54. @test
  55. def test_ujson_hash():
  56. for article in articles:
  57. pipe.hset('article', article['id'], ujson.dumps(article))
  58. pipe.execute()
  59. @test
  60. def test_ujson_string():
  61. for article in articles:
  62. pipe.set('article:%d' % article['id'], ujson.dumps(article))
  63. pipe.execute()
  64. @test
  65. def test_zlib_ujson_string():
  66. for article in articles:
  67. pipe.set('article:%d' % article['id'], zlib.compress(ujson.dumps(article, ensure_ascii=False)))
  68. pipe.execute()
  69. @test
  70. def test_msgpack():
  71. for article in articles:
  72. pipe.hset('article', article['id'], msgpack.packb(article))
  73. pipe.execute()
  74. @test
  75. def test_pickle_string():
  76. for article in articles:
  77. pipe.set('article:%d' % article['id'], cPickle.dumps(article))
  78. pipe.execute()
  79. @test
  80. def test_json_without_ensure_ascii():
  81. for article in articles:
  82. pipe.hset('article', article['id'], json.dumps(article, ensure_ascii=False))
  83. pipe.execute()
  84. @test
  85. def test_ujson_without_ensure_ascii():
  86. for article in articles:
  87. pipe.hset('article', article['id'], ujson.dumps(article, ensure_ascii=False))
  88. pipe.execute()
  89. def test_ujson_shard_id():
  90. @test
  91. def test_ujson_shard_id_of_size(size):
  92. for article in articles:
  93. article_id = article['id']
  94. pipe.hset('article:%d' % (article_id / size), article_id % size, ujson.dumps(article, ensure_ascii=False))
  95. pipe.execute()
  96. for size in (2, 4, 8, 10, 16, 32, 64, 100, 128, 256, 500, 512, 513, 1000, 1024, 1025, 2048, 4096, 8092):
  97. test_ujson_shard_id_of_size(size)
  98. test_ujson_shard_id_of_size(512)
  99. for key, value in sorted(globals().copy().iteritems(), key=lambda x:x[0]):
  100. if key.startswith('test_'):
  101. value()

人气教程排行