Skip to content

MergeTree FINAL extract non intersecting parts ranges - #58120

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
kitaisreal:merge-tree-final-extract-non-intersecting-parts-ranges
Dec 28, 2023
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
kitaisreal:merge-tree-final-extract-non-intersecting-parts-ranges

Conversation

@kitaisreal

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

Extract non intersecting parts ranges from MergeTree table during FINAL processing. That way we can avoid additional FINAL logic for this non intersecting parts ranges. In case when amount of duplicate values with same primary key is low, performance will be almost the same as without FINAL. Improve reading performance for MergeTree FINAL when do_not_merge_across_partitions_select_final setting is set.

@kitaisreal kitaisreal added the can be tested Allows running workflows for external contributors label Dec 21, 2023
@kitaisreal

Copy link
Copy Markdown
Contributor Author

Table without duplicates:

CREATE TABLE test_table (id UInt64, value String) ENGINE=ReplacingMergeTree ORDER BY id SETTINGS index_granularity = 8192;
INSERT INTO test_table SELECT number, number FROM numbers(500000000);

OPTIMIZE TABLE test_table;

SELECT name, part_type, marks FROM system.parts WHERE table = 'test_table' AND active = 1;

┌─name────────┬─part_type─┬─marks─┐
│ all_1_477_4 │ Wide      │ 61037 │
└─────────────┴───────────┴───────┘

Before:

SELECT * FROM test_table FINAL FORMAT Null

0 rows in set. Elapsed: 3.464 sec. Processed 500.12 million rows, 12.89 GB (144.36 million rows/s., 3.72 GB/s.)
Peak memory usage: 178.61 MiB.

After:

SELECT * FROM test_table FINAL FORMAT Null

0 rows in set. Elapsed: 0.525 sec. Processed 500.00 million rows, 12.89 GB (952.04 million rows/s., 24.54 GB/s.)
Peak memory usage: 43.50 MiB.

The same as without final:

SELECT * FROM test_table FORMAT Null

0 rows in set. Elapsed: 0.526 sec. Processed 500.00 million rows, 12.89 GB (950.44 million rows/s., 24.50 GB/s.)
Peak memory usage: 40.77 MiB.

Table with some intersecting ranges:

DROP TABLE IF EXISTS test_table_intersecting_ranges;
CREATE TABLE test_table_intersecting_ranges (id UInt64, value String) ENGINE=ReplacingMergeTree ORDER BY id SETTINGS index_granularity = 8192;
INSERT INTO test_table_intersecting_ranges SELECT number, number FROM numbers(250000000);
INSERT INTO test_table_intersecting_ranges SELECT number + 200000000, number FROM numbers(250000000);
SYSTEM STOP MERGES test_table_intersecting_ranges;

Before:

SELECT * FROM test_table_intersecting_ranges FINAL FORMAT Null

0 rows in set. Elapsed: 3.279 sec. Processed 500.16 million rows, 12.78 GB (152.53 million rows/s., 3.90 GB/s.)
Peak memory usage: 196.35 MiB.

After:

SELECT * FROM test_table_intersecting_ranges FINAL FORMAT Null

0 rows in set. Elapsed: 1.379 sec. Processed 500.06 million rows, 12.78 GB (362.61 million rows/s., 9.27 GB/s.)
Peak memory usage: 128.50 MiB.

Setting do_not_merge_across_partitions_select_final performance improvements:

Create table:

DROP TABLE IF EXISTS test_table_partitioned;
CREATE TABLE test_table_partitioned (id UInt64, value String) ENGINE=ReplacingMergeTree ORDER BY id PARTITION BY id % 10 SETTINGS index_granularity = 8192;
INSERT INTO test_table_partitioned SELECT number, number FROM numbers(500000000);
OPTIMIZE TABLE test_table_partitioned;

SELECT name, part_type, marks FROM system.parts WHERE table = 'test_table_partitioned' AND active = 1;

┌─name────────┬─part_type─┬─marks─┐
│ 0_1_4767_5  │ Compact   │  6105 │
│ 1_2_4768_5  │ Compact   │  6105 │
│ 2_3_4769_5  │ Compact   │  6105 │
│ 3_4_4770_5  │ Compact   │  6105 │
│ 4_5_4761_5  │ Compact   │  6105 │
│ 5_6_4762_5  │ Compact   │  6105 │
│ 6_7_4763_5  │ Compact   │  6105 │
│ 7_8_4764_5  │ Compact   │  6105 │
│ 8_9_4765_5  │ Compact   │  6105 │
│ 9_10_4766_5 │ Compact   │  6105 │
└─────────────┴───────────┴───────┘

