Skip to content

Improve grace hash join performance by moving the smaller table to the right side - #72237

Merged
nickitat merged 1 commit into
ClickHouse:masterfrom
KevinyhZou:support_join_sort_grace_hash
Dec 18, 2024
Merged

nickitat merged 1 commit into
ClickHouse:masterfrom
KevinyhZou:support_join_sort_grace_hash

Conversation

@KevinyhZou

Copy link
Copy Markdown
Contributor

Improve join performance by rerange the right table by keys, and this has been applied to hash join at pr: #60341, here we make it to apply to grace hash join.

The performance test at local environment, query SELECT MAX(test1.a) FROM test INNER JOIN test1 on test.b = test1.b settings join_algorithm='grace_hash', right table has 100000 rows.

before this pr:

1 row in set. Elapsed: 0.471 sec. Processed 10.10 million rows, 130.88 MB (21.46 million rows/s., 278.15 MB/s.)

after this pr:

1 row in set. Elapsed: 0.360 sec. Processed 10.10 million rows, 130.88 MB (28.03 million rows/s., 363.21 MB/s.)

Changelog category (leave one):

  • Performance Improvement

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

Improve grace hash join performance by rerange the right join table by keys

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/

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

  • Run only fuzzers related jobs (libFuzzer fuzzers, AST fuzzers, etc.)
  • Exclude: AST fuzzers

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

@nickitat nickitat self-assigned this Nov 22, 2024
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-performance Pull request with some performance improvements label Nov 22, 2024
@KevinyhZou

Copy link
Copy Markdown
Contributor Author

The ci run failed, seems need the can be tested label, please add it. @nickitat

@nickitat nickitat added the can be tested Allows running workflows for external contributors label Nov 25, 2024
@robot-ch-test-poll1

robot-ch-test-poll1 commented Nov 25, 2024 •

Copy link
Copy Markdown
Contributor

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

Comment thread src/Interpreters/IJoin.h Outdated

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.

Suggested change
virtual bool rightTableCanbeReranged() const { return false; }
virtual bool rightTableCanBeReranged() const { return false; }

Comment thread src/Interpreters/GraceHashJoin.h Outdated

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.

Suggested change
bool rightTableCanbeReranged() const override;
bool rightTableCanBeReranged() const override;

Comment thread src/Interpreters/GraceHashJoin.cpp Outdated

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 is really confusing code, basically:

hash_join->getJoinedData()->keys_to_join = hash_join->getTotalRowCount();

do we really need to set keys_to_join here and if yes, could you pls refactor it somehow

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, the keys_to_join will not be calculated in HashJoin as we call hash_join->addBlockToJoin in GraceHashJoin, because the checkLimit is set to false. So we need to calculate it separately.

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.

so if we anyway will do it after each call to addBlockToJoin, could we just pass check_limits=true?

@KevinyhZou KevinyhZou Dec 5, 2024 •

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.

we can not set check_limits = true, because the limits check will be done by hasMemoryOverflow of GraceHashJoin, which is a soft check, if the check_limits = true, it will use a different check (not soft) by HashJoin

Comment thread src/Interpreters/GraceHashJoin.cpp Outdated
Comment on lines 766 to 775

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.

Suggested change
if (hash_join)
{
/// Do not rerange when the right table is big, and the buckets size is greater than 1.
if (buckets.size() > 1)
return false;
else
return hash_join->rightTableCanbeReranged();
}
else
return false;
if (hash_join && buckets.size() <= 1)
return hash_join->rightTableCanbeReranged();
return false;

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 we could avoid creating a dedicated processor just to call this method on finish. could we call it on input.isFinished() in FillingRightJoinSideTransform instead?

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.

if do it in FillingRightJoinSideTransform, it would encounter data race problems, as the grace hash join supportParallelJoin is true, means that FillingRightJoinSideTransform run parallelly, but we only need to rerange right table once, it seems that not suitable to do the rerange. So we build another processor transform, which is resized by 1, not run parallelly, and to rerange the right table.

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.

we have similar situation in aggregation for example, when we need to trigger merging only once after pre-aggregation phase is finished. it is solved by this questionable pattern

if (many_data->num_finished.fetch_add(1) + 1 < many_data->variants.size())
{
/// Note: we reset aggregation state here to release memory earlier.
/// It might cause extra memory usage for complex queries othervise.
many_data.reset();
return;
}

@KevinyhZou KevinyhZou Dec 5, 2024 •

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.

In FillingRightJoinSideTransform it can not detect whether others' input is finished, so we do not know when to rerange the table in FillingRightJoinSideTransform. And I simplify code by move the rerange table logic to joinBlock of GraceHashJoin, then the logic of RerangeRightJoinTableTransform can be removed, and make the table rerange only once by use a hash_join_mutex lock. have a review that whether this would be ok?

@KevinyhZou

KevinyhZou commented Nov 26, 2024 •

Copy link
Copy Markdown
Contributor Author

image

the performance improved of grace hash join

@KevinyhZou
KevinyhZou force-pushed the support_join_sort_grace_hash branch 2 times, most recently from ccca884 to 30545e6 Compare December 5, 2024 09:05
Comment thread src/Interpreters/GraceHashJoin.cpp Outdated

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.

based on two methods below shouldn't we take hash_join_mutex and rehash_mutex?

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.

take rehash_mutex by use getNumBuckets, and I think we should not take hash_join_mutex for call hash_join->rightTableCanBeRereanged(), as this method access the variants in hash_join with no data race.

@KevinyhZou
KevinyhZou force-pushed the support_join_sort_grace_hash branch from 42a27b8 to 9ca9f98 Compare December 11, 2024 03:29
@nickitat
nickitat added this pull request to the merge queue Dec 18, 2024
Merged via the queue into ClickHouse:master with commit c9ba744 Dec 18, 2024
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Dec 18, 2024
@rschu1ze rschu1ze changed the title Improve grace hash join performance by rerange the right join table by keys Improve grace hash join performance by moving the smaller table to the right side Jan 8, 2025
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.

4 participants