Skip to content

Commit 0c1aa58

Browse files
committed
Fix the cardinality estimation (FIRST ROWS case) in HASH JOINs
1 parent 08387c8 commit 0c1aa58

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/jrd/optimizer/InnerJoin.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,22 @@ void InnerJoin::estimateCost(unsigned position,
198198
// likely cardinality under-estimation.
199199
const bool avoidHashJoin = (streamCardinality <= MINIMUM_CARDINALITY && !stream->baseIndexes);
200200

201-
auto currentCardinality = candidate->unique ?
202-
MINIMUM_CARDINALITY : streamCardinality * candidate->selectivity;
203-
auto currentCost = candidate->cost;
201+
auto currentCardinality = streamCardinality * candidate->selectivity;
204202

205203
// Given the "first-rows" mode specified (or implied)
206204
// and unless an external sort is to be applied afterwards,
207205
// fake the expected cardinality to look as low as possible
208-
// to estimate the cost just for a single row being produced
206+
// to estimate the cost just for a single row being produced.
207+
// The same rule is used if the retrieval is unique.
209208

210-
if ((!sort || candidate->navigated) && optimizer->favorFirstRows())
209+
const bool firstRows = (optimizer->favorFirstRows() &&
210+
(!sortPtr || !*sortPtr || candidate->navigated));
211+
212+
if ((candidate->unique || firstRows) && currentCardinality > MINIMUM_CARDINALITY)
211213
currentCardinality = MINIMUM_CARDINALITY;
212214

213215
// Calculate the nested loop cost, it's our default option
214-
const auto loopCost = currentCost * cardinality;
216+
const auto loopCost = candidate->cost * cardinality;
215217
cost = loopCost;
216218

217219
// Consider whether the current stream can be hash-joined to the prior ones.

0 commit comments

Comments
 (0)