Skip to content

Improve all join performance by append RowRefList or RowRef to AddedColumns for lazy output#63677

Merged
nickitat merged 2 commits intoClickHouse:masterfrom
KevinyhZou:improve_hash_join_by_reduce_vector_emplace
Aug 15, 2024
Merged

Improve all join performance by append RowRefList or RowRef to AddedColumns for lazy output#63677
nickitat merged 2 commits intoClickHouse:masterfrom
KevinyhZou:improve_hash_join_by_reduce_vector_emplace

Conversation

@KevinyhZou
Copy link
Copy Markdown
Contributor

@KevinyhZou KevinyhZou commented May 13, 2024

Changelog category (leave one):

  • Performance Improvement

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

Improve all join perfromance by append RowRefList or RowRef to AddedColumns for lazy output, while buildOutput, we use RowRefList/RowRef for output, and remove is_join_get condition from buildOutput for loop.

And we test as below, the left table test1((a Int64, b String, c LowCardinality(String), has 10000000 rows, the right table test2(a Int64, b String, c LowCardinality(String)) has 100000 rows, the test sql SELECT MAX(test1.a) FROM test1 INNER JOIN test2 on test1.b = test2.b SETTINGS max_threads=1

before this pr:

1 row in set. Elapsed: 2.024 sec. Processed 10.10 million rows, 130.88 MB (4.99 million rows/s., 64.66 MB/s.)
1 row in set. Elapsed: 2.146 sec. Processed 10.10 million rows, 130.88 MB (4.71 million rows/s., 60.98 MB/s.)
1 row in set. Elapsed: 2.091 sec. Processed 10.10 million rows, 130.88 MB (4.83 million rows/s., 62.60 MB/s.)

after this pr:

1 row in set. Elapsed: 1.347 sec. Processed 10.10 million rows, 130.88 MB (7.50 million rows/s., 97.14 MB/s.)
1 row in set. Elapsed: 1.356 sec. Processed 10.10 million rows, 130.88 MB (7.45 million rows/s., 96.50 MB/s.)
1 row in set. Elapsed: 1.451 sec. Processed 10.10 million rows, 130.88 MB (6.96 million rows/s., 90.22 MB/s.)

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

Modify your CI run

NOTE: If your merge the PR with modified CI you MUST KNOW what you are doing
NOTE: Checked options will be applied if set before CI RunConfig/PrepareRunConfig step

Include tests (required builds will be added automatically):

  • Fast test
  • Integration Tests
  • Stateless tests
  • Stateful tests
  • Unit tests
  • Performance tests
  • All with ASAN
  • All with TSAN
  • All with Analyzer
  • All with Azure
  • Add your option here

Exclude tests:

  • Fast test
  • Integration Tests
  • Stateless tests
  • Stateful tests
  • Performance tests
  • All with ASAN
  • All with TSAN
  • All with MSAN
  • All with UBSAN
  • All with Coverage
  • All with Aarch64
  • Add your option here

Extra options:

  • do not test (only style check)
  • disable merge-commit (no merge from master before tests)
  • disable CI cache (job reuse)

Only specified batches in multi-batch jobs:

  • 1
  • 2
  • 3
  • 4
Details

@KevinyhZou KevinyhZou changed the title Improve all join performance by append RowRefList to LazyOutput directly Improve all join performance by append RowRefList / RowRef to LazyOutput directly May 13, 2024
@KevinyhZou KevinyhZou changed the title Improve all join performance by append RowRefList / RowRef to LazyOutput directly Improve all join performance by append RowRefList or RowRef to LazyOutput directly May 13, 2024
@KevinyhZou KevinyhZou changed the title Improve all join performance by append RowRefList or RowRef to LazyOutput directly Improve all join performance by append RowRefList or RowRef to AddedColumns for lazy output May 13, 2024
@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label May 13, 2024
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-performance Pull request with some performance improvements label May 13, 2024
@robot-ch-test-poll2
Copy link
Copy Markdown
Contributor

robot-ch-test-poll2 commented May 13, 2024

This is an automated comment for commit 95e07ce 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
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
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
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
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ 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

@KevinyhZou KevinyhZou marked this pull request as draft May 14, 2024 04:14
@KevinyhZou KevinyhZou force-pushed the improve_hash_join_by_reduce_vector_emplace branch from 9dbaac4 to ec1886b Compare May 14, 2024 10:11
@KevinyhZou KevinyhZou marked this pull request as ready for review May 17, 2024 01:40
@KevinyhZou
Copy link
Copy Markdown
Contributor Author

KevinyhZou commented May 17, 2024

image

the performance improves in join_in_memory

@nickitat nickitat self-assigned this May 17, 2024
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 is not a good idea to increase RowRefList size. do we really need this field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, this is needed. it will be used to compute the current_offset in addFoundRowAll

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.

imo it would be better to avoid using func pointers (i.e. replace with template parameters) to avoid indirect call here

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.

why do we test only with max_threads=1?

@KevinyhZou KevinyhZou force-pushed the improve_hash_join_by_reduce_vector_emplace branch from 669f417 to b44ea2f Compare June 12, 2024 11:18
@nickitat
Copy link
Copy Markdown
Member

pls check reported slowdowns in storage_join_direct_join

@KevinyhZou
Copy link
Copy Markdown
Contributor Author

KevinyhZou commented Jun 14, 2024

yes, I see. Maybe it it same problem as join_append_block, has the cache miss problems in the buildOutput for loop.

@KevinyhZou
Copy link
Copy Markdown
Contributor Author

KevinyhZou commented Jun 17, 2024

It seems the reference in the for loop of buildOutputFromRowRefList cause storage_join_direct_join slowdown, and it is fixed now. But still has 9.7% performance slowdown in jon_append_block. @nickitat

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.

and why it is beneficial in the end? just because we filling in one array instead of two?

Copy link
Copy Markdown
Contributor Author

@KevinyhZou KevinyhZou Jun 18, 2024

Choose a reason for hiding this comment

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

I think it is because the fill array operation of addFoundRowAll is inside the joinRightColumns for loop, which will scan all the left table rows, and if the right table rows has a lots of matched rows per key, then the time complexity is very high. so I move the for loop of addFoundRowAll to the buildOutput to reduce the time complexity of joinRightColumns. and on another hand, the old code will fill 2 array, and now it fill one, and it can also improve the performance.

But like the case in join_append_block, every row in right table matched is single one, which will not reduce the time complexity, and it slowdown as the buildOutput's for loop cache miss of block. @nickitat

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.

indeed, it could be almost fixed by the following patch: https://pastila.nl/?0169ab09/eb769dfb95b000c681651866b8c89d86#VYcmBdzNznC3fy/9hHzw3Q==
afaiu the similar problem should exist for buildOutputFromRowRef as well, so it makes sense to unify their implementation and use this patch in both cases

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Contributor Author

@KevinyhZou KevinyhZou Jul 8, 2024

Choose a reason for hiding this comment

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

indeed, it could be almost fixed by the following patch: https://pastila.nl/?0169ab09/eb769dfb95b000c681651866b8c89d86#VYcmBdzNznC3fy/9hHzw3Q== afaiu the similar problem should exist for buildOutputFromRowRef as well, so it makes sense to unify their implementation and use this patch in both cases

This patch make test case hashjoin_with_large_output 10% slowdown. @nickitat , should we add a setting like a threshold, when the matched rows number exceed this threshold, then we use buildOutFromRowRefList, otherwise, output by a vector of blocks like in this patch?

@nickitat
Copy link
Copy Markdown
Member

overall looks ok for me, I'll check perf myself

@nickitat nickitat closed this Jun 18, 2024
@nickitat nickitat reopened this Jun 18, 2024
@pamarcos pamarcos mentioned this pull request Jun 21, 2024
1 task
@KevinyhZou KevinyhZou force-pushed the improve_hash_join_by_reduce_vector_emplace branch from e1992fc to 397deee Compare July 5, 2024 07:11
@KevinyhZou
Copy link
Copy Markdown
Contributor Author

KevinyhZou commented Jul 16, 2024

@nickitat I had add a settings join_output_by_rowlist_perkey_rows_threshold to control not to use output by rowref list if the average rows perkey in right table is below this threshold, and now it has no performance slowdown. could you help to review this?

Copy link
Copy Markdown
Member

@nickitat nickitat left a comment

Choose a reason for hiding this comment

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

otherwise lgtm

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.

pls leave a comment explaining why do we do this. it is really tricky place

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@KevinyhZou KevinyhZou force-pushed the improve_hash_join_by_reduce_vector_emplace branch from 5f874cc to 85bd63a Compare August 13, 2024 11:20
@KevinyhZou
Copy link
Copy Markdown
Contributor Author

could this pr be merged ? @nickitat

@nickitat nickitat added this pull request to the merge queue Aug 15, 2024
@nickitat
Copy link
Copy Markdown
Member

thanks for the contribution and your patience )

Merged via the queue into ClickHouse:master with commit 418c3fa Aug 15, 2024
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 15, 2024
\
M(Bool, join_use_nulls, false, "Use NULLs for non-joined rows of outer JOINs for types that can be inside Nullable. If false, use default value of corresponding columns data type.", IMPORTANT) \
\
M(Int32, join_output_by_rowlist_perkey_rows_threshold, 5, "The lower limit of per-key average rows in the right table to determine whether to output by row list in hash join.", 0) \
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.

The setting is used as unsigned int (size_t) but declared as Int32 here. It makes sense to make it unsigned (before 24.9)

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.

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-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants