Skip to content

Commit

Permalink
Removed /2 of InnoDB ref_per_key[] estimates
Browse files Browse the repository at this point in the history
The original code was there to favor index search over table scan.
This is not needed anymore as the cost calculations for table scans
and index lookups are now more exact.
  • Loading branch information
montywi committed Feb 10, 2023
1 parent 87507bb commit 01c8217
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 33 deletions.
8 changes: 4 additions & 4 deletions mysql-test/main/derived_split_innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ t2
where t1.id = dt.id and t1.itemid = dt.itemid and t2.id=t1.itemid;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 1
1 PRIMARY t1 ref idx idx 4 test.t2.id 1
1 PRIMARY <derived2> ref key0 key0 9 test.t2.id,test.t1.id 1
1 PRIMARY <derived2> ref key1 key1 4 test.t2.id 1
1 PRIMARY t1 ref idx idx 4 test.t2.id 3 Using where
2 DERIVED t3 ref idx1,idx2 idx1 4 const 5 Using where; Using index
select t1.id, t1.itemid, dt.id, t2.id
from t1,
Expand All @@ -227,8 +227,8 @@ t2
where t1.id = dt.id and t1.itemid = dt.itemid and t2.id=t1.itemid;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 1
1 PRIMARY t1 ref idx idx 4 test.t2.id 1
1 PRIMARY <derived2> ref key0 key0 9 test.t2.id,test.t1.id 1
1 PRIMARY <derived2> ref key1 key1 4 test.t2.id 1
1 PRIMARY t1 ref idx idx 4 test.t2.id 3 Using where
2 DERIVED t3 ref idx1 idx1 4 const 5 Using where; Using index
select t1.id, t1.itemid, dt.id, t2.id
from t1,
Expand Down
8 changes: 4 additions & 4 deletions mysql-test/main/join_outer_innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t12 eq_ref PRIMARY PRIMARY 4 test.t11.k3 1 Using where
1 SIMPLE l2 eq_ref PRIMARY PRIMARY 4 test.t11.k4 1 Using where
1 SIMPLE t9 ref PRIMARY PRIMARY 1 test.t1.a4 1
1 SIMPLE t13 ref PRIMARY,m3 PRIMARY 4 test.t1.a1 1 Using where
1 SIMPLE t13 ref PRIMARY,m3 m3 8 const,test.t1.a1 3 Using index
1 SIMPLE l4 eq_ref PRIMARY PRIMARY 4 test.t13.m2 1 Using where
1 SIMPLE m2 ref PRIMARY,m3 PRIMARY 4 test.t1.a1 1 Using where
1 SIMPLE m2 ref PRIMARY,m3 m3 8 const,test.t1.a1 3 Using index
1 SIMPLE l3 eq_ref PRIMARY PRIMARY 4 test.m2.m2 1 Using where
1 SIMPLE t14 eq_ref PRIMARY PRIMARY 2 test.t1.a8 1 Using where
1 SIMPLE t15 eq_ref PRIMARY PRIMARY 2 test.t1.a9 1 Using where
Expand All @@ -471,9 +471,9 @@ id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t12 eq_ref PRIMARY PRIMARY 4 test.t11.k3 1 Using where
1 SIMPLE l2 eq_ref PRIMARY PRIMARY 4 test.t11.k4 1 Using where
1 SIMPLE t9 ref PRIMARY PRIMARY 1 test.t1.a4 1
1 SIMPLE t13 ref PRIMARY,m3 PRIMARY 4 test.t1.a1 1 Using where
1 SIMPLE t13 ref PRIMARY,m3 m3 8 const,test.t1.a1 3 Using index
1 SIMPLE l4 eq_ref PRIMARY PRIMARY 4 test.t13.m2 1 Using where
1 SIMPLE m2 ref PRIMARY,m3 PRIMARY 4 test.t1.a1 1 Using where
1 SIMPLE m2 ref PRIMARY,m3 m3 8 const,test.t1.a1 3 Using index
1 SIMPLE l3 eq_ref PRIMARY PRIMARY 4 test.m2.m2 1 Using where
1 SIMPLE t14 eq_ref PRIMARY PRIMARY 2 test.t1.a8 1 Using where
1 SIMPLE t15 eq_ref PRIMARY PRIMARY 2 test.t1.a9 1 Using where
Expand Down
8 changes: 4 additions & 4 deletions mysql-test/main/mdev-25830.result
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ ORDER BY sysapproval_approver0.`order`
LIMIT 0, 50 ;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 SIMPLE task2 range PRIMARY,sys_class_name_2,sys_domain_path PRIMARY 96 NULL 1 0.00 100.00 100.00 Using where; Using temporary; Using filesort
1 SIMPLE task1 ref PRIMARY,task_parent,sys_class_name_2,sys_domain_path task_parent 99 test.task2.sys_id 1 NULL 100.00 NULL Using index condition; Using where
1 SIMPLE sysapproval_approver0 ref sysapproval_approver_ref5,sys_domain_path,sysapproval_approver_CHG1975376 sysapproval_approver_ref5 99 test.task1.sys_id 1 NULL 100.00 NULL Using index condition; Using where
1 SIMPLE task1 ref PRIMARY,task_parent,sys_class_name_2,sys_domain_path task_parent 99 test.task2.sys_id 2 NULL 100.00 NULL Using index condition; Using where
1 SIMPLE sysapproval_approver0 ref sysapproval_approver_ref5,sys_domain_path,sysapproval_approver_CHG1975376 sysapproval_approver_ref5 99 test.task1.sys_id 3 NULL 100.00 NULL Using index condition; Using where
set optimizer_use_condition_selectivity=4;
analyze SELECT sysapproval_approver0.`sys_id`
FROM ((sysapproval_approver sysapproval_approver0
Expand All @@ -48,8 +48,8 @@ ORDER BY sysapproval_approver0.`order`
LIMIT 0, 50 ;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 SIMPLE task2 range PRIMARY,sys_class_name_2,sys_domain_path PRIMARY 96 NULL 1 0.00 100.00 100.00 Using where; Using temporary; Using filesort
1 SIMPLE task1 ref PRIMARY,task_parent,sys_class_name_2,sys_domain_path task_parent 99 test.task2.sys_id 1 NULL 100.00 NULL Using index condition; Using where
1 SIMPLE sysapproval_approver0 ref sysapproval_approver_ref5,sys_domain_path,sysapproval_approver_CHG1975376 sysapproval_approver_ref5 99 test.task1.sys_id 1 NULL 100.00 NULL Using index condition; Using where
1 SIMPLE task1 ref PRIMARY,task_parent,sys_class_name_2,sys_domain_path task_parent 99 test.task2.sys_id 2 NULL 100.00 NULL Using index condition; Using where
1 SIMPLE sysapproval_approver0 ref sysapproval_approver_ref5,sys_domain_path,sysapproval_approver_CHG1975376 sysapproval_approver_ref5 99 test.task1.sys_id 3 NULL 100.00 NULL Using index condition; Using where
drop table sysapproval_approver,task;
set global innodb_stats_persistent= @innodb_stats_persistent_save;
set global innodb_stats_persistent_sample_pages=
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/main/order_by_innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ dd.d1, dd.d2, dd.id limit 1
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL #
1 PRIMARY t2 eq_ref PRIMARY,id2 id2 8 test.t1.id,func # Using where; Using index
2 DEPENDENT SUBQUERY dd ref id2,for_latest_sort id2 4 test.t1.id # Using where; Using filesort
2 DEPENDENT SUBQUERY dd range id2,for_latest_sort for_latest_sort 6 NULL # Using where
drop table t1,t2,t3;
# End of 10.2 tests
#
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/main/subselect2.result
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ DOCID DOCNAME DOCTYPEID FOLDERID AUTHOR CREATED TITLE SUBTITLE DOCABSTRACT PUBLI
c373e9f5ad07993f3859444553544200 Last Discussion c373e9f5ad079174ff17444553544200 c373e9f5ad0796c0eca4444553544200 Goldilocks 2003-06-09 11:21:06 Title: Last Discussion NULL Setting new abstract and keeping doc checked out 2003-06-09 10:51:26 2003-06-09 10:51:26 NULL NULL NULL 03eea05112b845949f3fd03278b5fe43 2003-06-09 11:21:06 admin 0 NULL Discussion NULL NULL
EXPLAIN SELECT t2.*, t4.DOCTYPENAME, t1.CONTENTSIZE,t1.MIMETYPE FROM t2 INNER JOIN t4 ON t2.DOCTYPEID = t4.DOCTYPEID LEFT OUTER JOIN t1 ON t2.DOCID = t1.DOCID WHERE t2.FOLDERID IN(SELECT t3_a.FOLDERID FROM t3 as t3_a WHERE t3_a.PARENTID IN(SELECT t3_b.FOLDERID FROM t3 as t3_b WHERE t3_b.PARENTID IN(SELECT t3_c.FOLDERID FROM t3 as t3_c WHERE t3_c.PARENTID IN(SELECT t3_d.FOLDERID FROM t3 as t3_d WHERE t3_d.PARENTID IN(SELECT t3_e.FOLDERID FROM t3 as t3_e WHERE t3_e.PARENTID='2f6161e879db43c1a5b82c21ddc49089' AND t3_e.FOLDERNAME = 'Level1') AND t3_d.FOLDERNAME = 'Level2') AND t3_c.FOLDERNAME = 'Level3') AND t3_b.FOLDERNAME = 'CopiedFolder') AND t3_a.FOLDERNAME = 'Movie Reviews') AND t2.DOCNAME = 'Last Discussion';
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL DDOCTYPEID_IDX,DFOLDERID_IDX NULL NULL NULL 9 Using where
1 PRIMARY t4 eq_ref PRIMARY PRIMARY 34 test.t2.DOCTYPEID 1
1 PRIMARY t4 ALL PRIMARY NULL NULL NULL 10
1 PRIMARY t2 ALL DDOCTYPEID_IDX,DFOLDERID_IDX NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
1 PRIMARY t1 eq_ref PRIMARY PRIMARY 34 test.t2.DOCID 1
1 PRIMARY t3_a eq_ref PRIMARY,FFOLDERID_IDX,CMFLDRPARNT_IDX PRIMARY 34 test.t2.FOLDERID 1 Using where
1 PRIMARY t3_b eq_ref PRIMARY,FFOLDERID_IDX,CMFLDRPARNT_IDX PRIMARY 34 test.t3_a.PARENTID 1 Using where
Expand Down
24 changes: 24 additions & 0 deletions mysql-test/main/subselect_innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ id select_type table type possible_keys key key_len ref rows Extra
#
# MDEV-6081: ORDER BY+ref(const): selectivity is very incorrect (MySQL Bug#14338686)
#
insert into t2 select seq,seq,seq from seq_10000_to_11000;
alter table t2 add key2 int;
update t2 set key2=key1;
alter table t2 add key(key2);
Expand All @@ -580,6 +581,29 @@ t1;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL #
2 DEPENDENT SUBQUERY t2 ref key1 key1 5 test.t1.a # Using where; Using filesort
select
(SELECT
concat(id, '-', key1, '-', col1)
FROM t2
WHERE t2.key1 = t1.a
ORDER BY t2.key2 ASC LIMIT 1)
from
t1;
(SELECT
concat(id, '-', key1, '-', col1)
FROM t2
WHERE t2.key1 = t1.a
ORDER BY t2.key2 ASC LIMIT 1)
100-0-123456
101-1-123456
102-2-123456
103-3-123456
104-4-123456
105-5-123456
106-6-123456
107-7-123456
108-8-123456
109-9-123456
drop table t1,t2;
#
# MDEV-12931: semi-join in ON expression of STRAIGHT_JOIN
Expand Down
14 changes: 14 additions & 0 deletions mysql-test/main/subselect_innodb.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- source include/no_valgrind_without_big.inc
-- source include/have_innodb.inc
-- source include/have_sequence.inc

# Note: the tests uses only non-semijoin subqueries so semi-join switch
# settings are not relevant.
Expand Down Expand Up @@ -568,6 +569,11 @@ from
--echo # MDEV-6081: ORDER BY+ref(const): selectivity is very incorrect (MySQL Bug#14338686)
--echo #


# Table t2 has 100 equal values / key value, which causes it to prefer index scan instead of ref
# Fix it by adding more different values to key1
insert into t2 select seq,seq,seq from seq_10000_to_11000;

alter table t2 add key2 int;
update t2 set key2=key1;
alter table t2 add key(key2);
Expand All @@ -583,6 +589,14 @@ explain select
ORDER BY t2.key2 ASC LIMIT 1)
from
t1;
select
(SELECT
concat(id, '-', key1, '-', col1)
FROM t2
WHERE t2.key1 = t1.a
ORDER BY t2.key2 ASC LIMIT 1)
from
t1;

drop table t1,t2;

Expand Down
2 changes: 1 addition & 1 deletion mysql-test/main/update_use_source.result
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ rollback;
explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 2 Using where
2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 4 Using index
2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 8 Using index
start transaction;
update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
affected rows: 4
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/suite/innodb/r/innodb-index-online.result
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ CREATE INDEX c2d ON t1(c2);
SHOW INDEX FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
t1 0 PRIMARY 1 c1 A 80 NULL NULL BTREE NO
t1 1 c2d 1 c2 A 10 NULL NULL YES BTREE NO
t1 1 c2d 1 c2 A 5 NULL NULL YES BTREE NO
EXPLAIN SELECT COUNT(*) FROM t1 WHERE c2 > 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range c2d c2d 5 NULL 32 Using where; Using index
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/suite/innodb/r/innodb_bug57252.result
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cardinality
2
1
Table Op Msg_type Msg_text
test.bug57252 analyze status Engine-independent statistics collected
test.bug57252 analyze status OK
Expand Down
8 changes: 4 additions & 4 deletions mysql-test/suite/innodb/r/innodb_stats.result
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ INDEX_NAME a_key
SEQ_IN_INDEX 1
COLUMN_NAME a
COLLATION A
CARDINALITY 2
CARDINALITY 1
SUB_PART NULL
PACKED NULL
NULLABLE YES
Expand Down Expand Up @@ -204,7 +204,7 @@ INDEX_NAME a_key
SEQ_IN_INDEX 1
COLUMN_NAME a
COLLATION A
CARDINALITY 3
CARDINALITY 1
SUB_PART NULL
PACKED NULL
NULLABLE YES
Expand Down Expand Up @@ -257,7 +257,7 @@ INDEX_NAME a_key
SEQ_IN_INDEX 1
COLUMN_NAME a
COLLATION A
CARDINALITY 2
CARDINALITY 1
SUB_PART NULL
PACKED NULL
NULLABLE YES
Expand Down Expand Up @@ -522,7 +522,7 @@ INDEX_NAME a_key
SEQ_IN_INDEX 1
COLUMN_NAME a
COLLATION A
CARDINALITY 10
CARDINALITY 5
SUB_PART NULL
PACKED NULL
NULLABLE YES
Expand Down
8 changes: 4 additions & 4 deletions mysql-test/suite/innodb/r/innodb_stats_fetch.result
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ FROM information_schema.statistics WHERE table_name = 'test_ps_fetch'
ORDER BY index_name, seq_in_index;
seq_in_index 1
column_name c
cardinality 6
cardinality 3
seq_in_index 2
column_name d
cardinality 22
cardinality 11
seq_in_index 1
column_name a
cardinality 40
cardinality 20
seq_in_index 2
column_name b
cardinality 200
cardinality 90
SELECT
table_rows, avg_row_length, max_data_length, index_length
FROM information_schema.tables WHERE table_name = 'test_ps_fetch';
Expand Down
7 changes: 0 additions & 7 deletions storage/innobase/handler/ha_innodb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14891,13 +14891,6 @@ ha_innobase::info_low(
innodb_rec_per_key(index, j,
stats.records));

/* Since MySQL seems to favor table scans
too much over index searches, we pretend
index selectivity is 2 times better than
our estimate: */

rec_per_key_int = rec_per_key_int / 2;

if (rec_per_key_int == 0) {
rec_per_key_int = 1;
}
Expand Down

0 comments on commit 01c8217

Please sign in to comment.