Skip to content

Commit 8c2faad

Browse files
committed
MDEV-28929: Plan selection takes forever with MDEV-28852 ...
Part #2: Extend heuristic pruning to use multiple tables as the "Model tables". Before the patch, heuristic pruning uses only one "Model table": The table which had the best cost AND record became the "Model table". After that, if a table's cost and record were both worse than those of the Model Table, the table would be pruned away. This didn't work well when the first table (the optimizer sorts them by record_count) had low record_count but relatively high cost: nothing could be pruned afterwards. The patch adds the two additional "Model tables": one with the least cost and the other with the least record_count. (In both cases, a table can be pruned away if BOTH its cost and record_count are worse than those of a Model table) The new pruning is active when the number of tables to consider for the prefix is higher than @@optimizer_extra_pruning_depth. One can see the new pruning in the Optimizer Trace as - "pruned_by_heuristic":"min_record_count", or - "pruned_by_heuristic":"min_read_time". Old heuristic pruning shows as "pruned_by_heuristic":1.
1 parent afadd58 commit 8c2faad

File tree

8 files changed

+112
-178
lines changed

8 files changed

+112
-178
lines changed

mysql-test/main/mysqld--help.result

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,10 @@ The following specify which files/extra groups are read (specified before remain
699699
max_connections*5 or max_connections + table_cache*2
700700
(whichever is larger) number of file descriptors
701701
(Automatically configured unless set explicitly)
702+
--optimizer-extra-pruning-depth=#
703+
If the optimizer needs to enumerate join prefix of this
704+
size or larger, then it will try agressively prune away
705+
the search space.
702706
--optimizer-max-sel-arg-weight=#
703707
The maximum weight of the SEL_ARG graph. Set to 0 for no
704708
limit
@@ -1662,6 +1666,7 @@ old-alter-table DEFAULT
16621666
old-mode UTF8_IS_UTF8MB3
16631667
old-passwords FALSE
16641668
old-style-user-limits FALSE
1669+
optimizer-extra-pruning-depth 8
16651670
optimizer-max-sel-arg-weight 32000
16661671
optimizer-prune-level 2
16671672
optimizer-search-depth 62

mysql-test/main/opt_trace.result

Lines changed: 2 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -6035,86 +6035,7 @@ t_outer_2.a in (select t_inner_3.a from t2 t_inner_3, t1 t_inner_4) {
60356035
"rows_for_plan": 729,
60366036
"cost_for_plan": 176.0410156,
60376037
"semijoin_strategy_choice": [],
6038-
"rest_of_plan": [
6039-
{
6040-
"plan_prefix": [
6041-
"t_outer_1",
6042-
"t_outer_2",
6043-
"t_inner_1",
6044-
"t_inner_3"
6045-
],
6046-
"get_costs_for_tables": [
6047-
{
6048-
"best_access_path": {
6049-
"table": "t_inner_4",
6050-
"considered_access_paths": [
6051-
{
6052-
"access_type": "scan",
6053-
"resulting_rows": 3,
6054-
"cost": 2.005126953,
6055-
"chosen": true
6056-
}
6057-
],
6058-
"chosen_access_method": {
6059-
"type": "scan",
6060-
"records": 3,
6061-
"cost": 2.005126953,
6062-
"uses_join_buffering": true
6063-
}
6064-
}
6065-
},
6066-
{
6067-
"best_access_path": {
6068-
"table": "t_inner_2",
6069-
"considered_access_paths": [
6070-
{
6071-
"access_type": "scan",
6072-
"resulting_rows": 9,
6073-
"cost": 2.015380859,
6074-
"chosen": true
6075-
}
6076-
],
6077-
"chosen_access_method": {
6078-
"type": "scan",
6079-
"records": 9,
6080-
"cost": 2.015380859,
6081-
"uses_join_buffering": true
6082-
}
6083-
}
6084-
}
6085-
]
6086-
},
6087-
{
6088-
"plan_prefix": [
6089-
"t_outer_1",
6090-
"t_outer_2",
6091-
"t_inner_1",
6092-
"t_inner_3"
6093-
],
6094-
"table": "t_inner_2",
6095-
"rows_for_plan": 6561,
6096-
"cost_for_plan": 1490.256396,
6097-
"semijoin_strategy_choice": [],
6098-
"pruned_by_cost": true,
6099-
"current_cost": 1490.256396,
6100-
"best_cost": 568.8615234
6101-
},
6102-
{
6103-
"plan_prefix": [
6104-
"t_outer_1",
6105-
"t_outer_2",
6106-
"t_inner_1",
6107-
"t_inner_3"
6108-
],
6109-
"table": "t_inner_4",
6110-
"rows_for_plan": 2187,
6111-
"cost_for_plan": 615.4461426,
6112-
"semijoin_strategy_choice": [],
6113-
"pruned_by_cost": true,
6114-
"current_cost": 615.4461426,
6115-
"best_cost": 568.8615234
6116-
}
6117-
]
6038+
"pruned_by_heuristic": "min_read_time"
61186039
}
61196040
]
61206041
},
@@ -6596,86 +6517,7 @@ t_outer_2.a in (select t_inner_3.a from t2 t_inner_3, t1 t_inner_4) {
65966517
"rows_for_plan": 729,
65976518
"cost_for_plan": 172.4410156,
65986519
"semijoin_strategy_choice": [],
6599-
"rest_of_plan": [
6600-
{
6601-
"plan_prefix": [
6602-
"t_outer_1",
6603-
"t_inner_1",
6604-
"t_outer_2",
6605-
"t_inner_3"
6606-
],
6607-
"get_costs_for_tables": [
6608-
{
6609-
"best_access_path": {
6610-
"table": "t_inner_4",
6611-
"considered_access_paths": [
6612-
{
6613-
"access_type": "scan",
6614-
"resulting_rows": 3,
6615-
"cost": 2.005126953,
6616-
"chosen": true
6617-
}
6618-
],
6619-
"chosen_access_method": {
6620-
"type": "scan",
6621-
"records": 3,
6622-
"cost": 2.005126953,
6623-
"uses_join_buffering": true
6624-
}
6625-
}
6626-
},
6627-
{
6628-
"best_access_path": {
6629-
"table": "t_inner_2",
6630-
"considered_access_paths": [
6631-
{
6632-
"access_type": "scan",
6633-
"resulting_rows": 9,
6634-
"cost": 2.015380859,
6635-
"chosen": true
6636-
}
6637-
],
6638-
"chosen_access_method": {
6639-
"type": "scan",
6640-
"records": 9,
6641-
"cost": 2.015380859,
6642-
"uses_join_buffering": true
6643-
}
6644-
}
6645-
}
6646-
]
6647-
},
6648-
{
6649-
"plan_prefix": [
6650-
"t_outer_1",
6651-
"t_inner_1",
6652-
"t_outer_2",
6653-
"t_inner_3"
6654-
],
6655-
"table": "t_inner_2",
6656-
"rows_for_plan": 6561,
6657-
"cost_for_plan": 1486.656396,
6658-
"semijoin_strategy_choice": [],
6659-
"pruned_by_cost": true,
6660-
"current_cost": 1486.656396,
6661-
"best_cost": 565.2615234
6662-
},
6663-
{
6664-
"plan_prefix": [
6665-
"t_outer_1",
6666-
"t_inner_1",
6667-
"t_outer_2",
6668-
"t_inner_3"
6669-
],
6670-
"table": "t_inner_4",
6671-
"rows_for_plan": 2187,
6672-
"cost_for_plan": 611.8461426,
6673-
"semijoin_strategy_choice": [],
6674-
"pruned_by_cost": true,
6675-
"current_cost": 611.8461426,
6676-
"best_cost": 565.2615234
6677-
}
6678-
]
6520+
"pruned_by_heuristic": "min_read_time"
66796521
}
66806522
]
66816523
},

