Repository navigation
Implement vertical replacing FINAL - #54366
Conversation
Signed-off-by: Duc Canh Le <[email protected]>
|
This is an automated comment for commit 458c8d7 with description of existing statuses. It's updated for the latest CI running ❌ Click here to open a full report in a separate page Successful checks
|
|
pls resolve conflicts |
|
@nickitat sorry, I've been busy with other works. Will go back to this PR as soon as I can |
- resolve conflicts Signed-off-by: Duc Canh Le <[email protected]>
Signed-off-by: Duc Canh Le <[email protected]>
@canhld94 repeating publicly, not that i think this idea is bad, but i fell into this trap myself 6 months ago. |
Signed-off-by: Duc Canh Le <[email protected]>
Co-authored-by: Joris Giovannangeli <[email protected]> Signed-off-by: Duc Canh Le <[email protected]>
19d2eb8 to
c6aa438
Compare
Co-authored-by: Joris Giovannangeli <[email protected]> Signed-off-by: Duc Canh Le <[email protected]>
Co-authored-by: Joris Giovannangeli <[email protected]> Signed-off-by: Duc Canh Le <[email protected]>
Signed-off-by: Duc Canh Le <[email protected]>
f07fe6f to
b3f4daf
Compare
Signed-off-by: Duc Canh Le <[email protected]>
Signed-off-by: Duc Canh Le <[email protected]>
7f72235 to
dcc464b
Compare
|
Test 02447_drop_database_replica is flaky |
Fix flaky test 02447_drop_database_replica
Signed-off-by: Duc Canh Le <[email protected]>
The overall performance improvement for FINAL is good. |
|
these two descriptions look a little bit orthogonal:
could you pls describe exactly the new approach, its pros and cons? |
|
Let me describe the original idea, hopefully @canhld94 can provide details of the new implemenation which is more advanced. One way to think about this is like vertical vs horizontal merges. So i came to the realization that the bottlneck was not in memory bandwidth, but in memory latency, and the performance cliff happened when cpu cache was full. I confirmed that with trying with different block size and columns size, and FINAL runs fast until there is enough data to fill the cache. Currently, FINAL is a merge sort that works on rows using sort_cursors. For each row, it iterates on all columns, and copy the data to a new block. But if the block is big enough, with enough input stream, it means when it comes back to read/write the next value in one column, the column was evicted from CPU cache because too many data was read/writen in between. With big enough table, it means that every copy of the rows ends up being a memory access, which slows down everything. The idea of this implementation is to prevent that and be more cache friendly. So instead of copying all columns for each rows when merge sort happens, it only works on sorting key to perform merge sorts, and writes an index for all blocks. The index tells if the row must be filtered out, because it is replaced by a another row with more recent version, or kept. This minimize data that is read/written during merge sort, and reduces cache usage. It just emits back the blocks that come from reading, extended with an index. Then after this there is a filter transform. The filter process the blocks, and removes rows based on the index, "collapsing" the columns. This happens columns by columns, and is optimized, so it is also cache friendly because it works vertically instead of horizontally, so memcpy is fast. The pros:
The cons:
|
| /// In the first case, we create a cloned chunk with only one row from `selected_row.row_num` and let selected_row point to it | ||
| /// In the second case, we mark selected_row.owned_chunk = nullptr | ||
| /// In either case, the chunk is "lonely" and if later selected_row is inserted to final result, the chunk will be emitted | ||
| /// immediately. This will create some blocks with only single row, but it's not a big problem. |
There was a problem hiding this comment.
I think we need to consider the possible issue of having too many small chunks (what jorisgio@ pointed out). e.g. we could do the same thing that is done in aggregation by partitions
There was a problem hiding this comment.
Honestly I don't want to squash chunks because the whole point of skipping algorithm is to prevent copy row-by-row.
But squashing into 1MB blocks doesn't look too bad. I will try and see the result.
|
Kindly ping @nickitat @alexey-milovidov |
Signed-off-by: Duc Canh Le <[email protected]>
27837e0 to
fa699a3
Compare
Signed-off-by: Duc Canh Le <[email protected]>
Signed-off-by: Duc Canh Le <[email protected]>
Try fixing CI issues
Signed-off-by: Duc Canh Le <[email protected]>
Resolve conflicts + add some comments Signed-off-by: Duc Canh Le <[email protected]>
Signed-off-by: Duc Canh Le <[email protected]>
|
@nickitat @alexey-milovidov can you also help to review this PR [THANKS]. We've been running this on prod for a month without any issue and stable performance. |
Resolve conflicts Signed-off-by: Duc Canh Le <[email protected]>
Resolve conflicts Signed-off-by: Duc Canh Le <[email protected]>
Signed-off-by: Duc Canh Le <[email protected]>
|
@nickitat I resolves the conflicts, with some small changes:
Do you think we can enable |
Signed-off-by: Duc Canh Le <[email protected]>
f05e32d to
458c8d7
Compare
take it with a pinch of salt. it might be just unstable |
I wouldn't think - I'd just enable ) |
|
The slowdown of base64 has confirmed after merge, so it is stable enough: https://s3.amazonaws.com/clickhouse-test-reports/0/65ac9050322c6e7f37d0e45fabe6e52e558d2a98/performance_comparison_[2_4]/report.html But we cannot do much about it - maybe it is just related to some re-arrangement of the code. |
|
Regarding enabling by default is't it a breaking change? I am not sure if is documented but as far as I know if you run select final without order by and max final thread 1 you will still get results in orders now it won't be true anymore I think? Unless I am mistaken but it might be worth a mention as a breaking change |
|
About result in order, i guess #58120 also can cause results out of order. |





Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
More cache-friendly final implementation.
Note on the behaviour change: previously queries with
FINALmodifier that read with a single stream (e.g.max_threads=1) produced sorted output without explicitly providedORDER BYclause. This behaviour no longer exists whenenable_vertical_final = true(and it is so by default).Original idea from @jorisgio , this PR is co-authored by @jorisgio
Explanation of the idea (original comment):
One way to think about this is like vertical vs horizontal merges.
Currently, FINAL is a merge sort that works on rows using sort_cursors. For each row, it iterates on all columns, and copy the data to a new block. But if the block is big enough, with enough input stream, it means when it comes back to read/write the next value in one column, the column was evicted from CPU cache because too many data was read/writen in between. With big enough table, it means that every copy of the rows ends up being a memory access, which slows down everything.
The idea of this implementation is to prevent that and be more cache friendly. So instead of copying all columns for each rows when merge sort happens, it only works on sorting key to perform merge sorts, and writes an index for all blocks. The index tells if the row must be filtered out, because it is replaced by a another row with more recent version, or kept. This minimize data that is read/written during merge sort, and reduces cache usage. It just emits back the blocks that come from reading, extended with an index.
Then after this there is a filter transform. The filter process the blocks, and removes rows based on the index, "collapsing" the columns. This happens columns by columns, and is optimized, so it is also cache friendly because it works vertically instead of horizontally, so memcpy is fast.
The pros:
this allows to skip non-overlapping blocks and pass it as is with a constant index, which means 0 work for "lonely" chunks. This happens relatively frequently in practice, since there is usually small parts that have low overlap with big old parts.
The cons: