Skip to content

Commit 01c8217

Browse files
committed
Removed /2 of InnoDB ref_per_key[] estimates
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.
1 parent 87507bb commit 01c8217

13 files changed

+64
-33
lines changed

mysql-test/main/derived_split_innodb.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ t2
208208
where t1.id = dt.id and t1.itemid = dt.itemid and t2.id=t1.itemid;
209209
id select_type table type possible_keys key key_len ref rows Extra
210210
1 PRIMARY t2 ALL NULL NULL NULL NULL 1
211-
1 PRIMARY t1 ref idx idx 4 test.t2.id 1
212-
1 PRIMARY <derived2> ref key0 key0 9 test.t2.id,test.t1.id 1
211+
1 PRIMARY <derived2> ref key1 key1 4 test.t2.id 1
212+
1 PRIMARY t1 ref idx idx 4 test.t2.id 3 Using where
213213
2 DERIVED t3 ref idx1,idx2 idx1 4 const 5 Using where; Using index
214214
select t1.id, t1.itemid, dt.id, t2.id
215215
from t1,
@@ -227,8 +227,8 @@ t2
227227
where t1.id = dt.id and t1.itemid = dt.itemid and t2.id=t1.itemid;
228228
id select_type table type possible_keys key key_len ref rows Extra
229229
1 PRIMARY t2 ALL NULL NULL NULL NULL 1
230-
1 PRIMARY t1 ref idx idx 4 test.t2.id 1
231-
1 PRIMARY <derived2> ref key0 key0 9 test.t2.id,test.t1.id 1
230+
1 PRIMARY <derived2> ref key1 key1 4 test.t2.id 1
231+
1 PRIMARY t1 ref idx idx 4 test.t2.id 3 Using where
232232
2 DERIVED t3 ref idx1 idx1 4 const 5 Using where; Using index
233233
select t1.id, t1.itemid, dt.id, t2.id
234234
from t1,

mysql-test/main/join_outer_innodb.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,9 @@ id select_type table type possible_keys key key_len ref rows Extra
448448
1 SIMPLE t12 eq_ref PRIMARY PRIMARY 4 test.t11.k3 1 Using where
449449
1 SIMPLE l2 eq_ref PRIMARY PRIMARY 4 test.t11.k4 1 Using where
450450
1 SIMPLE t9 ref PRIMARY PRIMARY 1 test.t1.a4 1
451-
1 SIMPLE t13 ref PRIMARY,m3 PRIMARY 4 test.t1.a1 1 Using where
451+
1 SIMPLE t13 ref PRIMARY,m3 m3 8 const,test.t1.a1 3 Using index
452452
1 SIMPLE l4 eq_ref PRIMARY PRIMARY 4 test.t13.m2 1 Using where
453-
1 SIMPLE m2 ref PRIMARY,m3 PRIMARY 4 test.t1.a1 1 Using where
453+
1 SIMPLE m2 ref PRIMARY,m3 m3 8 const,test.t1.a1 3 Using index
454454
1 SIMPLE l3 eq_ref PRIMARY PRIMARY 4 test.m2.m2 1 Using where
455455
1 SIMPLE t14 eq_ref PRIMARY PRIMARY 2 test.t1.a8 1 Using where
456456
1 SIMPLE t15 eq_ref PRIMARY PRIMARY 2 test.t1.a9 1 Using where
@@ -471,9 +471,9 @@ id select_type table type possible_keys key key_len ref rows Extra
471471
1 SIMPLE t12 eq_ref PRIMARY PRIMARY 4 test.t11.k3 1 Using where
472472
1 SIMPLE l2 eq_ref PRIMARY PRIMARY 4 test.t11.k4 1 Using where
473473
1 SIMPLE t9 ref PRIMARY PRIMARY 1 test.t1.a4 1
474-
1 SIMPLE t13 ref PRIMARY,m3 PRIMARY 4 test.t1.a1 1 Using where
474+
1 SIMPLE t13 ref PRIMARY,m3 m3 8 const,test.t1.a1 3 Using index
475475
1 SIMPLE l4 eq_ref PRIMARY PRIMARY 4 test.t13.m2 1 Using where
476-
1 SIMPLE m2 ref PRIMARY,m3 PRIMARY 4 test.t1.a1 1 Using where
476+
1 SIMPLE m2 ref PRIMARY,m3 m3 8 const,test.t1.a1 3 Using index
477477
1 SIMPLE l3 eq_ref PRIMARY PRIMARY 4 test.m2.m2 1 Using where
478478
1 SIMPLE t14 eq_ref PRIMARY PRIMARY 2 test.t1.a8 1 Using where
479479
1 SIMPLE t15 eq_ref PRIMARY PRIMARY 2 test.t1.a9 1 Using where

