Skip to content

Replacing final: an attempt at more cache friendly implementation. - #46810

Closed
jorisgio wants to merge 2 commits into
ClickHouse:masterfrom
jorisgio:vertical_skip_final
Closed

jorisgio wants to merge 2 commits into
ClickHouse:masterfrom
jorisgio:vertical_skip_final

Conversation

@jorisgio

@jorisgio jorisgio commented Feb 24, 2023 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Add a skip_replacing_final merge algorithm for ReplacingMergeTree to improving cache efficiency by merging columns by columns instead of row by row.

Description

This PR is a draft, i'm not very proficient in C++ or clickhouse and there is a major bug somewhere, Trying to get feedback on the idea and if the optimisation is worth it.

Currently, FINAL for ReplacingMergeTree uses an horizontal implementation. If you run FINAL with a ~5/10 columns, memcpy and moving data to MergedData chunk shows up in profile as ~40% of cpu. For a long time, i thought it was hitting memory bandwidth limitation, but in fact, it seems to be more a access latency/cache bottleneck.

To work around this, this is an attempt at implementing merging more vertically and with less copy, while dropping the benefit that the output stream remains sorted by PK.

There are two optimisations :

  1. while merge sorting chunks, we can skip chunks if the last PK in block is lower than the current cursor, we can pass the chunk without copying it, as it is fully merged.
  2. Instead of rewriting chunks row by row, we can reuse filter transform used for where, and write a boolean filter column, and then filtering chunk columns by columns, which is a lot more cache friendly.

On tests, it seems to make no impact on single columns/small final up to twice faster when doing final on table with many columns.

@CLAassistant

CLAassistant commented Feb 24, 2023 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@nickitat nickitat self-assigned this Feb 28, 2023
@nickitat nickitat added the can be tested Allows running workflows for external contributors label Feb 28, 2023
@alexey-milovidov

Copy link
Copy Markdown
Member

Let's fill the changelog entry and fix the style so that the tests can run.

@alexey-milovidov

Copy link
Copy Markdown
Member

I like the idea; let's see how it will go!

@jorisgio
jorisgio force-pushed the vertical_skip_final branch from fcddc46 to c5f4de5 Compare March 28, 2023 10:28
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-improvement Pull request with some product improvements submodule changed At least one submodule changed in this PR. labels Mar 28, 2023
@jorisgio
jorisgio force-pushed the vertical_skip_final branch from c5f4de5 to f4b02aa Compare March 28, 2023 10:38
@robot-ch-test-poll4 robot-ch-test-poll4 removed the submodule changed At least one submodule changed in this PR. label Mar 28, 2023
@jorisgio
jorisgio force-pushed the vertical_skip_final branch from bf189c6 to 0e4ce00 Compare March 29, 2023 15:11
@jorisgio
jorisgio force-pushed the vertical_skip_final branch from 0e4ce00 to 2d2b645 Compare March 30, 2023 07:51
@jorisgio

Copy link
Copy Markdown
Contributor Author

fwiw i'm not super convinced by this. It works on benchmarks with many columns/some string columns, can be as better as twice faster when just fetching data with FINAL. It is a bit slower with low columns counts / small numbers columns.
But in real world it i tried it at scale and it makes no real difference in performance. This would have good impact for actual merges, but merges require the data to be in order, which it does not provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants