Skip to content

Commit 605d849

Browse files
committed
Clean up cost_sort some more: most callers were double-counting
the cost of reading the source data.
1 parent 87d95ca commit 605d849

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

src/backend/optimizer/path/costsize.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.35 1999/04/30 04:01:44 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.36 1999/05/01 19:47:41 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -167,28 +167,23 @@ cost_index(Oid indexid,
167167
/*
168168
* cost_sort
169169
* Determines and returns the cost of sorting a relation by considering
170-
* 1. the cost of doing an external sort: XXX this is probably too low
170+
* the cost of doing an external sort: XXX this is probably too low
171171
* disk = (p lg p)
172172
* cpu = *CPU-PAGE-WEIGHT* * (t lg t)
173-
* 2. the cost of reading the sort result into memory (another seqscan)
174-
* unless 'noread' is set
175173
*
176174
* 'pathkeys' is a list of sort keys
177175
* 'tuples' is the number of tuples in the relation
178176
* 'width' is the average tuple width in bytes
179-
* 'noread' is a flag indicating that the cost of reading the sort
180-
* source data should not be included (i.e., the caller
181-
* will account for it separately).
182177
*
183178
* NOTE: some callers currently pass NULL for pathkeys because they
184-
* can't conveniently supply sort keys. Since this routine doesn't
179+
* can't conveniently supply the sort keys. Since this routine doesn't
185180
* currently do anything with pathkeys anyway, that doesn't matter...
181+
* but if it ever does, it should react gracefully to lack of key data.
186182
*
187183
* Returns a flonum.
188-
*
189184
*/
190185
Cost
191-
cost_sort(List *pathkeys, int tuples, int width, bool noread)
186+
cost_sort(List *pathkeys, int tuples, int width)
192187
{
193188
Cost temp = 0;
194189
int npages = page_size(tuples, width);
@@ -200,6 +195,8 @@ cost_sort(List *pathkeys, int tuples, int width, bool noread)
200195
/* We want to be sure the cost of a sort is never estimated as zero,
201196
* even if passed-in tuple count is zero. Besides, mustn't do log(0)...
202197
*/
198+
if (tuples <= 0)
199+
tuples = 1;
203200
if (npages <= 0)
204201
npages = 1;
205202

@@ -210,13 +207,10 @@ cost_sort(List *pathkeys, int tuples, int width, bool noread)
210207
temp += npages * log_npages;
211208

212209
/*
213-
* could be base_log(pages, NBuffers), but we are only doing 2-way
210+
* could be base_log(tuples, NBuffers), but we are only doing 2-way
214211
* merges
215212
*/
216-
temp += _cpu_page_wight_ * tuples * log_npages;
217-
218-
if (!noread)
219-
temp += cost_seqscan(_NONAME_RELATION_ID_, npages, tuples);
213+
temp += _cpu_page_wight_ * tuples * base_log((double) tuples, 2.0);
220214

221215
Assert(temp > 0);
222216

@@ -282,7 +276,8 @@ cost_nestloop(Cost outercost,
282276
* 'outercost' and 'innercost' are the (disk+cpu) costs of scanning the
283277
* outer and inner relations
284278
* 'outersortkeys' and 'innersortkeys' are lists of the keys to be used
285-
* to sort the outer and inner relations
279+
* to sort the outer and inner relations (or NIL if no explicit
280+
* sort is needed because the source path is already ordered)
286281
* 'outertuples' and 'innertuples' are the number of tuples in the outer
287282
* and inner relations
288283
* 'outerwidth' and 'innerwidth' are the (typical) widths (in bytes)
@@ -309,9 +304,9 @@ cost_mergejoin(Cost outercost,
309304
temp += outercost;
310305
temp += innercost;
311306
if (outersortkeys) /* do we need to sort? */
312-
temp += cost_sort(outersortkeys, outersize, outerwidth, true);
307+
temp += cost_sort(outersortkeys, outersize, outerwidth);
313308
if (innersortkeys) /* do we need to sort? */
314-
temp += cost_sort(innersortkeys, innersize, innerwidth, true);
309+
temp += cost_sort(innersortkeys, innersize, innerwidth);
315310
temp += _cpu_page_wight_ * (outersize + innersize);
316311

317312
Assert(temp >= 0);

src/backend/optimizer/path/joinpath.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinpath.c,v 1.33 1999/04/03 00:18:28 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/joinpath.c,v 1.34 1999/05/01 19:47:42 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -348,7 +348,7 @@ match_unsorted_outer(RelOptInfo *joinrel,
348348
(mergeinnerpath->path_cost <
349349
(cheapest_inner->path_cost +
350350
cost_sort(matchedJoinKeys, innerrel->size,
351-
innerrel->width, false))));
351+
innerrel->width))));
352352
if (!path_is_cheaper_than_sort)
353353
{
354354
varkeys = make_pathkeys_from_joinkeys(matchedJoinKeys,
@@ -464,8 +464,7 @@ match_unsorted_inner(RelOptInfo *joinrel,
464464
if (clauses && matchedJoinKeys)
465465
{
466466
temp1 = outerrel->cheapestpath->path_cost +
467-
cost_sort(matchedJoinKeys, outerrel->size, outerrel->width,
468-
false);
467+
cost_sort(matchedJoinKeys, outerrel->size, outerrel->width);
469468

470469
temp2 = (bool) (FLOAT_IS_ZERO(innerpath->outerjoincost)
471470
|| (innerpath->outerjoincost > temp1));

src/backend/optimizer/plan/createplan.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.51 1999/04/30 04:04:27 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.52 1999/05/01 19:47:40 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1106,7 +1106,7 @@ make_sort(List *tlist, Oid nonameid, Plan *lefttree, int keycount)
11061106
Plan *plan = &node->plan;
11071107

11081108
copy_costsize(plan, lefttree);
1109-
plan->cost += cost_sort(NULL, plan->plan_size, plan->plan_width, true);
1109+
plan->cost += cost_sort(NULL, plan->plan_size, plan->plan_width);
11101110
plan->state = (EState *) NULL;
11111111
plan->targetlist = tlist;
11121112
plan->qual = NIL;

src/include/optimizer/cost.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: cost.h,v 1.17 1999/02/13 23:21:43 momjian Exp $
9+
* $Id: cost.h,v 1.18 1999/05/01 19:47:39 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -34,7 +34,7 @@ extern Cost cost_seqscan(int relid, int relpages, int reltuples);
3434
extern Cost cost_index(Oid indexid, int expected_indexpages, Cost selec,
3535
int relpages, int reltuples, int indexpages,
3636
int indextuples, bool is_injoin);
37-
extern Cost cost_sort(List *pathkeys, int tuples, int width, bool noread);
37+
extern Cost cost_sort(List *pathkeys, int tuples, int width);
3838
extern Cost cost_nestloop(Cost outercost, Cost innercost, int outertuples,
3939
int innertuples, int outerpages, bool is_indexjoin);
4040
extern Cost cost_mergejoin(Cost outercost, Cost innercost,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy