Repository navigation
Conversation
…onary and codes per granule
…ithout copying them
Contributor
|
Workflow [PR], commit [77eef8d] Summary: ❌
|
String columnsString
… by packed size and batch their reads
…and inserts into `MergeTree`
… a function only when it pays
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: #107436
Related: #123531
A prototype of a new
MergeTreeserialization ofStringcolumns. It is a third value ofstring_serialization_version, next tosingle_streamandwith_size_stream:with_dictionaries.On disk
Each granule of a top-level
Stringcolumn in aWidepart is stored in one of two forms:with_size_streamstores them;b,a,b,bbecome the valuesa,band the codes1,0,1,1.The writer holds the rows of a granule until the granule is complete, so it decides from exact sizes. A granule gets a dictionary when both hold:
string_dictionary_min_size_reduction(default 0.1) smaller than the plain form, in uncompressed bytes;string_dictionary_max_distinct_ratio(default 0.75) of its rows.A column has five streams, each with marks per granule:
s.strdict.idx.bins.size.bins.bins.strdict.size.bins.strdict.binA granule of 8,192 rows with three distinct values:
''is code 0 when the granule has it, and two granules with the same values write the same bytes.s.binands.size.bin. The writer drops its empty dictionary streams, and the reader reads such a column exactly aswith_size_stream.Compactparts,StringinsideNullable,Array,MaporTuple, and sparseStringcolumns keepwith_size_stream.In memory
The reader returns the column as
ColumnReplicated(values, indexes), which master already uses for dictionary pages of Parquet and for lazyJOINreplication.valuesis aColumnStringandindexesa column ofUInt8,UInt16orUInt32positions in it.A block covers several granules (about 8 of 8,192 rows at the default
max_block_size), and all of them append to oneColumnReplicated:valuesonce, and its codes, shifted by the number of values already there, toindexes;values, with indexes that point at themselves.Three granules of four rows become one block:
'a'is invaluestwice, once for each granule that has it. The.sizesubcolumn reads the codes and the dictionary lengths, but none of the bytes.A block whose granules are all plain is still a
ColumnReplicated, with indexes0, 1, 2, ...that only add an index to every row. TODO: return such a block as a plainColumnString.Operators
From here it comes down to how each operator handles
ColumnReplicated. Master already runs a function once per entry ofvalueswhen all its replicated arguments share their indexes, and converts the column to a plainColumnStringeverywhere else. This PR makes these operators work on the dictionary values or through the indexes:LIKEandREGEXP_REPLACErun once per dictionary value (master's path). When the substring filter of aPREWHERE(StringValueFilter) matches none of the values of a dictionary granule, its codes are skipped.ORDER BY ... LIMIT: the top-K filter__topKFiltercompares each dictionary value once.length(s),empty(s)ands = '', which reads.size, use the codes and the dictionary lengths.GROUP BYoneStringkey: the keys are read through the indexes, without a copy, and each value is hashed once. When the values of a block repeat, the key of each position is looked up once, in the ordinary aggregation and in the frozen phase of the adaptive aggregator, with one count per position for a lonecount.GROUP BYfixed-width keys and oneString: the string is read through the indexes.GROUP BYthat ignore repeated values (uniqExact,count(DISTINCT),groupUniqArray,min,max) take each used value once.minandmaxof aStringper group read through the indexes, and skip a row that repeats the value and the group of an earlier row of the block.Everything else still converts the column: other aggregates of a
String, keys with two or more strings, merges and mutations.Disk usage
ClickBench
hits(100M rows), one part in each form, bothZSTD(3). The whole table takes 8,686 MB on disk instead of 9,409 MB (−7.7%), and its 28Stringcolumns 4,528 MB compressed instead of 5,253 MB (−14%). Uncompressed, those columns shrink from 43.9 GB to 18.7 GB (−57%), which is what decompression and memory bandwidth pay for.TitleURLRefererOriginalURLPageCharsetSearchPhraseUserAgentMinorBrowserCountryFlashMinor2ParamCurrencyBrowserLanguageHitColorWriting costs a little more CPU: the
INSERT ... SELECTof the whole table takes 393 s of CPU instead of 367 s (+7%).Results
ClickBench
hits(100M rows) on AWSc8g.metal-48xl(192 cores, two NUMA nodes),clickhouse-local,FORMAT Null, each query 3 times and the faster of runs 2 and 3. Master is521c4bcb7b9readinghits; the PR reads the same table rewritten withstring_serialization_version = 'with_dictionaries'. At 16 and 64 threads both run on one NUMA node, at 192 threads on both with interleaved memory. The table has the 26 queries that read aStringcolumn; speedup is master / PR, so above 1 is faster.count(DISTINCT SearchPhrase)GROUP BY MobilePhoneModel:count(DISTINCT UserID), whereMobilePhoneModel <> ''GROUP BY MobilePhone, MobilePhoneModel:count(DISTINCT UserID), whereMobilePhoneModel <> ''GROUP BY SearchPhrase:count, whereSearchPhrase <> ''GROUP BY SearchPhrase:count(DISTINCT UserID), whereSearchPhrase <> ''GROUP BY SearchEngineID, SearchPhrase:count, whereSearchPhrase <> ''GROUP BY UserID, SearchPhrase:countGROUP BY UserID, SearchPhrase:count,LIMIT 10withoutORDER BYGROUP BY UserID, minute(EventTime), SearchPhrase:countcount, whereURL LIKE '%google%'GROUP BY SearchPhrase:min(URL),count, whereURL LIKE '%google%'GROUP BY SearchPhrase:min(URL),min(Title),count,count(DISTINCT UserID), whereTitle LIKE '%Google%'SELECT *whereURL LIKE '%google%',ORDER BY EventTime LIMIT 10SELECT SearchPhrasewhereSearchPhrase <> '',ORDER BY EventTime LIMIT 10SELECT SearchPhrasewhereSearchPhrase <> '',ORDER BY SearchPhrase LIMIT 10SELECT SearchPhrasewhereSearchPhrase <> '',ORDER BY EventTime, SearchPhrase LIMIT 10GROUP BY CounterID:avg(length(URL)),count, whereURL <> ''GROUP BYthe domain ofReferer:avg(length(Referer)),count,min(Referer)GROUP BY SearchEngineID, ClientIP:count,sum,avg, whereSearchPhrase <> ''GROUP BY WatchID, ClientIP:count,sum,avg, whereSearchPhrase <> ''GROUP BY URL:countGROUP BY 1, URL:countGROUP BY URL:count, whereCounterID = 62and moreGROUP BY Title:count, whereCounterID = 62and moreGROUP BY URL:count, whereCounterID = 62and more,OFFSET 1000GROUP BY TraficSourceID, SearchEngineID, AdvEngineID, Src, Dst:count, whereCounterID = 62Over all 43 queries the total time is 1.19× faster at 16 threads, 1.13× at 64 and 1.09× at 192, and the queries without strings stay the same. The
LIKEqueries (Q20-Q22) gain most at 16 threads, where the decompression and the search they save weigh most; the sorting queries (Q24-Q26) gain most at 192 threads.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add the experimental serialization
with_dictionariesofStringcolumns inMergeTree(string_serialization_version): inWideparts, each granule of a top-levelStringcolumn is stored either as before or as a sorted dictionary of its distinct values plus a code per row, whichever is smaller, and it is read as aColumnReplicated, so that functions, filters,GROUP BYandlengthwork once per distinct value. On ClickBench, theStringcolumns take 14% less space compressed and 57% less uncompressed, and the 26 queries that read them are 1.13x faster at 16 threads, 1.11x at 64 threads and 1.13x at 192 threads (geometric mean). The new settingsstring_dictionary_min_size_reduction(0.1) andstring_dictionary_max_distinct_ratio(0.75) control when a granule gets a dictionary.Workflow [PR]
Sync PR [sync-upstream/pr/133397]