mysql-test/main/mdev-25830.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ ORDER BY sysapproval_approver0.`order`
2525
LIMIT 0, 50 ;
2626
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
2727
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
28-
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
29-
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
28+
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
29+
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
3030
set optimizer_use_condition_selectivity=4;
3131
analyze SELECT sysapproval_approver0.`sys_id`
3232
FROM ((sysapproval_approver sysapproval_approver0
@@ -48,8 +48,8 @@ ORDER BY sysapproval_approver0.`order`
4848
LIMIT 0, 50 ;
4949
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
5050
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
51-
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
52-
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
51+
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
52+
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
5353
drop table sysapproval_approver,task;
5454
set global innodb_stats_persistent= @innodb_stats_persistent_save;
5555
set global innodb_stats_persistent_sample_pages=

mysql-test/main/order_by_innodb.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ dd.d1, dd.d2, dd.id limit 1
258258
id select_type table type possible_keys key key_len ref rows Extra
259259
1 PRIMARY t1 ALL NULL NULL NULL NULL #
260260
1 PRIMARY t2 eq_ref PRIMARY,id2 id2 8 test.t1.id,func # Using where; Using index
261-
2 DEPENDENT SUBQUERY dd ref id2,for_latest_sort id2 4 test.t1.id # Using where; Using filesort
261+
2 DEPENDENT SUBQUERY dd range id2,for_latest_sort for_latest_sort 6 NULL # Using where
262262
drop table t1,t2,t3;
263263
# End of 10.2 tests
264264
#

mysql-test/main/subselect2.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ DOCID DOCNAME DOCTYPEID FOLDERID AUTHOR CREATED TITLE SUBTITLE DOCABSTRACT PUBLI
125125
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
126126
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';
127127
id select_type table type possible_keys key key_len ref rows Extra
128-
1 PRIMARY t2 ALL DDOCTYPEID_IDX,DFOLDERID_IDX NULL NULL NULL 9 Using where
129-
1 PRIMARY t4 eq_ref PRIMARY PRIMARY 34 test.t2.DOCTYPEID 1
128+
1 PRIMARY t4 ALL PRIMARY NULL NULL NULL 10
129+
1 PRIMARY t2 ALL DDOCTYPEID_IDX,DFOLDERID_IDX NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
130130
1 PRIMARY t1 eq_ref PRIMARY PRIMARY 34 test.t2.DOCID 1
131131
1 PRIMARY t3_a eq_ref PRIMARY,FFOLDERID_IDX,CMFLDRPARNT_IDX PRIMARY 34 test.t2.FOLDERID 1 Using where
132132
1 PRIMARY t3_b eq_ref PRIMARY,FFOLDERID_IDX,CMFLDRPARNT_IDX PRIMARY 34 test.t3_a.PARENTID 1 Using where

mysql-test/main/subselect_innodb.result

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ id select_type table type possible_keys key key_len ref rows Extra
560560
#
561561
# MDEV-6081: ORDER BY+ref(const): selectivity is very incorrect (MySQL Bug#14338686)
562562
#
563+
insert into t2 select seq,seq,seq from seq_10000_to_11000;
563564
alter table t2 add key2 int;
564565
update t2 set key2=key1;
565566
alter table t2 add key(key2);
@@ -580,6 +581,29 @@ t1;
580581
id select_type table type possible_keys key key_len ref rows Extra
581582
1 PRIMARY t1 ALL NULL NULL NULL NULL #
582583
2 DEPENDENT SUBQUERY t2 ref key1 key1 5 test.t1.a # Using where; Using filesort
584+
select
585+
(SELECT
586+
concat(id, '-', key1, '-', col1)
587+
FROM t2
588+
WHERE t2.key1 = t1.a
589+
ORDER BY t2.key2 ASC LIMIT 1)
590+
from
591+
t1;
592+
(SELECT
593+
concat(id, '-', key1, '-', col1)
594+
FROM t2
595+
WHERE t2.key1 = t1.a
596+
ORDER BY t2.key2 ASC LIMIT 1)
597+
100-0-123456
598+
101-1-123456
599+
102-2-123456
600+
103-3-123456
601+
104-4-123456
602+
105-5-123456
603+
106-6-123456
604+
107-7-123456
605+
108-8-123456
606+
109-9-123456
583607
drop table t1,t2;
584608
#
585609
# MDEV-12931: semi-join in ON expression of STRAIGHT_JOIN

mysql-test/main/subselect_innodb.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- source include/no_valgrind_without_big.inc
22
-- source include/have_innodb.inc
3+
-- source include/have_sequence.inc
34

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

572+
573+
# Table t2 has 100 equal values / key value, which causes it to prefer index scan instead of ref
574+
# Fix it by adding more different values to key1
575+
insert into t2 select seq,seq,seq from seq_10000_to_11000;
576+
571577
alter table t2 add key2 int;
572578
update t2 set key2=key1;
573579
alter table t2 add key(key2);
@@ -583,6 +589,14 @@ explain select
583589
ORDER BY t2.key2 ASC LIMIT 1)
584590
from
585591
t1;
592+
select
593+
(SELECT
594+
concat(id, '-', key1, '-', col1)
595+
FROM t2
596+
WHERE t2.key1 = t1.a
597+
ORDER BY t2.key2 ASC LIMIT 1)
598+
from
599+
t1;
586600

587601
drop table t1,t2;
588602

mysql-test/main/update_use_source.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ rollback;
317317
explain update t1 set c1=0 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 > 3;
318318
id select_type table type possible_keys key key_len ref rows Extra
319319
1 PRIMARY t1 range t1_c2 t1_c2 5 NULL 2 Using where
320-
2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 4 Using index
320+
2 DEPENDENT SUBQUERY a ref t1_c2 t1_c2 5 test.t1.c2 8 Using index
321321
start transaction;
322322
update t1 set c1=c1+10 where exists (select 'X' from t1 a where a.c2 = t1.c2) and c2 >= 3;
323323
affected rows: 4

mysql-test/suite/innodb/r/innodb-index-online.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ CREATE INDEX c2d ON t1(c2);
204204
SHOW INDEX FROM t1;
205205
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Ignored
206206
t1 0 PRIMARY 1 c1 A 80 NULL NULL BTREE NO
207-
t1 1 c2d 1 c2 A 10 NULL NULL YES BTREE NO
207+
t1 1 c2d 1 c2 A 5 NULL NULL YES BTREE NO
208208
EXPLAIN SELECT COUNT(*) FROM t1 WHERE c2 > 3;
209209
id select_type table type possible_keys key key_len ref rows Extra
210210
1 SIMPLE t1 range c2d c2d 5 NULL 32 Using where; Using index

mysql-test/suite/innodb/r/innodb_bug57252.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cardinality
2-
2
2+
1
33
Table Op Msg_type Msg_text
44
test.bug57252 analyze status Engine-independent statistics collected
55
test.bug57252 analyze status OK

0 commit comments

Comments
 (0)