Repository navigation
Improve grace hash join performance by moving the smaller table to the right side - #72237
Conversation
|
The ci run failed, seems need the |
|
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
|
There was a problem hiding this comment.
| virtual bool rightTableCanbeReranged() const { return false; } | |
| virtual bool rightTableCanBeReranged() const { return false; } |
There was a problem hiding this comment.
| bool rightTableCanbeReranged() const override; | |
| bool rightTableCanBeReranged() const override; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
so if we anyway will do it after each call to addBlockToJoin, could we just pass check_limits=true?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
| 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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
ClickHouse/src/Processors/Transforms/AggregatingTransform.cpp
Lines 728 to 734 in ce82c19
There was a problem hiding this comment.
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?
ccca884 to
30545e6
Compare
There was a problem hiding this comment.
based on two methods below shouldn't we take hash_join_mutex and rehash_mutex?
There was a problem hiding this comment.
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.
42a27b8 to
9ca9f98
Compare

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 has100000rows.before this pr:
after this pr:
Changelog category (leave one):
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
CI Settings (Only check the boxes if you know what you are doing):