Commit 11846111 authored by Tom Lane's avatar Tom Lane

Performance fix for new anti-join code in nodeMergejoin.c: after finding a

match in antijoin mode, we should advance to next outer tuple not next inner.
We know we don't want to return this outer tuple, and there is no point in
advancing over matching inner tuples now, because we'd just have to do it
again if the next outer tuple has the same merge key.  This makes a noticeable
difference if there are lots of duplicate keys in both inputs.

Similarly, after finding a match in semijoin mode, arrange to advance to
the next outer tuple after returning the current match; or immediately,
if it fails the extra quals.  The rationale is the same.  (This is a
performance bug in existing releases; perhaps worth back-patching?  The
planner tries to avoid using mergejoin with lots of duplicates, so it may
not be a big issue in practice.)

Nestloop and hash got this right to start with, but I made some cosmetic
adjustments there to make the corresponding bits of logic look more similar.
parent 5b8eb2b4
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/nodeHashjoin.c,v 1.94 2008/08/14 18:47:58 tgl Exp $
* $PostgreSQL: pgsql/src/backend/executor/nodeHashjoin.c,v 1.95 2008/08/15 19:20:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -264,8 +264,7 @@ ExecHashJoin(HashJoinState *node)
node->hj_NeedNewOuter = true;
break; /* out of loop over hash bucket */
}
else
{
/*
* In a semijoin, we'll consider returning the first match,
* but after that we're done with this outer tuple.
......@@ -295,7 +294,6 @@ ExecHashJoin(HashJoinState *node)
break; /* out of loop over hash bucket */
}
}
}
/*
* Now the current outer tuple has run out of matches, so check
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/nodeMergejoin.c,v 1.92 2008/08/14 18:47:58 tgl Exp $
* $PostgreSQL: pgsql/src/backend/executor/nodeMergejoin.c,v 1.93 2008/08/15 19:20:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -757,15 +757,9 @@ ExecMergeJoin(MergeJoinState *node)
innerTupleSlot = node->mj_InnerTupleSlot;
econtext->ecxt_innertuple = innerTupleSlot;
if (node->js.jointype == JOIN_SEMI &&
node->mj_MatchedOuter)
qualResult = false;
else
{
qualResult = (joinqual == NIL ||
ExecQual(joinqual, econtext, false));
MJ_DEBUG_QUAL(joinqual, qualResult);
}
if (qualResult)
{
......@@ -774,7 +768,17 @@ ExecMergeJoin(MergeJoinState *node)
/* In an antijoin, we never return a matched tuple */
if (node->js.jointype == JOIN_ANTI)
{
node->mj_JoinState = EXEC_MJ_NEXTOUTER;
break;
}
/*
* In a semijoin, we'll consider returning the first match,
* but after that we're done with this outer tuple.
*/
if (node->js.jointype == JOIN_SEMI)
node->mj_JoinState = EXEC_MJ_NEXTOUTER;
qualResult = (otherqual == NIL ||
ExecQual(otherqual, econtext, false));
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/nodeNestloop.c,v 1.47 2008/08/14 18:47:58 tgl Exp $
* $PostgreSQL: pgsql/src/backend/executor/nodeNestloop.c,v 1.48 2008/08/15 19:20:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -226,21 +226,23 @@ ExecNestLoop(NestLoopState *node)
/* In an antijoin, we never return a matched tuple */
if (node->js.jointype == JOIN_ANTI)
node->nl_NeedNewOuter = true;
else
{
node->nl_NeedNewOuter = true;
continue; /* return to top of loop */
}
/*
* In a semijoin, we'll consider returning the first match,
* but after that we're done with this outer tuple.
*/
if (node->js.jointype == JOIN_SEMI)
node->nl_NeedNewOuter = true;
if (otherqual == NIL || ExecQual(otherqual, econtext, false))
{
/*
* qualification was satisfied so we project and return
* the slot containing the result tuple using
* ExecProject().
* qualification was satisfied so we project and return the
* slot containing the result tuple using ExecProject().
*/
TupleTableSlot *result;
ExprDoneCond isDone;
......@@ -257,7 +259,6 @@ ExecNestLoop(NestLoopState *node)
}
}
}
}
/*
* Tuple fails qual, so free per-tuple memory and try again.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment