Commit 7a36c15
Chaithra Gopalareddy
Bug #18194196: OPTIMIZER EXECUTES STATEMENT INPERFORMANT
Problem:
While choosing the join order, the cost for doing table_scan
is wrongly calculated. As a result table_scan is preferred
over eq_ref, thereby choosing a bad plan.
Analysis:
While calculating the fanout in semijoin_dupsweedout method,
if an inner table is ahead of the outer table in the join order,
fanout is not calculated correctly. This is what is happening
w.r.t the query in the bugpage.
As seen from the trace, a table scan is preferred over eq_ref.
This is because when calculating the cost for eq_ref, optimizer
takes into consideration the correct prefix_row_count calculated
in prev_records_read. In this case the first table emailstorerel
has around 42000 records and the next one is a ref scan with 1 row as
fanout and the next one is also 1. Cost for eq_ref is calculated
based on this.
While in table_scan, cost is calculated based on row_count
passed to best_access_path which ideally should be the prefix_row_count
based on the partial plan chosen. In this case it should be more than
42000. It is currently 1. This changes to "1" after semi-join strategy
for dups_weedout is checked after choosing emailstore as the second table
in the following part of trace.
"plan_prefix": [
"`emailstoreorel`"
],
"table": "`emailstore`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "ref",
"index": "PRIMARY",
"rows": 1,
"cost": 42092,
"chosen": true
},
{
"access_type": "scan",
"using_join_cache": true,
"rows": 70554,
"cost": 5.94e8,
"chosen": false
}
]
},
"cost_for_plan": 67583,
"rows_for_plan": 42092,
"semijoin_strategy_choice": [
{
"strategy": "DuplicatesWeedout",
"cost": 76004,
"rows": 1,
"duplicate_tables_left": true,
"chosen": true
}
],
So after Optimize_table_order::semijoin_dupsweedout_access_paths(), the
current_rowcount now becomes "1" because the prefix_row_count is
calculated solely on outer fanout (from table emailstore) which in this case
is 1. The inner fanout from table emailstoreorel is not considered at all.
Solution:
Change the current formulas to the one's mentioned in the todo text.
The formulas now take into consideration all the scenarios which can have inner
tables ahead of outer tables in a join order. In such a scenario, if
inner_fanout is more than 1, this will be moved to outer_fanout and inner_fanout
is re-calculated.
max_outer_fanout is introduced to keep a cap on outer_fanout not to exceed the
cardinality of the cross product of outer tables.
changes to test files:
Two sets of changes can be noted.
1. When a inner table with full table scan is chosen as the first table in the join
order, earlier fanout was wrongly calculated. As a result the cost for doing
dups weedout was less. With the current formulas, the cost for doing writes become
more because of increased outer fanout in these cases. As a result the cost is not less
than materialized scan.
2. Optimizer now calculates the cost of table scan correctly. As a result, eq_ref
is preferred over table scan in these cases.1 parent d9d2140 commit 7a36c15
File tree
31 files changed
+2527
-880
lines changed- mysql-test
- include
- r
- sql
31 files changed
+2527
-880
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6522 | 6522 | | |
6523 | 6523 | | |
6524 | 6524 | | |
| 6525 | + | |
| 6526 | + | |
| 6527 | + | |
| 6528 | + | |
| 6529 | + | |
| 6530 | + | |
| 6531 | + | |
| 6532 | + | |
| 6533 | + | |
| 6534 | + | |
| 6535 | + | |
| 6536 | + | |
| 6537 | + | |
| 6538 | + | |
| 6539 | + | |
| 6540 | + | |
| 6541 | + | |
| 6542 | + | |
| 6543 | + | |
| 6544 | + | |
| 6545 | + | |
| 6546 | + | |
| 6547 | + | |
| 6548 | + | |
| 6549 | + | |
| 6550 | + | |
| 6551 | + | |
| 6552 | + | |
| 6553 | + | |
| 6554 | + | |
| 6555 | + | |
| 6556 | + | |
| 6557 | + | |
| 6558 | + | |
| 6559 | + | |
| 6560 | + | |
| 6561 | + | |
| 6562 | + | |
| 6563 | + | |
| 6564 | + | |
| 6565 | + | |
| 6566 | + | |
| 6567 | + | |
| 6568 | + | |
6525 | 6569 | | |
6526 | 6570 | | |
6527 | 6571 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2939 | 2939 | | |
2940 | 2940 | | |
2941 | 2941 | | |
2942 | | - | |
2943 | | - | |
2944 | | - | |
2945 | | - | |
2946 | | - | |
2947 | | - | |
2948 | | - | |
2949 | | - | |
2950 | | - | |
2951 | | - | |
2952 | | - | |
2953 | | - | |
2954 | | - | |
2955 | | - | |
2956 | | - | |
2957 | | - | |
2958 | | - | |
2959 | | - | |
2960 | | - | |
2961 | | - | |
2962 | | - | |
2963 | | - | |
2964 | | - | |
2965 | | - | |
2966 | | - | |
2967 | | - | |
2968 | | - | |
2969 | | - | |
| 2942 | + | |
| 2943 | + | |
| 2944 | + | |
| 2945 | + | |
| 2946 | + | |
| 2947 | + | |
| 2948 | + | |
| 2949 | + | |
| 2950 | + | |
| 2951 | + | |
| 2952 | + | |
| 2953 | + | |
| 2954 | + | |
| 2955 | + | |
| 2956 | + | |
2970 | 2957 | | |
2971 | 2958 | | |
2972 | | - | |
2973 | | - | |
| 2959 | + | |
| 2960 | + | |
| 2961 | + | |
| 2962 | + | |
| 2963 | + | |
| 2964 | + | |
| 2965 | + | |
| 2966 | + | |
| 2967 | + | |
| 2968 | + | |
| 2969 | + | |
| 2970 | + | |
| 2971 | + | |
| 2972 | + | |
| 2973 | + | |
| 2974 | + | |
| 2975 | + | |
| 2976 | + | |
| 2977 | + | |
| 2978 | + | |
| 2979 | + | |
2974 | 2980 | | |
2975 | 2981 | | |
2976 | 2982 | | |
2977 | | - | |
| 2983 | + | |
2978 | 2984 | | |
2979 | 2985 | | |
2980 | 2986 | | |
| |||
3227 | 3233 | | |
3228 | 3234 | | |
3229 | 3235 | | |
3230 | | - | |
3231 | | - | |
| 3236 | + | |
| 3237 | + | |
| 3238 | + | |
3232 | 3239 | | |
3233 | 3240 | | |
3234 | 3241 | | |
| |||
5746 | 5753 | | |
5747 | 5754 | | |
5748 | 5755 | | |
5749 | | - | |
5750 | | - | |
5751 | | - | |
| 5756 | + | |
| 5757 | + | |
| 5758 | + | |
| 5759 | + | |
5752 | 5760 | | |
5753 | | - | |
| 5761 | + | |
5754 | 5762 | | |
5755 | 5763 | | |
5756 | 5764 | | |
| |||
6868 | 6876 | | |
6869 | 6877 | | |
6870 | 6878 | | |
6871 | | - | |
6872 | | - | |
| 6879 | + | |
| 6880 | + | |
6873 | 6881 | | |
6874 | 6882 | | |
6875 | 6883 | | |
| |||
7641 | 7649 | | |
7642 | 7650 | | |
7643 | 7651 | | |
7644 | | - | |
7645 | | - | |
| 7652 | + | |
| 7653 | + | |
| 7654 | + | |
7646 | 7655 | | |
7647 | 7656 | | |
7648 | 7657 | | |
| |||
7714 | 7723 | | |
7715 | 7724 | | |
7716 | 7725 | | |
7717 | | - | |
7718 | | - | |
7719 | | - | |
| 7726 | + | |
| 7727 | + | |
| 7728 | + | |
| 7729 | + | |
7720 | 7730 | | |
7721 | 7731 | | |
7722 | 7732 | | |
| |||
10582 | 10592 | | |
10583 | 10593 | | |
10584 | 10594 | | |
| 10595 | + | |
| 10596 | + | |
| 10597 | + | |
| 10598 | + | |
| 10599 | + | |
| 10600 | + | |
| 10601 | + | |
| 10602 | + | |
| 10603 | + | |
| 10604 | + | |
| 10605 | + | |
| 10606 | + | |
| 10607 | + | |
| 10608 | + | |
| 10609 | + | |
| 10610 | + | |
| 10611 | + | |
| 10612 | + | |
| 10613 | + | |
| 10614 | + | |
| 10615 | + | |
| 10616 | + | |
| 10617 | + | |
| 10618 | + | |
| 10619 | + | |
| 10620 | + | |
| 10621 | + | |
| 10622 | + | |
| 10623 | + | |
| 10624 | + | |
| 10625 | + | |
| 10626 | + | |
| 10627 | + | |
| 10628 | + | |
| 10629 | + | |
| 10630 | + | |
| 10631 | + | |
| 10632 | + | |
| 10633 | + | |
| 10634 | + | |
| 10635 | + | |
| 10636 | + | |
| 10637 | + | |
| 10638 | + | |
| 10639 | + | |
| 10640 | + | |
| 10641 | + | |
| 10642 | + | |
| 10643 | + | |
| 10644 | + | |
| 10645 | + | |
| 10646 | + | |
| 10647 | + | |
| 10648 | + | |
| 10649 | + | |
| 10650 | + | |
| 10651 | + | |
| 10652 | + | |
| 10653 | + | |
| 10654 | + | |
| 10655 | + | |
| 10656 | + | |
10585 | 10657 | | |
10586 | 10658 | | |
10587 | 10659 | | |
0 commit comments