mysql-test/suite/sys_vars/r/sysvars_server_embedded.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,16 @@ NUMERIC_BLOCK_SIZE 1
22522252
ENUM_VALUE_LIST NULL
22532253
READ_ONLY YES
22542254
COMMAND_LINE_ARGUMENT REQUIRED
2255+
VARIABLE_NAME OPTIMIZER_EXTRA_PRUNING_DEPTH
2256+
VARIABLE_SCOPE SESSION
2257+
VARIABLE_TYPE BIGINT UNSIGNED
2258+
VARIABLE_COMMENT If the optimizer needs to enumerate join prefix of this size or larger, then it will try agressively prune away the search space.
2259+
NUMERIC_MIN_VALUE 0
2260+
NUMERIC_MAX_VALUE 62
2261+
NUMERIC_BLOCK_SIZE 1
2262+
ENUM_VALUE_LIST NULL
2263+
READ_ONLY NO
2264+
COMMAND_LINE_ARGUMENT REQUIRED
22552265
VARIABLE_NAME OPTIMIZER_MAX_SEL_ARG_WEIGHT
22562266
VARIABLE_SCOPE SESSION
22572267
VARIABLE_TYPE BIGINT UNSIGNED

mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,16 @@ NUMERIC_BLOCK_SIZE 1
24222422
ENUM_VALUE_LIST NULL
24232423
READ_ONLY YES
24242424
COMMAND_LINE_ARGUMENT REQUIRED
2425+
VARIABLE_NAME OPTIMIZER_EXTRA_PRUNING_DEPTH
2426+
VARIABLE_SCOPE SESSION
2427+
VARIABLE_TYPE BIGINT UNSIGNED
2428+
VARIABLE_COMMENT If the optimizer needs to enumerate join prefix of this size or larger, then it will try agressively prune away the search space.
2429+
NUMERIC_MIN_VALUE 0
2430+
NUMERIC_MAX_VALUE 62
2431+
NUMERIC_BLOCK_SIZE 1
2432+
ENUM_VALUE_LIST NULL
2433+
READ_ONLY NO
2434+
COMMAND_LINE_ARGUMENT REQUIRED
24252435
VARIABLE_NAME OPTIMIZER_MAX_SEL_ARG_WEIGHT
24262436
VARIABLE_SCOPE SESSION
24272437
VARIABLE_TYPE BIGINT UNSIGNED

sql/sql_class.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ typedef struct system_variables
720720
ulong net_retry_count;
721721
ulong net_wait_timeout;
722722
ulong net_write_timeout;
723+
ulong optimizer_extra_pruning_depth;
723724
ulong optimizer_prune_level;
724725
ulong optimizer_search_depth;
725726
ulong optimizer_selectivity_sampling_limit;

0 commit comments

Comments
 (0)