mirror of
https://github.com/MariaDB/server.git
synced 2025-08-07 00:04:31 +03:00
Also expand vcol field index coverings to include indexes covering all the fields in the expression. The reasoning goes as follows: let f(c1, c2, ..., cn) be a function on applied to columns c1, c2, ..., cn, if f(...) is covered by an index, so should vc whose expression is f(...). For example, if t.vf = t.c1 + t.c2, and t has three indexes (vf), (c1, c2), (c1). Before this change, vf's index covering is a singleton {(vf)}. Let's call that the "conventional" index covering. After this change vf's index covering is now {(vf), (c1, c2)}, since (c1, c2) covers both c1 and c2. Let's call (c1, c2) in this case the "extra" covering. With the coverings updated, when an index in the "extra" covering is chosen for keyread, the vcol also needs to be calculated. In this case we mark vcol in the table read_set, and ensure it is computed. With these changes, we see various improvements, including from using full table scan + filesort to full index scan + filesort when ORDER BY an indexed vcol (here vc = c + 1 is a vcol and both c and vc are indexes): explain select c + 1 from t order by vc; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t ALL NULL NULL NULL NULL 10000 Using filesort +1 SIMPLE t index NULL c 5 NULL 10000 Using index; Using filesort The substitutions are followed updates to all_fields which include a copy of the ORDER BY/GROUP BY item pointers, as well as corresponding updates to ref_pointer_array so that the all_fields and ref_pointer_array remain in sync. Another, related change is the recomputation of table index covering on substitutions. It not only reflects the correct table index covering after the substitutions, but also improve executions where the vcol index can be chosen, such as this example (here vc = c + 1 and vc is the only index in the table), from full table scan + filesort to full index scan: select vc from t order by c + 1; We do it in SELECT as well as in single table DELETE/UPDATE.
271 lines
9.2 KiB
Plaintext
271 lines
9.2 KiB
Plaintext
SET @@session.default_storage_engine = 'InnoDB';
|
|
SET @save_stats_persistent=@@GLOBAL.innodb_stats_persistent;
|
|
SET GLOBAL innodb_stats_persistent=0;
|
|
create table t1 (a int,
|
|
b int as (-a),
|
|
c int as (-a) persistent,
|
|
index (c));
|
|
insert into t1 (a) values (2), (1), (1), (3), (NULL);
|
|
create table t2 like t1;
|
|
insert into t2 (a) values (1);
|
|
create table t3 (a int primary key,
|
|
b int as (-a),
|
|
c int as (-a) persistent unique);
|
|
insert into t3 (a) values (2),(1),(3),(5),(4),(7);
|
|
# select_type=SIMPLE, type=system
|
|
select * from t2;
|
|
a b c
|
|
1 -1 -1
|
|
explain select * from t2;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t2 ALL NULL NULL NULL NULL 1
|
|
select * from t2 where c=-1;
|
|
a b c
|
|
1 -1 -1
|
|
explain select * from t2 where c=-1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t2 ref c c 5 const 1
|
|
# select_type=SIMPLE, type=ALL
|
|
select * from t1 where b=-1;
|
|
a b c
|
|
1 -1 -1
|
|
1 -1 -1
|
|
explain select * from t1 where b=-1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
|
|
# select_type=SIMPLE, type=const
|
|
select * from t3 where a=1;
|
|
a b c
|
|
1 -1 -1
|
|
explain select * from t3 where a=1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 const PRIMARY PRIMARY 4 const 1
|
|
# select_type=SIMPLE, type=range
|
|
select * from t3 where c>=-1;
|
|
a b c
|
|
1 -1 -1
|
|
explain select * from t3 where c>=-1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 range c c 5 NULL 1 Using where; Using index
|
|
# select_type=SIMPLE, type=ref
|
|
select * from t1,t3 where t1.c=t3.c and t3.c=-1;
|
|
a b c a b c
|
|
1 -1 -1 1 -1 -1
|
|
1 -1 -1 1 -1 -1
|
|
explain select * from t1,t3 where t1.c=t3.c and t3.c=-1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 const c c 5 const 1 Using index
|
|
1 SIMPLE t1 ref c c 5 const 2
|
|
# select_type=PRIMARY, type=index,ALL
|
|
select * from t1 where b in (select c from t3);
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
1 -1 -1
|
|
3 -3 -3
|
|
explain select * from t1 where b in (select c from t3);
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using where
|
|
1 PRIMARY t3 eq_ref c c 5 test.t1.b 1 Using index
|
|
# select_type=PRIMARY, type=range,ref
|
|
select * from t1 where c in (select c from t3 where c between -2 and -1);
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
1 -1 -1
|
|
explain select * from t1 where c in (select c from t3 where c between -2 and -1);
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 PRIMARY t3 range c c 5 NULL 2 Using where; Using index
|
|
1 PRIMARY t1 ref c c 5 test.t3.c 1
|
|
# select_type=UNION, type=system
|
|
# select_type=UNION RESULT, type=<union1,2>
|
|
select * from t1 union select * from t2;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
3 -3 -3
|
|
NULL NULL NULL
|
|
explain select * from t1 union select * from t2;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 PRIMARY t1 ALL NULL NULL NULL NULL 5
|
|
2 UNION t2 ALL NULL NULL NULL NULL 1
|
|
NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL
|
|
# select_type=DERIVED, type=system
|
|
set @tmp_optimizer_switch=@@optimizer_switch;
|
|
set optimizer_switch='derived_merge=off,derived_with_keys=off';
|
|
select * from (select a,b,c from t1) as t11;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
1 -1 -1
|
|
3 -3 -3
|
|
NULL NULL NULL
|
|
explain select * from (select a,b,c from t1) as t11;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 5
|
|
2 DERIVED t1 ALL NULL NULL NULL NULL 5
|
|
set optimizer_switch=@tmp_optimizer_switch;
|
|
###
|
|
### Using aggregate functions with/without DISTINCT
|
|
###
|
|
# SELECT COUNT(*) FROM tbl_name
|
|
select count(*) from t1;
|
|
count(*)
|
|
5
|
|
explain select count(*) from t1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 index NULL c 5 NULL 5 Using index
|
|
# SELECT COUNT(DISTINCT <non-vcol>) FROM tbl_name
|
|
select count(distinct a) from t1;
|
|
count(distinct a)
|
|
3
|
|
explain select count(distinct a) from t1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 5
|
|
# SELECT COUNT(DISTINCT <non-stored vcol>) FROM tbl_name
|
|
select count(distinct b) from t1;
|
|
count(distinct b)
|
|
3
|
|
explain select count(distinct b) from t1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 5
|
|
# SELECT COUNT(DISTINCT <stored vcol>) FROM tbl_name
|
|
select count(distinct c) from t1;
|
|
count(distinct c)
|
|
3
|
|
explain select count(distinct c) from t1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 range NULL c 5 NULL 5 Using index for group-by
|
|
###
|
|
### filesort & range-based utils
|
|
###
|
|
# SELECT * FROM tbl_name WHERE <vcol expr>
|
|
select * from t3 where c >= -2;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where c >= -2;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 range c c 5 NULL 2 Using where; Using index
|
|
# SELECT * FROM tbl_name WHERE <non-vcol expr>
|
|
select * from t3 where a between 1 and 2;
|
|
a b c
|
|
1 -1 -1
|
|
2 -2 -2
|
|
explain select * from t3 where a between 1 and 2;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 range PRIMARY PRIMARY 4 NULL 2 Using where
|
|
# SELECT * FROM tbl_name WHERE <non-indexed vcol expr>
|
|
select * from t3 where b between -2 and -1;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where b between -2 and -1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 index NULL c 5 NULL 6 Using where; Using index
|
|
# SELECT * FROM tbl_name WHERE <indexed vcol expr>
|
|
select * from t3 where c between -2 and -1;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where c between -2 and -1;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 range c c 5 NULL 2 Using where; Using index
|
|
# SELECT * FROM tbl_name WHERE <non-vcol expr> ORDER BY <non-indexed vcol>
|
|
select * from t3 where a between 1 and 2 order by b;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where a between 1 and 2 order by b;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 range PRIMARY PRIMARY 4 NULL 2 Using where; Using filesort
|
|
# SELECT * FROM tbl_name WHERE <non-vcol expr> ORDER BY <indexed vcol>
|
|
select * from t3 where a between 1 and 2 order by c;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where a between 1 and 2 order by c;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 index PRIMARY c 5 NULL 6 Using where; Using index
|
|
# SELECT * FROM tbl_name WHERE <non-indexed vcol expr> ORDER BY <non-vcol>
|
|
select * from t3 where b between -2 and -1 order by a;
|
|
a b c
|
|
1 -1 -1
|
|
2 -2 -2
|
|
explain select * from t3 where b between -2 and -1 order by a;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 index NULL PRIMARY 4 NULL 6 Using where
|
|
# SELECT * FROM tbl_name WHERE <non-indexed vcol expr> ORDER BY <non-indexed vcol>
|
|
select * from t3 where b between -2 and -1 order by b;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where b between -2 and -1 order by b;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 index NULL c 5 NULL 6 Using where; Using index; Using filesort
|
|
# SELECT * FROM tbl_name WHERE <indexed vcol expr> ORDER BY <non-indexed vcol>
|
|
select * from t3 where c between -2 and -1 order by b;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where c between -2 and -1 order by b;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 range c c 5 NULL 2 Using where; Using index; Using filesort
|
|
# SELECT * FROM tbl_name WHERE <non-indexed vcol expr> ORDER BY <indexed vcol>
|
|
select * from t3 where b between -2 and -1 order by c;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where b between -2 and -1 order by c;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 index NULL c 5 NULL 6 Using where; Using index
|
|
# SELECT * FROM tbl_name WHERE <indexed vcol expr> ORDER BY <indexed vcol>
|
|
select * from t3 where c between -2 and -1 order by c;
|
|
a b c
|
|
2 -2 -2
|
|
1 -1 -1
|
|
explain select * from t3 where c between -2 and -1 order by c;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t3 range c c 5 NULL 2 Using where; Using index
|
|
# SELECT sum(<non-indexed vcol>) FROM tbl_name GROUP BY <non-indexed vcol>
|
|
select sum(b) from t1 group by b;
|
|
sum(b)
|
|
NULL
|
|
-3
|
|
-2
|
|
-2
|
|
explain select sum(b) from t1 group by b;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort
|
|
# SELECT sum(<indexed vcol>) FROM tbl_name GROUP BY <indexed vcol>
|
|
select sum(c) from t1 group by c;
|
|
sum(c)
|
|
NULL
|
|
-3
|
|
-2
|
|
-2
|
|
explain select sum(c) from t1 group by c;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 index NULL c 5 NULL 5 Using index
|
|
# SELECT sum(<non-indexed vcol>) FROM tbl_name GROUP BY <indexed vcol>
|
|
select sum(b) from t1 group by c;
|
|
sum(b)
|
|
NULL
|
|
-3
|
|
-2
|
|
-2
|
|
explain select sum(b) from t1 group by c;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 index NULL c 5 NULL 5
|
|
# SELECT sum(<indexed vcol>) FROM tbl_name GROUP BY <non-indexed vcol>
|
|
select sum(c) from t1 group by b;
|
|
sum(c)
|
|
NULL
|
|
-3
|
|
-2
|
|
-2
|
|
explain select sum(c) from t1 group by b;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort
|
|
SET GLOBAL innodb_stats_persistent=@save_stats_persistent;
|