Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-stage PREWHERE enabled by default #46365

Merged
merged 8 commits into from
Jul 21, 2023
Merged

Conversation

davenger
Copy link
Member

@davenger davenger commented Feb 13, 2023

Changelog category (leave one):

  • Performance Improvement

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

Enable move_all_conditions_to_prewhere and enable_multiple_prewhere_read_steps settings by default.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-performance Pull request with some performance improvements label Feb 13, 2023
@davenger davenger marked this pull request as draft February 13, 2023 17:09
@davenger davenger force-pushed the split_prewhere_dag_test_enable branch 7 times, most recently from dd2857e to 838f4c1 Compare February 19, 2023 11:15
@davenger davenger force-pushed the split_prewhere_dag_test_enable branch from 9c22581 to 23fe24c Compare March 2, 2023 11:31
@robot-ch-test-poll1
Copy link
Contributor

robot-ch-test-poll1 commented May 5, 2023

This is an automated comment for commit 6886e84 with description of existing statuses. It's updated for the latest CI running
The full report is available here
The overall status of the commit is 🔴 failure

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
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🟢 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
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
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests🔴 failure
Mergeable CheckChecks if all other necessary checks are successful🟢 success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests🟢 success
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub🟢 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
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🟢 success

@davenger davenger force-pushed the split_prewhere_dag_test_enable branch from 50a4d14 to f4239f9 Compare June 5, 2023 09:09
@davenger davenger changed the title [Do not merge][Testing]Split PREWHERE DAG enabled [Testing] Multi-stage PREWHERE enabled by default Jun 5, 2023
@davenger davenger force-pushed the split_prewhere_dag_test_enable branch 6 times, most recently from 7f636a7 to e0bac26 Compare June 8, 2023 12:46
@davenger davenger force-pushed the split_prewhere_dag_test_enable branch from b171451 to e223668 Compare June 15, 2023 17:08
@davenger davenger force-pushed the split_prewhere_dag_test_enable branch 2 times, most recently from e2a7bc4 to aaf06b0 Compare July 6, 2023 07:00
@davenger davenger force-pushed the split_prewhere_dag_test_enable branch 2 times, most recently from 1ca1f9e to af7f871 Compare July 13, 2023 09:58
@davenger davenger changed the title [Testing] Multi-stage PREWHERE enabled by default Multi-stage PREWHERE enabled by default Jul 15, 2023
@davenger davenger marked this pull request as ready for review July 15, 2023 07:25
@davenger davenger force-pushed the split_prewhere_dag_test_enable branch from 68329c4 to 6886e84 Compare July 16, 2023 06:24
@azat
Copy link
Member

azat commented Jul 16, 2023

@davenger in theory move_all_conditions_to_prewhere may slowdown queries, right? maybe you have some related numbers?

@davenger
Copy link
Member Author

davenger commented Jul 17, 2023

@davenger in theory move_all_conditions_to_prewhere may slowdown queries, right? maybe you have some related numbers?

@azat yes, move_all_conditions_to_prewhere must be used together with enable_multiple_prewhere_read_steps and in this PR they both are enabled. With both settings enabled we should do no more reads reads than previously, and in good cases we should read less. Or maybe you have some example in mind that I'm missing?
There are some numbers in the original PRs description and comment: #46140

@azat
Copy link
Member

azat commented Jul 17, 2023

I'm was worrying extra seeks, but apparently the problem is in the code.

Consider the following example:

CREATE TABLE data
(
    `a` UInt64,
    `b` UInt64,
    `c` UInt64,
    `d` UInt64
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO data SELECT
    number % 1,
    number % 1,
    number % 1,
    number
FROM numbers(100000000.)

Query id: 02488797-7172-45c9-88ba-243955669efe

Ok.

0 rows in set. Elapsed: 1.573 sec. Processed 100.65 million rows, 805.21 MB (63.97 million rows/s., 511.79 MB/s.)

p620.local :) select count() from data where a = 1 and b = 0 and c = 0 settings optimize_move_to_prewhere=0

SELECT count()
FROM data
WHERE (a = 1) AND (b = 0) AND (c = 0)
SETTINGS optimize_move_to_prewhere = 0

Query id: 18acb99f-3636-4e17-8778-d345ec81f341

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.091 sec. Processed 100.00 million rows, 2.40 GB (1.10 billion rows/s., 26.36 GB/s.)

p620.local :) select count() from data prewhere a = 1 and b = 0 and c = 0

SELECT count()
FROM data
PREWHERE (a = 1) AND (b = 0) AND (c = 0)

Query id: fb7950c0-92ca-4a6b-98c1-277c478ed9cb

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.200 sec. Processed 100.00 million rows, 2.40 GB (501.22 million rows/s., 12.03 GB/s.)

p620.local :) select count() from data prewhere a = 1 and b = 0 and c = 0 settings merge_tree_min_rows_for_seek=10e6, enable_multiple_prewhere_read_steps=1

SELECT count()
FROM data
PREWHERE (a = 1) AND (b = 0) AND (c = 0)
SETTINGS merge_tree_min_rows_for_seek = 10000000., enable_multiple_prewhere_read_steps = 1

Query id: 444e9360-360f-4669-8881-06de4387c742

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.252 sec. Processed 100.00 million rows, 800.00 MB (396.17 million rows/s., 3.17 GB/s.)

