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

@@ -502,7 +502,7 @@ EXPLAIN EXTENDED SELECT vkey FROM t0 WHERE pk IN
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t0 ALL PRIMARY NULL NULL NULL 5 100.00
1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t0.pk 1 100.00 Using where
1 PRIMARY t2 ref vkey vkey 4 test.t1.vnokey 2 50.00 Using index; FirstMatch(t1)
1 PRIMARY t2 ref vkey vkey 4 test.t1.vnokey 1 100.00 Using index; FirstMatch(t1)
Warnings:
Note 1003 select `test`.`t0`.`vkey` AS `vkey` from `test`.`t0` `t1` semi join (`test`.`t0` `t2`) join `test`.`t0` where `test`.`t1`.`pk` = `test`.`t0`.`pk` and `test`.`t2`.`vkey` = `test`.`t1`.`vnokey`
SELECT vkey FROM t0 WHERE pk IN
@@ -2154,7 +2154,7 @@ explain
SELECT * FROM t1, t2 WHERE (t2.a , t1.b) IN (SELECT a, b FROM t3);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
1 PRIMARY t3 ref b b 5 test.t1.b 2 Using where; Start temporary
1 PRIMARY t3 ref b b 5 test.t1.b 1 Using where; Start temporary
1 PRIMARY t2 eq_ref PRIMARY PRIMARY 4 test.t3.a 1 Using index; End temporary
SELECT * FROM t1, t2 WHERE (t2.a , t1.b) IN (SELECT a, b FROM t3);
b a
@@ -2179,7 +2179,7 @@ explain
SELECT * FROM t3 WHERE t3.a IN (SELECT t5.a FROM t2, t4, t5 WHERE t2.c = t5.a AND t2.b = t5.b);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t5 index a a 10 NULL 2 Using where; Using index; Start temporary
1 PRIMARY t2 ref b b 5 test.t5.b 2 Using where
1 PRIMARY t2 ref b b 5 test.t5.b 1 Using where
1 PRIMARY t4 ALL NULL NULL NULL NULL 3
1 PRIMARY t3 ALL NULL NULL NULL NULL 15 Using where; End temporary; Using join buffer (flat, BNL join)
SELECT * FROM t3 WHERE t3.a IN (SELECT t5.a FROM t2, t4, t5 WHERE t2.c = t5.a AND t2.b = t5.b);
@@ -2436,9 +2436,9 @@ SET SESSION optimizer_switch='loosescan=off';
EXPLAIN
SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4
1 PRIMARY t2 range idx idx 4 NULL 2 Using where; Using index
1 PRIMARY t3 ref idx idx 4 test.t2.b 1 Using index; FirstMatch(t1)
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
1 PRIMARY t3 range idx idx 4 NULL 2 Using where; Using index
1 PRIMARY t2 ref idx idx 9 test.t3.b,test.t1.a 1 Using index; FirstMatch(t1)
SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
a
5
@@ -2446,9 +2446,9 @@ SET SESSION optimizer_switch='loosescan=on';
EXPLAIN
SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 4
1 PRIMARY t2 range idx idx 4 NULL 2 Using where; Using index
1 PRIMARY t3 ref idx idx 4 test.t2.b 1 Using index; FirstMatch(t1)
1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
1 PRIMARY t3 range idx idx 4 NULL 2 Using where; Using index
1 PRIMARY t2 ref idx idx 9 test.t3.b,test.t1.a 1 Using index; FirstMatch(t1)
SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
a
5
@@ -2561,7 +2561,7 @@ INSERT INTO t1 VALUES
(6,3),(7,1),(8,4),(9,3),(10,2);
CREATE TABLE t2 ( c INT, d INT, KEY(c) );
INSERT INTO t2 VALUES
(1,2),(2,1),(3,3),(4,2),(5,5),(6,3),(7,1);
(1,2),(2,1),(3,3),(4,2),(5,5),(6,3),(7,1),(11,11);
analyze table t1,t2;
Table Op Msg_type Msg_text
test.t1 analyze status Engine-independent statistics collected
@@ -2571,15 +2571,15 @@ test.t2 analyze status OK
explain
SELECT a, b, d FROM t1, t2
WHERE ( b, d ) IN
( SELECT b, d FROM t1, t2 WHERE b = c );
( SELECT b, d FROM t1 as t3, t2 as t4 WHERE b = c );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index b b 5 NULL 10 Using where; Using index; Start temporary
1 PRIMARY t2 ref c c 5 test.t1.b 1
1 PRIMARY t3 index b b 5 NULL 10 Using where; Using index; Start temporary
1 PRIMARY t4 ref c c 5 test.t3.b 1
1 PRIMARY t1 ALL b NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
1 PRIMARY t2 ALL NULL NULL NULL NULL 7 Using where; End temporary; Using join buffer (flat, BNL join)
1 PRIMARY t2 ALL NULL NULL NULL NULL 8 Using where; End temporary; Using join buffer (flat, BNL join)
SELECT a, b, d FROM t1, t2
WHERE ( b, d ) IN
( SELECT b, d FROM t1, t2 WHERE b = c );
( SELECT b, d FROM t1 as t3, t2 as t4 WHERE b = c );
a b d
1 2 1
1 2 1
@@ -2768,7 +2768,7 @@ WHERE (t1_1.a, t1_2.a) IN ( SELECT a, b FROM v1 );
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1_1 ALL NULL NULL NULL NULL 11 Using where
1 PRIMARY t1_2 ALL NULL NULL NULL NULL 11
1 PRIMARY <derived3> ref key0 key0 5 test.t1_1.a 2 Using where; FirstMatch(t1_2)
1 PRIMARY <derived3> ref key0 key0 5 test.t1_1.a 1 Using where; FirstMatch(t1_2)
3 DERIVED t1 ALL NULL NULL NULL NULL 11
SELECT * FROM t1 AS t1_1, t1 AS t1_2
WHERE (t1_1.a, t1_2.a) IN ( SELECT a, b FROM v1 );