1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-08 11:22:35 +03:00

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)
This commit is contained in:
Monty
2022-12-27 14:49:27 +02:00
parent b5df077e85
commit 3fa99f0c0e
81 changed files with 765 additions and 740 deletions

View File

@@ -22,15 +22,15 @@ Last_query_cost 0.000000
explain select count(*) from t1 where a > 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 12 Using where; Using index
Last_query_cost 0.002795
Last_query_cost 0.002877
explain select count(*) from t1 where a > 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 12 Using where; Using index
Last_query_cost 0.002795
Last_query_cost 0.002877
explain select count(*) from t1 where a > 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 11 Using where; Using index
Last_query_cost 0.002665
Last_query_cost 0.002747
#
# Shorter indexes are prefered over longer indexs
#
@@ -41,20 +41,20 @@ Last_query_cost 0.007441
explain select count(*) from t1 where b between 5 and 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range ba,bda ba 5 NULL 6 Using where; Using index
Last_query_cost 0.002015
Last_query_cost 0.002097
explain select sum(b+c) from t1 where b between 5 and 6 and c between 5 and 6;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range ba,bda,cba,cb cba 10 NULL 2 Using where; Using index
Last_query_cost 0.001494
Last_query_cost 0.001577
# Cost of 'd' should be slightly smaller as key 'ba' is longer than 'd'
explain select count(*) from t1 where b > 6;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range ba,bda ba 5 NULL 5 Using where; Using index
Last_query_cost 0.001885
Last_query_cost 0.001967
explain select count(*) from t1 where d > 6;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range d d 5 NULL 5 Using where; Using index
Last_query_cost 0.001885
Last_query_cost 0.001967
#
# Check covering index usage
#
@@ -68,15 +68,15 @@ Last_query_cost 0.007441
explain select count(*) from t1 where b=2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ref ba,bda ba 5 const 2 Using index
Last_query_cost 0.001059
Last_query_cost 0.001141
explain select count(*) from t1 where b=2 and c=2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ref ba,bda,cba,cb cba 10 const,const 2 Using index
Last_query_cost 0.001059
Last_query_cost 0.001141
explain select count(*) from t1 where b=3 and c between 3 and 4;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range ba,bda,cba,cb cba 10 NULL 2 Using where; Using index
Last_query_cost 0.001494
Last_query_cost 0.001577
#
# Prefer eq keys over ref keys
#
@@ -87,5 +87,5 @@ Last_query_cost 0.003126
explain select a,b,e from t1 where d=10 or d=11;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range d d 5 NULL 2 Using index condition
Last_query_cost 0.003126
Last_query_cost 0.003291
drop table t1;