Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8bbfdc6

Browse files
committed
Consider ndistinct on the first column in cost_sort().
1 parenta911013 commit8bbfdc6

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

‎src/backend/optimizer/path/costsize.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,7 @@ cost_recursive_union(Path *runion, Path *nrterm, Path *rterm)
18631863
*/
18641864
staticvoid
18651865
cost_tuplesort(Cost*startup_cost,Cost*run_cost,
1866-
doubletuples,intwidth,intcmpMultiplier,
1866+
doubletuples,intwidth,doublecmpMultiplier,
18671867
Costcomparison_cost,intsort_mem,
18681868
doublelimit_tuples)
18691869
{
@@ -2132,8 +2132,34 @@ cost_sort(Path *path, PlannerInfo *root,
21322132
{
21332133
Coststartup_cost;
21342134
Costrun_cost;
2135-
intcmpMultiplier=
2136-
(pathkeys==NIL) ?2.0 :list_length(pathkeys)+1.0;
2135+
intn=list_length(pathkeys);
2136+
doublecmpMultiplier= (n==0) ?2.0 :n+1;
2137+
2138+
if (root!=NULL&&tuples>1&&n>1)
2139+
{
2140+
PathKey*key=linitial_node(PathKey,pathkeys);
2141+
boolisdefault;
2142+
VariableStatDatavardata;
2143+
doublend=-1;
2144+
Bitmapset*relids=pull_varnos(root, (Node*)key->source_expr);
2145+
2146+
if (!bms_is_member(0,relids))
2147+
{
2148+
examine_variable(root, (Node*)key->source_expr,0,&vardata);
2149+
if (HeapTupleIsValid(vardata.statsTuple))
2150+
{
2151+
nd=get_variable_numdistinct(&vardata,&isdefault);
2152+
2153+
if (!isdefault)
2154+
{
2155+
if (tuples >=nd)
2156+
cmpMultiplier=
2157+
2.0+ ((tuples-nd) / (tuples-1))* (n-1);
2158+
}
2159+
}
2160+
ReleaseVariableStats(vardata);
2161+
}
2162+
}
21372163

21382164
cost_tuplesort(&startup_cost,&run_cost,
21392165
tuples,width,cmpMultiplier,

‎src/test/regress/expected/aggregates.out

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,19 +2842,18 @@ SELECT count(*)
28422842
QUERY PLAN
28432843
-------------------------------------------------------------------------------
28442844
GroupAggregate
2845-
Group Key: t1.z, t1.w, t1.x, t1.y
2846-
-> Incremental Sort
2847-
Sort Key: t1.z, t1.w, t1.x, t1.y
2848-
Presorted Key: t1.z, t1.w, t1.x
2845+
Group Key: t1.x, t1.y, t1.z, t1.w
2846+
-> Sort
2847+
Sort Key: t1.x, t1.y, t1.z, t1.w
28492848
-> Merge Join
2850-
Merge Cond: ((t1.z = t2.z) AND (t1.w = t2.w) AND (t1.x = t2.x))
2849+
Merge Cond: ((t1.w = t2.w) AND (t1.z = t2.z) AND (t1.x = t2.x))
28512850
-> Sort
2852-
Sort Key: t1.z, t1.w, t1.x
2851+
Sort Key: t1.w, t1.z, t1.x
28532852
-> Index Scan using btg_x_y_idx on btg t1
28542853
-> Sort
2855-
Sort Key: t2.z, t2.w, t2.x
2854+
Sort Key: t2.w, t2.z, t2.x
28562855
-> Index Scan using btg_x_y_idx on btg t2
2857-
(13 rows)
2856+
(12 rows)
28582857

28592858
RESET enable_nestloop;
28602859
RESET enable_hashjoin;
@@ -2878,12 +2877,11 @@ SELECT count(*) FROM btg GROUP BY w, x, y, z ORDER BY x*x, z;
28782877
Sort
28792878
Sort Key: ((x * x)), z
28802879
-> GroupAggregate
2881-
Group Key: x, y, w, z
2882-
-> Incremental Sort
2883-
Sort Key: x, y, w, z
2884-
Presorted Key: x, y
2880+
Group Key: w, x, y, z
2881+
-> Sort
2882+
Sort Key: w, x, y, z
28852883
-> Index Scan using btg_x_y_idx on btg
2886-
(8 rows)
2884+
(7 rows)
28872885

28882886
-- Test the case where the number of incoming subtree path keys is more than
28892887
-- the number of grouping keys.

‎src/test/regress/expected/partition_join.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,13 +641,13 @@ SELECT a, b FROM prt1 FULL JOIN prt2 p2(b,a,c) USING(a,b)
641641
-> Sort
642642
Sort Key: (COALESCE(prt1_2.a, p2_2.a)), (COALESCE(prt1_2.b, p2_2.b))
643643
-> Merge Full Join
644-
Merge Cond: ((prt1_2.a = p2_2.a) AND (prt1_2.b = p2_2.b))
644+
Merge Cond: ((prt1_2.b = p2_2.b) AND (prt1_2.a = p2_2.a))
645645
Filter: ((COALESCE(prt1_2.a, p2_2.a) >= 490) AND (COALESCE(prt1_2.a, p2_2.a) <= 510))
646646
-> Sort
647-
Sort Key: prt1_2.a, prt1_2.b
647+
Sort Key: prt1_2.b, prt1_2.a
648648
-> Seq Scan on prt1_p3 prt1_2
649649
-> Sort
650-
Sort Key: p2_2.a, p2_2.b
650+
Sort Key: p2_2.b, p2_2.a
651651
-> Seq Scan on prt2_p3 p2_2
652652
(43 rows)
653653

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp