mariadb/mysql-test/main/opt_trace_selectivity.result
Monty 3fa99f0c0e Change cost for REF to take into account cost for 1 extra key read_next
The main difference in code path between EQ_REF and REF is that for
REF we have to do an extra read_next on the index to check that there
is no more matching rows.

Before this patch we added a preference of EQ_REF by ensuring that REF
would always estimate to find at least 2 rows.

This patch adds the cost of the extra key read_next to REF access and
removes the code that limited REF to at least 2 rows. For some queries
this can have a big effect as the total estimated rows will be halved
for each REF table with 1 rows.

multi_range cost calculations are also changed to take into account
the difference between EQ_REF and REF.

The effect of the patch to the test suite:
- About 80 test case changed
- Almost all changes where for EXPLAIN where estimated rows for REF
  where changed from 2 to 1.
- A few test cases using explain extended had a change of 'filtered'.
  This is because of the estimated rows are now closer to the
  calculated selectivity.
- A very few test had a change of table order.
  This is because the change of estimated rows from 2 to 1 or the small
  cost change for REF
  (main.subselect_sj_jcl6, main.group_by, main.dervied_cond_pushdown,
  main.distinct, main.join_nested, main.order_by, main.join_cache)
- No key statistics and the estimated rows are now smaller which cased
  estimated filtering to be lower.
  (main.subselect_sj_mat)
- The number of total rows are halved.
  (main.derived_cond_pushdown)
- Plans with 1 row changed to use RANGE instead of REF.
  (main.group_min_max)
- ALL changed to REF
  (main.key_diff)
- Key changed from ref + index_only to PRIMARY key for InnoDB, as
  OPTIMIZER_ROW_LOOKUP_COST + OPTIMIZER_ROW_NEXT_FIND_COST is smaller than
  OPTIMIZER_KEY_LOOKUP_COST + OPTIMIZER_KEY_NEXT_FIND_COST.
  (main.join_outer_innodb)
- Cost changes printouts
  (main.opt_trace*)
- Result order change
  (innodb_gis.rtree)
2023-02-10 12:58:50 +02:00

369 lines
9.7 KiB
Text

create or replace table t1 (a int, b int, c int, key(a,c), key(b,c), key (c,b)) engine=aria;
insert into t1 select seq/100+1, mod(seq,10), mod(seq,15) from seq_1_to_10000;
insert into t1 select seq/100+1, mod(seq,10), 10 from seq_1_to_1000;
optimize table t1;
Table Op Msg_type Msg_text
test.t1 optimize status OK
select count(*) from t1 where a=2;
count(*)
200
select count(*) from t1 where b=5;
count(*)
1100
select count(*) from t1 where c=5;
count(*)
667
select count(*) from t1 where c=10;
count(*)
1667
select count(*) from t1 where a=2 and b=5;
count(*)
20
select count(*) from t1 where c=10 and b=5;
count(*)
433
select count(*) from t1 where c=5 and b=5;
count(*)
334
set optimizer_trace="enabled=on";
select count(*) from t1 where a=2 and b=5 and c=10;
count(*)
14
set @trace=(select trace from INFORMATION_SCHEMA.OPTIMIZER_TRACE);
select
JSON_DETAILED(
JSON_EXTRACT(
JSON_EXTRACT(@trace, '$**.considered_execution_plans'),
'$[0]'
)
) as JS;
JS
[
{
"plan_prefix": "",
"get_costs_for_tables":
[
{
"best_access_path":
{
"table": "t1",
"plan_details":
{
"record_count": 1
},
"considered_access_paths":
[
{
"access_type": "ref",
"index": "a",
"used_range_estimates": true,
"rows": 104,
"cost": 0.060988785,
"chosen": true
},
{
"access_type": "ref",
"index": "b",
"used_range_estimates": true,
"rows": 340,
"cost": 0.141618657,
"chosen": false,
"cause": "cost"
},
{
"access_type": "ref",
"index": "c",
"used_range_estimates": true,
"rows": 632,
"cost": 0.241826241,
"chosen": false,
"cause": "cost"
},
{
"access_type": "index_merge",
"rows": 7,
"rows_after_filter": 7,
"rows_out": 7,
"cost": 0.045367017,
"chosen": true
}
],
"chosen_access_method":
{
"type": "index_merge",
"rows_read": 7,
"rows_out": 7,
"cost": 0.045367017,
"uses_join_buffering": false
}
}
}
]
},
{
"plan_prefix": "",
"table": "t1",
"rows_for_plan": 7,
"cost_for_plan": 0.045367017
}
]
select JSON_DETAILED(JSON_EXTRACT(@trace, '$**.selectivity_for_indexes')) as JS;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.009454545
},
{
"index_name": "b",
"selectivity_from_index": 0.1
},
{
"use_opt_range_condition_rows_selectivity": 6.363636e-4
}
]
]
select count(*) from t1 where a=2 and b=5 and c=5;
count(*)
3
set @trace=(select trace from INFORMATION_SCHEMA.OPTIMIZER_TRACE);
select
JSON_DETAILED(
JSON_EXTRACT(
JSON_EXTRACT(@trace, '$**.considered_execution_plans'),
'$[0]'
)
) as JS;
JS
[
{
"plan_prefix": "",
"get_costs_for_tables":
[
{
"best_access_path":
{
"table": "t1",
"plan_details":
{
"record_count": 1
},
"considered_access_paths":
[
{
"access_type": "ref",
"index": "a",
"used_range_estimates": true,
"rows": 6,
"cost": 0.005388489,
"chosen": true
},
{
"access_type": "ref",
"index": "b",
"used_range_estimates": true,
"rows": 232,
"cost": 0.104720241,
"chosen": false,
"cause": "cost"
},
{
"access_type": "ref",
"index": "c",
"used_range_estimates": true,
"rows": 293,
"cost": 0.125561013,
"chosen": false,
"cause": "cost"
},
{
"type": "scan",
"chosen": false,
"cause": "cost"
}
],
"chosen_access_method":
{
"type": "ref",
"rows_read": 6,
"rows_out": 0.6,
"cost": 0.005388489,
"uses_join_buffering": false
}
}
}
]
},
{
"plan_prefix": "",
"table": "t1",
"rows_for_plan": 0.6,
"cost_for_plan": 0.005388489,
"pushdown_cond_selectivity": 0.1,
"filtered": 10,
"rows_out": 0.6
}
]
select JSON_DETAILED(JSON_EXTRACT(@trace, '$**.selectivity_for_indexes')) as JS;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 5.454545e-4
},
{
"index_name": "b",
"selectivity_from_index": 0.1
}
]
]
# Ensure that we only use selectivity from non used index for simple cases
select count(*) from t1 where (a=2 and b= 5);
count(*)
20
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.017545455
},
{
"index_name": "b",
"selectivity_from_index": 0.073181818
}
]
]
# All of the following should have selectivity=1 for index 'b'
select count(*) from t1 where (a=2 and b between 0 and 100);
count(*)
200
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.017545455
},
{
"index_name": "b",
"selectivity_from_index": 1
}
]
]
select count(*) from t1 where (a in (2,3) and b between 0 and 100);
count(*)
400
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.035090909
},
{
"index_name": "b",
"selectivity_from_index": 1
}
]
]
select count(*) from t1 where (a>2 and b between 0 and 100);
count(*)
10702
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.973909091
},
{
"index_name": "b",
"selectivity_from_index": 1
}
]
]
select count(*) from t1 where (a>=2 and b between 0 and 100);
count(*)
10902
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.991454545
},
{
"index_name": "b",
"selectivity_from_index": 1
}
]
]
select count(*) from t1 where (a<=2 and b between 0 and 100);
count(*)
298
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.026181818
},
{
"index_name": "b",
"selectivity_from_index": 1
}
]
]
select count(*) from t1 where (a<2 and b between 0 and 100);
count(*)
98
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.008636364
},
{
"index_name": "b",
"selectivity_from_index": 1
}
]
]
select count(*) from t1 where (a between 2 and 3 and b between 0 and 100);
count(*)
400
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) as JS
from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
JS
[
[
{
"index_name": "a",
"selectivity_from_index": 0.035090909
},
{
"index_name": "b",
"selectivity_from_index": 1
}
]
]
drop table t1;
set optimizer_trace='enabled=off';