mirror of
https://github.com/postgres/postgres.git
synced 2025-07-12 21:01:52 +03:00
Optimize joins when the inner relation can be proven unique.
If there can certainly be no more than one matching inner row for a given outer row, then the executor can move on to the next outer row as soon as it's found one match; there's no need to continue scanning the inner relation for this outer row. This saves useless scanning in nestloop and hash joins. In merge joins, it offers the opportunity to skip mark/restore processing, because we know we have not advanced past the first possible match for the next outer row. Of course, the devil is in the details: the proof of uniqueness must depend only on joinquals (not otherquals), and if we want to skip mergejoin mark/restore then it must depend only on merge clauses. To avoid adding more planning overhead than absolutely necessary, the present patch errs in the conservative direction: there are cases where inner_unique or skip_mark_restore processing could be used, but it will not do so because it's not sure that the uniqueness proof depended only on "safe" clauses. This could be improved later. David Rowley, reviewed and rather heavily editorialized on by me Discussion: https://postgr.es/m/CAApHDvqF6Sw-TK98bW48TdtFJ+3a7D2mFyZ7++=D-RyPsL76gw@mail.gmail.com
This commit is contained in:
@ -21,6 +21,7 @@
|
||||
#include "optimizer/cost.h"
|
||||
#include "optimizer/pathnode.h"
|
||||
#include "optimizer/paths.h"
|
||||
#include "optimizer/planmain.h"
|
||||
|
||||
/* Hook for plugins to get control in add_paths_to_joinrel() */
|
||||
set_join_pathlist_hook_type set_join_pathlist_hook = NULL;
|
||||
@ -120,6 +121,35 @@ add_paths_to_joinrel(PlannerInfo *root,
|
||||
extra.sjinfo = sjinfo;
|
||||
extra.param_source_rels = NULL;
|
||||
|
||||
/*
|
||||
* See if the inner relation is provably unique for this outer rel.
|
||||
*
|
||||
* We have some special cases: for JOIN_SEMI and JOIN_ANTI, it doesn't
|
||||
* matter since the executor can make the equivalent optimization anyway;
|
||||
* we need not expend planner cycles on proofs. For JOIN_UNIQUE_INNER, we
|
||||
* know we're going to force uniqueness of the innerrel below. For
|
||||
* JOIN_UNIQUE_OUTER, pass JOIN_INNER to avoid letting that value escape
|
||||
* this module.
|
||||
*/
|
||||
switch (jointype)
|
||||
{
|
||||
case JOIN_SEMI:
|
||||
case JOIN_ANTI:
|
||||
extra.inner_unique = false; /* well, unproven */
|
||||
break;
|
||||
case JOIN_UNIQUE_INNER:
|
||||
extra.inner_unique = true;
|
||||
break;
|
||||
case JOIN_UNIQUE_OUTER:
|
||||
extra.inner_unique = innerrel_is_unique(root, outerrel, innerrel,
|
||||
JOIN_INNER, restrictlist);
|
||||
break;
|
||||
default:
|
||||
extra.inner_unique = innerrel_is_unique(root, outerrel, innerrel,
|
||||
jointype, restrictlist);
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find potential mergejoin clauses. We can skip this if we are not
|
||||
* interested in doing a mergejoin. However, mergejoin may be our only
|
||||
@ -136,10 +166,10 @@ add_paths_to_joinrel(PlannerInfo *root,
|
||||
&mergejoin_allowed);
|
||||
|
||||
/*
|
||||
* If it's SEMI or ANTI join, compute correction factors for cost
|
||||
* estimation. These will be the same for all paths.
|
||||
* If it's SEMI, ANTI, or inner_unique join, compute correction factors
|
||||
* for cost estimation. These will be the same for all paths.
|
||||
*/
|
||||
if (jointype == JOIN_SEMI || jointype == JOIN_ANTI)
|
||||
if (jointype == JOIN_SEMI || jointype == JOIN_ANTI || extra.inner_unique)
|
||||
compute_semi_anti_join_factors(root, outerrel, innerrel,
|
||||
jointype, sjinfo, restrictlist,
|
||||
&extra.semifactors);
|
||||
@ -336,8 +366,7 @@ try_nestloop_path(PlannerInfo *root,
|
||||
* methodology worthwhile.
|
||||
*/
|
||||
initial_cost_nestloop(root, &workspace, jointype,
|
||||
outer_path, inner_path,
|
||||
extra->sjinfo, &extra->semifactors);
|
||||
outer_path, inner_path, extra);
|
||||
|
||||
if (add_path_precheck(joinrel,
|
||||
workspace.startup_cost, workspace.total_cost,
|
||||
@ -348,8 +377,7 @@ try_nestloop_path(PlannerInfo *root,
|
||||
joinrel,
|
||||
jointype,
|
||||
&workspace,
|
||||
extra->sjinfo,
|
||||
&extra->semifactors,
|
||||
extra,
|
||||
outer_path,
|
||||
inner_path,
|
||||
extra->restrictlist,
|
||||
@ -399,8 +427,7 @@ try_partial_nestloop_path(PlannerInfo *root,
|
||||
* cost. Bail out right away if it looks terrible.
|
||||
*/
|
||||
initial_cost_nestloop(root, &workspace, jointype,
|
||||
outer_path, inner_path,
|
||||
extra->sjinfo, &extra->semifactors);
|
||||
outer_path, inner_path, extra);
|
||||
if (!add_partial_path_precheck(joinrel, workspace.total_cost, pathkeys))
|
||||
return;
|
||||
|
||||
@ -410,8 +437,7 @@ try_partial_nestloop_path(PlannerInfo *root,
|
||||
joinrel,
|
||||
jointype,
|
||||
&workspace,
|
||||
extra->sjinfo,
|
||||
&extra->semifactors,
|
||||
extra,
|
||||
outer_path,
|
||||
inner_path,
|
||||
extra->restrictlist,
|
||||
@ -486,7 +512,7 @@ try_mergejoin_path(PlannerInfo *root,
|
||||
initial_cost_mergejoin(root, &workspace, jointype, mergeclauses,
|
||||
outer_path, inner_path,
|
||||
outersortkeys, innersortkeys,
|
||||
extra->sjinfo);
|
||||
extra);
|
||||
|
||||
if (add_path_precheck(joinrel,
|
||||
workspace.startup_cost, workspace.total_cost,
|
||||
@ -497,7 +523,7 @@ try_mergejoin_path(PlannerInfo *root,
|
||||
joinrel,
|
||||
jointype,
|
||||
&workspace,
|
||||
extra->sjinfo,
|
||||
extra,
|
||||
outer_path,
|
||||
inner_path,
|
||||
extra->restrictlist,
|
||||
@ -562,7 +588,7 @@ try_partial_mergejoin_path(PlannerInfo *root,
|
||||
initial_cost_mergejoin(root, &workspace, jointype, mergeclauses,
|
||||
outer_path, inner_path,
|
||||
outersortkeys, innersortkeys,
|
||||
extra->sjinfo);
|
||||
extra);
|
||||
|
||||
if (!add_partial_path_precheck(joinrel, workspace.total_cost, pathkeys))
|
||||
return;
|
||||
@ -573,7 +599,7 @@ try_partial_mergejoin_path(PlannerInfo *root,
|
||||
joinrel,
|
||||
jointype,
|
||||
&workspace,
|
||||
extra->sjinfo,
|
||||
extra,
|
||||
outer_path,
|
||||
inner_path,
|
||||
extra->restrictlist,
|
||||
@ -620,8 +646,7 @@ try_hashjoin_path(PlannerInfo *root,
|
||||
* never have any output pathkeys, per comments in create_hashjoin_path.
|
||||
*/
|
||||
initial_cost_hashjoin(root, &workspace, jointype, hashclauses,
|
||||
outer_path, inner_path,
|
||||
extra->sjinfo, &extra->semifactors);
|
||||
outer_path, inner_path, extra);
|
||||
|
||||
if (add_path_precheck(joinrel,
|
||||
workspace.startup_cost, workspace.total_cost,
|
||||
@ -632,8 +657,7 @@ try_hashjoin_path(PlannerInfo *root,
|
||||
joinrel,
|
||||
jointype,
|
||||
&workspace,
|
||||
extra->sjinfo,
|
||||
&extra->semifactors,
|
||||
extra,
|
||||
outer_path,
|
||||
inner_path,
|
||||
extra->restrictlist,
|
||||
@ -683,8 +707,7 @@ try_partial_hashjoin_path(PlannerInfo *root,
|
||||
* cost. Bail out right away if it looks terrible.
|
||||
*/
|
||||
initial_cost_hashjoin(root, &workspace, jointype, hashclauses,
|
||||
outer_path, inner_path,
|
||||
extra->sjinfo, &extra->semifactors);
|
||||
outer_path, inner_path, extra);
|
||||
if (!add_partial_path_precheck(joinrel, workspace.total_cost, NIL))
|
||||
return;
|
||||
|
||||
@ -694,8 +717,7 @@ try_partial_hashjoin_path(PlannerInfo *root,
|
||||
joinrel,
|
||||
jointype,
|
||||
&workspace,
|
||||
extra->sjinfo,
|
||||
&extra->semifactors,
|
||||
extra,
|
||||
outer_path,
|
||||
inner_path,
|
||||
extra->restrictlist,
|
||||
|
Reference in New Issue
Block a user