2012-10-08 14 views
15

मैं this article पढ़ रहा था जो रेडिस में 1 मिलियन चाबियों को संग्रहीत करने का उल्लेख करता है 17 जीबी मेमोरी का उपयोग करेगा। हालांकि, 1k प्रत्येक (पूर्व: HSET "mediabucket:1155" "1155315" "939") पर उन्हें हंक करने के लिए स्विच करते समय उन्हें 5 जीबी में 1 एम स्टोर करने की अनुमति मिलती है जो एक बहुत बड़ी बचत है।एचएसईटी बनाम एसईटी स्मृति उपयोग?

मैंने redis memory-optimization पढ़ा लेकिन मुझे अंतर समझ में नहीं आया। यह कहता है कि एचजीईटी काफी ओ (1) नहीं है लेकिन हैससेट का उपयोग करते समय पर्याप्त बंद है और अधिक सीपीयू उपयोग का उल्लेख करता है। मुझे समझ में नहीं आता कि क्यों अधिक सीपीयू उपयोग होगा (अंतरिक्ष के लिए निश्चित व्यापार समय। लेकिन कैसे/क्या?)। यह 'एन्कोडिंग' का उल्लेख करता है लेकिन यह नहीं कि वे इसे कैसे एन्कोड करते हैं।

यह केवल स्ट्रिंग का उल्लेख करता है लेकिन मुझे नहीं पता कि केवल स्ट्रिंग का मतलब क्या है। क्या यह हैश क्षेत्र है? क्या इसका मतलब हैश फ़ील्ड? मुझे HSET में इसके बारे में कुछ भी दिखाई नहीं देता है। वास्तव में एन्कोड किया जाएगा और एसईटी का उपयोग करते हुए एन्कोडिंग अधिक कुशल क्यों होगी?

यह कैसे संभव है HSET "mediabucket:1155" "1155315" "939"SET "mediabucket:1155315" "939" अधिक कुशल है? एसईटी में कम डेटा (1155315 ए डी 1155 का उपयोग 1155315 के बजाय किया जाता है)। मैं व्यक्तिगत रूप से द्विआधारी कुंजी का उपयोग करने की कोशिश करता हूं हालांकि मुझे नहीं लगता कि एचएसईटी अधिक कुशल क्यों हैं।

संपादित करें:

क्रॉस के रूप में अच्छी तरह से redis-db मेलिंग सूची पर पोस्ट: https://groups.google.com/d/topic/redis-db/90K3UqciAx0/discussion

उत्तर

19

छोटे हैश वस्तुओं हैश अधिकतम-ziplist-प्रविष्टियों और हैश max- के मूल्यों के आधार पर ziplists के रूप में इनकोड ziplist-value पैरामीटर। यह सरल डेटा क्रमबद्धता है।

एक ziplist परिभाषित किया गया है इस प्रकार है (Redis स्रोत कोड से निकाली गई):

/* The ziplist is a specially encoded dually linked list that is designed 
* to be very memory efficient. It stores both strings and integer values, 
* where integers are encoded as actual integers instead of a series of 
* characters. It allows push and pop operations on either side of the list 
* in O(1) time. However, because every operation requires a reallocation of 
* the memory used by the ziplist, the actual complexity is related to the 
* amount of memory used by the ziplist. 
* 
* ---------------------------------------------------------------------------- 
* 
* ZIPLIST OVERALL LAYOUT: 
* The general layout of the ziplist is as follows: 
* <zlbytes><zltail><zllen><entry><entry><zlend> 
* 
* <zlbytes> is an unsigned integer to hold the number of bytes that the 
* ziplist occupies. This value needs to be stored to be able to resize the 
* entire structure without the need to traverse it first. 
* 
* <zltail> is the offset to the last entry in the list. This allows a pop 
* operation on the far side of the list without the need for full traversal. 
* 
* <zllen> is the number of entries.When this value is larger than 2**16-2, 
* we need to traverse the entire list to know how many items it holds. 
* 
* <zlend> is a single byte special value, equal to 255, which indicates the 
* end of the list. 
* 
* ZIPLIST ENTRIES: 
* Every entry in the ziplist is prefixed by a header that contains two pieces 
* of information. First, the length of the previous entry is stored to be 
* able to traverse the list from back to front. Second, the encoding with an 
* optional string length of the entry itself is stored. 
* 
* The length of the previous entry is encoded in the following way: 
* If this length is smaller than 254 bytes, it will only consume a single 
* byte that takes the length as value. When the length is greater than or 
* equal to 254, it will consume 5 bytes. The first byte is set to 254 to 
* indicate a larger value is following. The remaining 4 bytes take the 
* length of the previous entry as value. 
* 
* The other header field of the entry itself depends on the contents of the 
* entry. When the entry is a string, the first 2 bits of this header will hold 
* the type of encoding used to store the length of the string, followed by the 
* actual length of the string. When the entry is an integer the first 2 bits 
* are both set to 1. The following 2 bits are used to specify what kind of 
* integer will be stored after this header. An overview of the different 
* types and encodings is as follows: 
* 
* |00pppppp| - 1 byte 
*  String value with length less than or equal to 63 bytes (6 bits). 
* |01pppppp|qqqqqqqq| - 2 bytes 
*  String value with length less than or equal to 16383 bytes (14 bits). 
* |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes 
*  String value with length greater than or equal to 16384 bytes. 
* |11000000| - 1 byte 
*  Integer encoded as int16_t (2 bytes). 
* |11010000| - 1 byte 
*  Integer encoded as int32_t (4 bytes). 
* |11100000| - 1 byte 
*  Integer encoded as int64_t (8 bytes). 
* |11110000| - 1 byte 
*  Integer encoded as 24 bit signed (3 bytes). 
* |11111110| - 1 byte 
*  Integer encoded as 8 bit signed (1 byte). 
* |1111xxxx| - (with xxxx between 0000 and 1101) immediate 4 bit integer. 
*  Unsigned integer from 0 to 12. The encoded value is actually from 
*  1 to 13 because 0000 and 1111 can not be used, so 1 should be 
*  subtracted from the encoded 4 bit value to obtain the right value. 
* |11111111| - End of ziplist. 
* 
* All the integers are represented in little endian byte order. 
*/ 

हैश वस्तु से प्रत्येक आइटम ziplist (2 लगातार प्रविष्टियों) में एक कुंजी/मान जोड़ी के रूप में प्रतिनिधित्व किया है। दोनों कुंजी और मानों को एक साधारण स्ट्रिंग, या पूर्णांक के रूप में संग्रहीत किया जा सकता है। यह प्रारूप स्मृति में अधिक कॉम्पैक्ट है क्योंकि यह बहुत से पॉइंटर्स (8 बाइट्स प्रत्येक) को बचाता है जो एक गतिशील डेटा संरचना (जैसे एक वास्तविक हैशटेबल) को लागू करने के लिए आवश्यक हैं।

ज़ीप्लिस्ट पर लागू होने पर एचएसईटी/एचजीईटी ऑपरेशंस वास्तव में ओ (एन) है। यही कारण है कि ज़ीप्लिस्ट को छोटा रखा जाना चाहिए। जब ज़ीप्लिस्ट डेटा एल 1 सीपीयू कैश में फिट होता है, तो संबंधित एल्गोरिदम उनके रैखिक जटिलता के बावजूद पर्याप्त तेज़ होते हैं।

अधिक जानकारी के लिए निम्न लिंक का उल्लेख कर सकते हैं:

Redis 10x more memory usage than data

Redis Data Structure Space Requirements

ये जवाब (सेट, सूची, या अनुसार क्रमबद्ध सेट की तरह) अन्य डेटा संरचना का उल्लेख है, लेकिन यह बिल्कुल वही अवधारणा है।

+3

अच्छा स्पष्टीकरण। –

संबंधित मुद्दे