Before:

SELECT * FROM test_table_partitioned FINAL SETTINGS do_not_merge_across_partitions_select_final = 1 FORMAT Null

0 rows in set. Elapsed: 0.891 sec. Processed 500.00 million rows, 12.89 GB (560.99 million rows/s., 14.46 GB/s.)
Peak memory usage: 32.01 MiB.

After:

SELECT * FROM test_table_partitioned FINAL SETTINGS do_not_merge_across_partitions_select_final = 1 FORMAT Null

0 rows in set. Elapsed: 0.610 sec. Processed 500.00 million rows, 12.89 GB (819.56 million rows/s., 21.13 GB/s.)
Peak memory usage: 54.96 MiB.

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-performance Pull request with some performance improvements label Dec 21, 2023
@robot-ch-test-poll2

robot-ch-test-poll2 commented Dec 21, 2023 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit df6bec5 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
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker image for serversThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style CheckRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Check nameDescriptionStatus
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR⏳ pending
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests❌ failure
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts❌ failure

@UnamedRus

Copy link
Copy Markdown
Contributor

For ReplacingMergeTree, test like that is more likely

DROP TABLE IF EXISTS test_table_intersecting_ranges;
CREATE TABLE test_table_intersecting_ranges (id UInt64, value String) ENGINE=ReplacingMergeTree ORDER BY id SETTINGS index_granularity = 8192;
INSERT INTO test_table_intersecting_ranges SELECT number , number FROM numbers(500000000);
INSERT INTO test_table_intersecting_ranges SELECT number*50000, number FROM numbers(20);

SYSTEM STOP MERGES test_table_intersecting_ranges;

@kitaisreal

Copy link
Copy Markdown
Contributor Author

For ReplacingMergeTree, test like that is more likely

DROP TABLE IF EXISTS test_table_intersecting_ranges;
CREATE TABLE test_table_intersecting_ranges (id UInt64, value String) ENGINE=ReplacingMergeTree ORDER BY id SETTINGS index_granularity = 8192;
INSERT INTO test_table_intersecting_ranges SELECT number , number FROM numbers(500000000);
INSERT INTO test_table_intersecting_ranges SELECT number*50000, number FROM numbers(20);

SYSTEM STOP MERGES test_table_intersecting_ranges;

Thanks for test.

Before:

SELECT * FROM test_table_intersecting_ranges FINAL FORMAT Null

0 rows in set. Elapsed: 3.343 sec. Processed 500.12 million rows, 12.89 GB (149.62 million rows/s., 3.86 GB/s.)
Peak memory usage: 239.85 MiB.

After:

SELECT * FROM test_table_intersecting_ranges FINAL FORMAT Null

0 rows in set. Elapsed: 0.629 sec. Processed 489.72 million rows, 12.62 GB (779.03 million rows/s., 20.08 GB/s.)
Peak memory usage: 46.30 MiB.

@alexey-milovidov alexey-milovidov added the 🎅 🎁 gift🎄 To make people wonder label Dec 21, 2023
@kitaisreal
kitaisreal force-pushed the merge-tree-final-extract-non-intersecting-parts-ranges branch from f83578b to 19843ed Compare December 21, 2023 13:12
@UnamedRus UnamedRus mentioned this pull request Dec 21, 2023
1 task
@kitaisreal
kitaisreal force-pushed the merge-tree-final-extract-non-intersecting-parts-ranges branch 2 times, most recently from e7eb1eb to c1f1a0e Compare December 22, 2023 14:31
@kitaisreal
kitaisreal force-pushed the merge-tree-final-extract-non-intersecting-parts-ranges branch 2 times, most recently from 86d3503 to a85e2cd Compare December 23, 2023 11:50
@kitaisreal
kitaisreal force-pushed the merge-tree-final-extract-non-intersecting-parts-ranges branch from a85e2cd to c595906 Compare December 25, 2023 09:51
@alexey-milovidov alexey-milovidov self-assigned this Dec 27, 2023
*
* Additional details:
*
* 1. If part level is 0, we must process all ranges from this part, because they can contain duplicate primary keys.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumption is incorrect for ReplacingMergeTree with is_deleted column, and that's why 02814_ReplacingMergeTree_fix_select_final_on_single_partition was broken
See also 95e4b00

@tavplubix

Copy link
Copy Markdown
Member

The code is quite tricky and it handles many corner cases, so please consider introducing a setting like optimize_non_intersecting_ranges_on_final that allows disabling this optimization (just in case).

Also, there's a lack of introspection/logging, so it will be hard to debug potential issues

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 🎅 🎁 gift🎄 To make people wonder pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants