summaryrefslogtreecommitdiff
path: root/mysql-test/t/selectivity_no_engine.test
blob: d1e34a7ac6f0008cba2ba4dab738ec0adefce8ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
--source include/have_stat_tables.inc

--echo #
--echo #  Engine-agnostic tests for statistics-based selectivity calculations.
--echo #   - selectivity tests that depend on the engine should go into
--echo #     t/selectivity.test. That test is run with myisam/innodb/xtradb.
--echo #   - this file is for tests that don't depend on the engine. 
--echo #

--disable_warnings
drop table if exists t0,t1,t2,t3;
--enable_warnings

select @@global.use_stat_tables;
select @@session.use_stat_tables;

set @save_use_stat_tables=@@use_stat_tables;

set use_stat_tables='preferably';

set @save_optimizer_use_condition_selectivity=@@optimizer_use_condition_selectivity;
set @save_histogram_size=@@histogram_size;
set @save_histogram_type=@@histogram_type;

--echo #
--echo # MDEV-5917: EITS: different order of predicates in IN (...) causes different estimates
--echo #
create table t1(a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t2 (col1 int);
--echo # one value in 1..100 range
insert into t2 select A.a + B.a*10 from t1 A, t1 B;
--echo # ten values in 100...200 range
insert into t2 select 100 + A.a + B.a*10 from t1 A, t1 B, t1 C;

set histogram_type='SINGLE_PREC_HB';
set histogram_size=100;
set optimizer_use_condition_selectivity=4;
analyze table t2 persistent for all;

--echo # The following two must have the same in 'Extra' column:
explain extended select * from t2 where col1 IN (20, 180);
explain extended select * from t2 where col1 IN (180, 20);

drop table t1, t2;

--echo #
--echo # MDEV-5926: EITS: Histogram estimates for column=least_possible_value are wrong
--echo #
create table t0(a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1(a int);
insert into t1 select A.a from t0 A, t0 B, t0 C;
set histogram_size=20;
set histogram_type='single_prec_hb';
analyze table t1 persistent for all;
set use_stat_tables='preferably';
set optimizer_use_condition_selectivity=4;
--echo # Should select about 10%:
explain extended select * from t1 where a=2;
--echo # Should select about 10%:
explain extended select * from t1 where a=1;
--echo # Must not have filtered=100%:
explain extended select * from t1 where a=0;
--echo # Again, must not have filtered=100%:
explain extended select * from t1 where a=-1;

drop table t0, t1;

--echo #
--echo # MDEV-4362: Selectivity estimates for IN (...) do not depend on whether the values are in range
--echo #
create table t1 (col1 int);
set @a=-1;
create table t2 (a int)  select (@a:=@a+1) as a from information_schema.session_variables A limit 100;
insert into t1 select A.a from t2 A, t2 B where A.a < 100 and B.a < 100;
select min(col1), max(col1), count(*) from t1;
set histogram_size=100;
analyze table t1 persistent for all;
explain extended select * from t1 where col1 in (1,2,3);
--echo # Must not cause fp division by zero, or produce nonsense numbers:
explain extended select * from t1 where col1 in (-1,-2,-3);
explain extended select * from t1 where col1<=-1;
drop table t1, t2;

--echo # 
--echo # MDEV-5984: EITS: Incorrect filtered% value for single-table select with range access
--echo # 
create table t1(a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t2 (a int, b int, col1 varchar(64), col2 varchar(64), key(a,b));
insert into t2 select A.a+10*B.a, C.a+10*D.a, 'filler-data1', 'filler-data2' from t1 A, t1 B, t1 C, t1 D;

set histogram_size=100;
set optimizer_use_condition_selectivity=4;
set use_stat_tables='preferably';
analyze table t2 persistent for all;
--echo # This must show filtered=100%:
explain extended select * from t2 where a in (1,2,3) and b in (1,2,3);

drop table t2, t1;

--echo # 
--echo # MDEV-5980: EITS: if condition is used for REF access, its selectivity is still in filtered%
--echo # 
create table t0(a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1(key1 int, col1 int, key(key1));
insert into t1 select A.a, A.a from t0 A, t0 B, t0 C;

set histogram_size=100;
set use_stat_tables='preferably';
set optimizer_use_condition_selectivity=4;
analyze table t1 persistent for all;
--echo # 10% is ok
explain extended select * from t1 where col1=2;
--echo # Must show 100%, not 10%
explain extended select * from t1 where key1=2;
drop table t0, t1;

##--disable_parsing
--echo # 
--echo # MDEV-5985: EITS: selectivity estimates look illogical for join and non-key equalities
--echo # 
create table t1(a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t2(a int);
insert into t2 select A.a + B.a* 10 + C.a * 100 from t1 A, t1 B, t1 C;
create table t3 as select * from t2;
set histogram_size=100;
set use_stat_tables='preferably';
set optimizer_use_condition_selectivity=4;
analyze table t2 persistent for all;
analyze table t3 persistent for all;
explain extended select * from t2 A where A.a < 40;
explain extended select * from t3 B where B.a < 100;
explain extended select * from t2 A, t3 B where A.a < 40 and B.a < 100;
explain extended select * from t2 A, t3 B where A.a < 40 and B.a < 100 and B.a=A.a;
drop table t1, t2, t3;
## --enable_parsing
select 1;
##--disable_parsing
--echo # 
--echo # MDEV-6003: EITS: ref access, keypart2=const vs keypart2=expr - inconsistent filtered% value
--echo # 
create table t0(a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (
  kp1 int, kp2 int, 
  filler1 char(100),
  filler2 char(100),
  key(kp1, kp2)
);

insert into t1 
select 
  A.a,
  B.a,
  'filler-data-1',
  'filler-data-2'
from t0 A, t0 B, t0 C;
set histogram_size=100;
set use_stat_tables='preferably';
set optimizer_use_condition_selectivity=4;
analyze table t1 persistent for all;
--echo # NOTE: 10*100%, 10*100% rows is ok
explain extended select * from t0, t1 where t1.kp1=t0.a and t1.kp2=t0.a+1;

--echo # NOTE: t0: 10*100% is ok,  t1: 10*9.90% is bad. t1 should have 10*100%.
explain extended select * from t0, t1 where t1.kp1=t0.a and t1.kp2=4;
drop table t0, t1;
##--enable_parsing

--echo # 
--echo # End of the test file
--echo # 

set use_stat_tables= @save_use_stat_tables;
set histogram_type=@save_histogram_type;
set histogram_size=@save_histogram_size;
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;