42
42
* Portions Copyright (c) 1994, Regents of the University of California
43
43
*
44
44
* IDENTIFICATION
45
- * $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.81 2002/03/01 06:01:19 tgl Exp $
45
+ * $Header: /cvsroot/pgsql/src/backend/optimizer/path/costsize.c,v 1.82 2002/03/01 20:50:20 tgl Exp $
46
46
*
47
47
*-------------------------------------------------------------------------
48
48
*/
@@ -467,10 +467,11 @@ cost_sort(Path *path, Query *root,
467
467
}
468
468
469
469
/*
470
- * Note: should we bother to assign a nonzero run_cost to reflect the
471
- * overhead of extracting tuples from the sort result? Probably not
472
- * worth worrying about.
470
+ * Also charge a small amount (arbitrarily set equal to operator cost)
471
+ * per extracted tuple.
473
472
*/
473
+ run_cost += cpu_operator_cost * tuples ;
474
+
474
475
path -> startup_cost = startup_cost ;
475
476
path -> total_cost = startup_cost + run_cost ;
476
477
}
@@ -567,11 +568,12 @@ cost_mergejoin(Path *path, Query *root,
567
568
Cost run_cost = 0 ;
568
569
Cost cpu_per_tuple ;
569
570
RestrictInfo * firstclause ;
571
+ Var * leftvar ;
570
572
double outer_rows ,
571
573
inner_rows ;
572
574
double ntuples ;
573
- Selectivity leftscan ,
574
- rightscan ;
575
+ Selectivity outerscansel ,
576
+ innerscansel ;
575
577
Path sort_path ; /* dummy for result of cost_sort */
576
578
577
579
if (!enable_mergejoin )
@@ -592,11 +594,24 @@ cost_mergejoin(Path *path, Query *root,
592
594
mergejoinscansel (root , (Node * ) firstclause -> clause ,
593
595
& firstclause -> left_mergescansel ,
594
596
& firstclause -> right_mergescansel );
595
- leftscan = firstclause -> left_mergescansel ;
596
- rightscan = firstclause -> right_mergescansel ;
597
597
598
- outer_rows = outer_path -> parent -> rows * leftscan ;
599
- inner_rows = inner_path -> parent -> rows * rightscan ;
598
+ leftvar = get_leftop (firstclause -> clause );
599
+ Assert (IsA (leftvar , Var ));
600
+ if (intMember (leftvar -> varno , outer_path -> parent -> relids ))
601
+ {
602
+ /* left side of clause is outer */
603
+ outerscansel = firstclause -> left_mergescansel ;
604
+ innerscansel = firstclause -> right_mergescansel ;
605
+ }
606
+ else
607
+ {
608
+ /* left side of clause is inner */
609
+ outerscansel = firstclause -> right_mergescansel ;
610
+ innerscansel = firstclause -> left_mergescansel ;
611
+ }
612
+
613
+ outer_rows = outer_path -> parent -> rows * outerscansel ;
614
+ inner_rows = inner_path -> parent -> rows * innerscansel ;
600
615
601
616
/* cost of source data */
602
617
@@ -616,13 +631,13 @@ cost_mergejoin(Path *path, Query *root,
616
631
outer_path -> parent -> width );
617
632
startup_cost += sort_path .startup_cost ;
618
633
run_cost += (sort_path .total_cost - sort_path .startup_cost )
619
- * leftscan ;
634
+ * outerscansel ;
620
635
}
621
636
else
622
637
{
623
638
startup_cost += outer_path -> startup_cost ;
624
639
run_cost += (outer_path -> total_cost - outer_path -> startup_cost )
625
- * leftscan ;
640
+ * outerscansel ;
626
641
}
627
642
628
643
if (innersortkeys ) /* do we need to sort inner? */
@@ -635,13 +650,13 @@ cost_mergejoin(Path *path, Query *root,
635
650
inner_path -> parent -> width );
636
651
startup_cost += sort_path .startup_cost ;
637
652
run_cost += (sort_path .total_cost - sort_path .startup_cost )
638
- * rightscan ;
653
+ * innerscansel ;
639
654
}
640
655
else
641
656
{
642
657
startup_cost += inner_path -> startup_cost ;
643
658
run_cost += (inner_path -> total_cost - inner_path -> startup_cost )
644
- * rightscan ;
659
+ * innerscansel ;
645
660
}
646
661
647
662
/*
0 commit comments