在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/人工智能/ redis鍵名長度如何影響性能的?

redis鍵名長度如何影響性能的?

鍵名太長,性能不好,大家都這樣說,太短不方面記憶閱讀,我看了看自己用的最長的鍵長度50個字符,這算長嗎?對性能影響有多少呢?如何評估呢?就是如何測試性能?

回答
編輯回答
嘟尛嘴

使用的key在保證能表明存的是什么數(shù)據(jù)的情況下盡可能的短即可。
比如key="article_view_top_10" 表示瀏覽量最高的10篇文章。
50個字符算長了,用了redis這么長時間了,真的沒有用過這么長的key。
千里之堤毀于蟻穴,對性能影響又大多,沒有評估過。長的key意味著占用更多的內(nèi)存,意味著查找匹配的時候消耗更多的時間,很多時候,遵循規(guī)范即可。

2018年7月5日 07:56
編輯回答
莓森

官方涉及到的文章:An introduction to Redis data types and abstractionsRedis keys章節(jié)
原文:

Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like "foo" to the content of a JPEG file. The empty string is also a valid key.

A few other rules about keys:

  • Very long keys are not a good idea. For instance a key of 1024 bytes is a bad idea not only memory-wise, but also because the lookup of the key in the dataset may require several costly key-comparisons. Even when the task at hand is to match the existence of a large value, hashing it (for example with SHA1) is a better idea, especially from the perspective of memory and bandwidth.
  • Very short keys are often not a good idea. There is little point in writing "u1000flw" as a key if you can instead write "user:1000:followers". The latter is more readable and the added space is minor compared to the space used by the key object itself and the value object. While short keys will obviously consume a bit less memory, your job is to find the right balance.
  • Try to stick with a schema. For instance "object-type:id" is a good idea, as in "user:1000". Dots or dashes are often used for multi-word fields, as in comment:1234:reply.to or comment:1234:reply-to.

The maximum allowed key size is 512 MB.

我翻譯的這篇文章:
Redis深入系列-0x014:Redis數(shù)據(jù)類型和概念介紹(上)0x001 Redis的key章節(jié)

Redis的key是比特安全的,這意味著你可以使用任何的二進制序列作為key,從像foo的字符串到一個JPEG文件的內(nèi)容。甚至空字符串也是可以的。

關(guān)于key有一些其他的規(guī)則:

  • 非常長的key是不推薦的。一個1024 bytes是一個非常壞的注意,不僅僅是因為內(nèi)存浪費,更是因為在數(shù)據(jù)集中搜索對比的時候需要耗費更多的成本。當要處理的是匹配一個非常大的值,從內(nèi)存和帶寬的角度來看,使用這個值的hash值是更好的辦法(比如使用SHA1)。
  • 特別短的key通常也是不推薦的。在寫像u100flw這樣的鍵的時候,有一個小小的要點,我們可以用user:1000:followers代替??勺x性更好,對于key對象和value對象增加的空間占用與此相比來說倒是次要的。當短的key可以很明顯減少空間占用的時候,你的工作就是找到正確的平衡
  • 嘗試去固定一個密室。比如object-type:id是一個好主意,-和.通常用于多個字符的域,就像comment:1234:reply.to,或者comment:1234:reply-to
  • 最大的key允許512MB

以上僅供參考

2017年9月15日 00:22