It looks like there is something suboptimal, because in one thread the difference is not that big:

p620.local :) select count() from data prewhere a = 1 and b = 0 and c = 0 settings enable_multiple_prewhere_read_steps=1, max_threads=1

SELECT count()
FROM data
PREWHERE (a = 1) AND (b = 0) AND (c = 0)
SETTINGS enable_multiple_prewhere_read_steps = 1, max_threads = 1

Query id: 6a6d1ff0-dee5-4444-871e-b16212dcdef7

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.249 sec. Processed 100.00 million rows, 800.00 MB (402.10 million rows/s., 3.22 GB/s.)

p620.local :) select count() from data where a = 1 and b = 0 and c = 0 settings optimize_move_to_prewhere=0, max_threads=1

SELECT count()
FROM data
WHERE (a = 1) AND (b = 0) AND (c = 0)
SETTINGS optimize_move_to_prewhere = 0, max_threads = 1

Query id: 2e414046-c987-49dc-9203-3c1e507b7a71

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.282 sec. Processed 100.00 million rows, 2.40 GB (354.57 million rows/s., 8.51 GB/s.)

@azat
Copy link
Member

azat commented Jul 17, 2023

There are some numbers in the original PRs description and comment: #46140

This query I guess filters out a lot of data, hence it give this benefit.

@davenger
Copy link
Member Author

davenger commented Jul 17, 2023

@azat is the data in your tests on HDD?
I see the following timing on my laptop with SSD:

davenger-xps :) select count() from data where a = 1 and b = 0 and c = 0 settings optimize_move_to_prewhere=0

SELECT count()
FROM data
WHERE (a = 1) AND (b = 0) AND (c = 0)
SETTINGS optimize_move_to_prewhere = 0

Query id: 53725fbe-5075-47b3-867a-3ab3b5ee28be

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.108 sec. Processed 100.00 million rows, 2.40 GB (929.14 million rows/s., 22.30 GB/s.)



davenger-xps :) select count() from data where a = 1 and b = 0 and c = 0 settings optimize_move_to_prewhere=1, enable_multiple_prewhere_read_steps=0, move_all_conditions_to_prewhere=0

SELECT count()
FROM data
WHERE (a = 1) AND (b = 0) AND (c = 0)
SETTINGS optimize_move_to_prewhere = 1, enable_multiple_prewhere_read_steps = 0, move_all_conditions_to_prewhere = 0

Query id: 08f80037-d8df-47c2-8f32-59744eb0ea54

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.033 sec. Processed 100.00 million rows, 800.00 MB (3.05 billion rows/s., 24.43 GB/s.)



davenger-xps :) select count() from data where a = 1 and b = 0 and c = 0 settings optimize_move_to_prewhere=1, enable_multiple_prewhere_read_steps=1, move_all_conditions_to_prewhere=1

SELECT count()
FROM data
WHERE (a = 1) AND (b = 0) AND (c = 0)
SETTINGS optimize_move_to_prewhere = 1, enable_multiple_prewhere_read_steps = 1, move_all_conditions_to_prewhere = 1

Query id: 01b2b94d-1aa7-4316-945d-f66e20955306

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.025 sec. Processed 100.00 million rows, 800.00 MB (3.93 billion rows/s., 31.40 GB/s.)



davenger-xps :) select count() from data where a = 1 and b = 0 and c = 0 settings optimize_move_to_prewhere=1, enable_multiple_prewhere_read_steps=1, move_all_conditions_to_prewhere=1, merge_tree_min_rows_for_seek = 10000000.

SELECT count()
FROM data
WHERE (a = 1) AND (b = 0) AND (c = 0)
SETTINGS optimize_move_to_prewhere = 1, enable_multiple_prewhere_read_steps = 1, move_all_conditions_to_prewhere = 1, merge_tree_min_rows_for_seek = 10000000.

Query id: 5d11a8f0-8815-4531-adc6-fa6b82aadf8d

┌─count()─┐
│       0 │
└─────────┘

1 row in set. Elapsed: 0.026 sec. Processed 100.00 million rows, 800.00 MB (3.85 billion rows/s., 30.79 GB/s.)

@azat
Copy link
Member

azat commented Jul 18, 2023

@azat is the data in your tests on HDD?

SSD, I would say that it was even memory, because the data was in page cache.

@davenger
Copy link
Member Author

@azat is the data in your tests on HDD?

SSD, I would say that it was even memory, because the data was in page cache.

@azat could you please check that log_level is not 'test'?
This might explain such a big difference between your result and what I see.

@azat
Copy link
Member

azat commented Jul 18, 2023

Funny enough - indeed it is!
Yep, now everything looks OK (tested on HDD as well)

And I guess my initial concerns about random IO were not wealthy, since you will not read more data anyway, plus there is still compressed blocks that will be read at once and will not be read twice as well due to caching in the code.

@davenger
Copy link
Member Author

The results of ClickBench with settings off and on: https://pastila.nl/?000b1ba6/95bfe2f2029917388977bc956a6ddf6c.html
The difference of 1.5x is seen in Q22, the rest are pretty much the same

@davenger davenger merged commit 06c1e68 into master Jul 21, 2023
@davenger davenger deleted the split_prewhere_dag_test_enable branch July 21, 2023 06:13
@alexey-milovidov
Copy link
Member

Wow! 1.5x improvement on one of the queries!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-performance Pull request with some performance improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants