diff --git a/src/Interpreters/HashJoin.cpp b/src/Interpreters/HashJoin.cpp index 68bde0d3edb..c9843dca825 100644 --- a/src/Interpreters/HashJoin.cpp +++ b/src/Interpreters/HashJoin.cpp @@ -79,7 +79,13 @@ namespace JoinStuff { assert(flags[nullptr].size() <= size); need_flags = true; - flags[nullptr] = std::vector(size); + // For one disjunct clause case, we don't need to reinit each time we call addJoinedBlock. + // and there is no value inserted in this JoinUsedFlags before addJoinedBlock finish. + // So we reinit only when the hash table is rehashed to a larger size. + if (flags.empty() || flags[nullptr].size() < size) [[unlikely]] + { + flags[nullptr] = std::vector(size); + } } } diff --git a/tests/performance/join_used_flags.xml b/tests/performance/join_used_flags.xml new file mode 100644 index 00000000000..cd2073ee106 --- /dev/null +++ b/tests/performance/join_used_flags.xml @@ -0,0 +1,6 @@ + + CREATE TABLE test_join_used_flags (i64 Int64, i32 Int32) ENGINE = Memory + INSERT INTO test_join_used_flags SELECT number AS i64, rand32() AS i32 FROM numbers(20000000) + SELECT l.i64, r.i64, l.i32, r.i32 FROM test_join_used_flags l RIGHT JOIN test_join_used_flags r USING i64 format Null + DROP TABLE IF EXISTS test_join_used_flags +