Skip to content

Use buffering while reading in order in queries with WHERE#64607

Merged
CurtizJ merged 17 commits intoClickHouse:masterfrom
CurtizJ:buffer-read-in-order
Jul 9, 2024
Merged

Use buffering while reading in order in queries with WHERE#64607
CurtizJ merged 17 commits intoClickHouse:masterfrom
CurtizJ:buffer-read-in-order

Conversation

@CurtizJ
Copy link
Copy Markdown
Member

@CurtizJ CurtizJ commented May 29, 2024

Changelog category (leave one):

  • Performance Improvement

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

Optimized queries with ORDER BY primary key and WHERE that have a condition with high selectivity by using of buffering. It is controlled by setting read_in_order_use_buffering (enabled by default) and can increase memory usage of query.

Resolves #40583
Resolves #40675
Resolves #11482
Resolves #17364

CI Settings (Only check the boxes if you know what you are doing):

  • Allow: All Required Checks
  • Allow: Stateless tests
  • Allow: Stateful tests
  • Allow: Integration Tests
  • Allow: Performance tests
  • Allow: All Builds
  • Allow: batch 1, 2 for multi-batch jobs
  • Allow: batch 3, 4, 5, 6 for multi-batch jobs

  • Exclude: Style check
  • Exclude: Fast test
  • Exclude: All with ASAN
  • Exclude: All with TSAN, MSAN, UBSAN, Coverage
  • Exclude: All with aarch64, release, debug

  • Do not test
  • Woolen Wolfdog
  • Upload binaries for special builds
  • Disable merge-commit
  • Disable CI cache

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-performance Pull request with some performance improvements label May 29, 2024
@robot-ch-test-poll3
Copy link
Copy Markdown
Contributor

robot-ch-test-poll3 commented May 29, 2024

This is an automated comment for commit a072cd2 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
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
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
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
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ 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. Integration 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
Stateful testsRuns stateful 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

@nickitat nickitat self-assigned this May 30, 2024
@CurtizJ
Copy link
Copy Markdown
Member Author

CurtizJ commented May 30, 2024

On example from #40583:

CREATE TABLE logs_time_dt
(
    `time` DateTime64(9) Codec(Delta, ZSTD(7)),
    `project` LowCardinality(String) CODEC(ZSTD(7)),
    `service` LowCardinality(String) CODEC(ZSTD(7)),
    `message` String CODEC(ZSTD(7)),
    `tags_hash` Array(UInt64) CODEC(ZSTD(7)),
    INDEX idx_message message TYPE ngrambf_v1(3, 512, 2, 0) GRANULARITY 3,
    INDEX idx_tags_hash tags_hash TYPE bloom_filter(0.01) GRANULARITY 1
)
ENGINE = MergeTree
PARTITION BY toStartOfHour(time)
ORDER BY (project, service, time)
SETTINGS index_granularity = 1024;

insert into logs_time_dt
(time, project, service, message, tags_hash)
select
fromUnixTimestamp64Nano(toInt64(toUnixTimestamp64Nano(toDateTime64('2022-08-01',9))+number/(2777)*1e9)),
'test' as project,
'test' as service,
'foo',
[ number % 3000 ]
from system.numbers
limit 60*1e6;

Before:

SELECT *
FROM logs_time_dt
WHERE (project = 'test') AND (service = 'test') AND has(tags_hash, 42)
ORDER BY time ASC
FORMAT `Null`

0 rows in set. Elapsed: 1.636 sec. Processed 29.26 million rows, 696.98 MB (17.88 million rows/s., 425.92 MB/s.)
Peak memory usage: 32.66 MiB.

After:

SELECT *
FROM logs_time_dt
WHERE (project = 'test') AND (service = 'test') AND has(tags_hash, 42)
ORDER BY time ASC
FORMAT `Null`;

0 rows in set. Elapsed: 0.205 sec. Processed 29.26 million rows, 696.98 MB (142.46 million rows/s., 3.39 GB/s.)
Peak memory usage: 30.39 MiB.

@CurtizJ CurtizJ force-pushed the buffer-read-in-order branch from 48d0bb3 to 6a8bd46 Compare May 31, 2024 15:54
@CurtizJ CurtizJ marked this pull request as ready for review June 7, 2024 14:42
@CurtizJ CurtizJ requested review from nickitat June 12, 2024 14:42
@CurtizJ
Copy link
Copy Markdown
Member Author

CurtizJ commented Jun 12, 2024

Perf tests:

Screenshot 2024-06-12 at 16 43 19 Screenshot 2024-06-12 at 16 43 28 Screenshot 2024-06-12 at 16 45 19

class BufferChunksTransform : public IProcessor
{
public:
BufferChunksTransform(const Block & header_, size_t max_bytes_to_buffer_, size_t limit_);
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.

it seems to me that if this optimisation will only be based on the number of bytes we will miss a lot of cases when rows are more or less wide. I think we always can buffer up to a full block for example. and that would be a good lower limit.
wdyt?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, probably it makes sense. However in current implementation we always buffer at least one chunk because we can exceed the threshold.

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.

I meant 65K rows regardless of their size in bytes, because afaiu we assume individual chunks to be much smaller in terms of number of rows

@CurtizJ CurtizJ force-pushed the buffer-read-in-order branch from 872a3f5 to c8be63a Compare June 17, 2024 19:38
Comment on lines +48 to +52
else if (input.isFinished())
{
output.finish();
return Status::Finished;
}
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.

I'm not sure if any problem could actually happen with the current implementation, but just for a piece of mind I'd move this if outside of the if (output.canPush()) condition just right on the l.28

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok, probably makes sense.

@CurtizJ CurtizJ force-pushed the buffer-read-in-order branch from f308b1b to 9c071fc Compare July 8, 2024 19:28
@CurtizJ
Copy link
Copy Markdown
Member Author

CurtizJ commented Jul 8, 2024

00504_mergetree_arrays_rw.sql: #66248

@CurtizJ
Copy link
Copy Markdown
Member Author

CurtizJ commented Jul 9, 2024

Perf tests: replaceRegexp_fallback - #66185 (comment)
test_zookeeper_config_load_balancing is unrelated.

@CurtizJ CurtizJ added this pull request to the merge queue Jul 9, 2024
Merged via the queue into ClickHouse:master with commit e29635d Jul 9, 2024
@CurtizJ CurtizJ deleted the buffer-read-in-order branch July 9, 2024 11:01
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 9, 2024
{

/// Transform that buffers chunks from the input
/// up to the certain limit and pushes chunks to
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.

Double whitespace.

@FrankChen021
Copy link
Copy Markdown
Contributor

Linked to this performance regression #66578 for anyone who care about this improvement.

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 pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

6 participants