注意:由於是重復數據,詞法不具有通用性!文章價值不大!
摘自:https://segmentfault.com/a/1190000002695169
Doc Values 會壓縮存儲重復的內容。 給定這樣一個簡單的 mapping
mappings = {
'testdata': { '_source': {'enabled': False}, '_all': {'enabled': False}, 'properties': { 'name': { 'type': 'string', 'index': 'no', 'store': False, 'dynamic': 'strict', 'fielddata': {'format': 'doc_values'} } } } }
插入100萬行隨機的重復值
words = ['hello', 'world', 'there', 'here'] def read_test_data_in_batches(): batch = [] for i in range(10000 * 100): if i % 50000 == 0: print(i) if len(batch) > 10000: yield batch batch = [] batch.append({ '_index': 'wentao-test-doc-values', '_type': 'testdata', '_source': {'name': random.choice(words)} }) print(i) yield batch
磁盤占用是
size: 28.5Mi (28.5Mi) docs: 1,000,000 (1,000,000)
把每個word搞長一些,同樣是插入100萬行
words = ['hello' * 100, 'world' * 100, 'there' * 100, 'here' * 100] def read_test_data_in_batches(): batch = [] for i in range(10000 * 100): if i % 50000 == 0: print(i) if len(batch) > 10000: yield batch batch = [] batch.append({ '_index': 'wentao-test-doc-values', '_type': 'testdata', '_source': {'name': random.choice(words)} }) print(i) yield batch
磁盤占用不升反降
size: 14.4Mi (14.4Mi) docs: 1,000,000 (1,000,000)
這說明了lucene在底層用列式存儲這些字符串的時候是做了壓縮的。這個要是在某個商業列式數據庫里,就這么點優化都是要大書特書的dictionary encoding優化雲雲。
Nested Document
實驗表明把一堆小文檔打包成一個大文檔的nested document可以壓縮存儲空間。把前面的mapping改成這樣:
mappings = {
'testdata': { '_source': {'enabled': False}, '_all': {'enabled': False}, 'properties': { 'children': { 'type': 'nested', 'properties': { 'name': { 'type': 'string', 'index': 'no', 'store': False, 'dynamic': 'strict', 'fielddata': {'format': 'doc_values'} } } } } } }
還是插入100萬行,但是每一千行打包成一個大文檔
words = ['hello', 'world', 'there', 'here'] def read_test_data_in_batches(): batch = [] for i in range(10000 * 100): if i % 50000 == 0: print(i) if len(batch) > 1000: yield [{ '_index': 'wentao-test-doc-values2', '_type': 'testdata', '_source': {'children': batch} }] batch = [] batch.append({'name': random.choice(words)}) print(i) yield [{ '_index': 'wentao-test-doc-values2', '_type': 'testdata', '_source': {'children': batch} }]
磁盤占用是
size: 2.47Mi (2.47Mi) docs: 1,001,000 (1,001,000)
文檔數沒有變小,但是磁盤空間僅僅占用了2.47M。這個應該受益於lucene內部對於嵌套文檔的存儲優化。
