| 1 |
# |
| 2 |
# WL#6737: Integrating InnoDB temporary tables with MySQL Optimizer. |
| 3 |
# |
| 4 |
|
| 5 |
SET @optimizer_switch_saved= @@optimizer_switch; |
| 6 |
SET @@optimizer_switch="derived_merge=off"; |
| 7 |
|
| 8 |
# use sql_buffer_result |
| 9 |
explain format = json select sql_buffer_result * from t1; |
| 10 |
select sql_buffer_result * from t1; |
| 11 |
explain format = json select sql_buffer_result * from t2; |
| 12 |
select sql_buffer_result * from t2; |
| 13 |
|
| 14 |
# |
| 15 |
# If order by is not simple order |
| 16 |
# If not (sort_order only uses fields from head table and the head table is not left join table) |
| 17 |
explain format = json select * from t1 left join t2 on t1.b = t2.b order by t1.a; |
| 18 |
select * from t1 left join t2 on t1.b = t2.b order by t1.a; |
| 19 |
|
| 20 |
# |
| 21 |
# If sorting by a sp or udf function due to their expensive and probably non-deterministic nature. |
| 22 |
explain format = json select * from t1 order by func1(a); |
| 23 |
select * from t1 order by func1(a); |
| 24 |
|
| 25 |
--source include/turn_off_only_full_group_by.inc |
| 26 |
|
| 27 |
# order by aggregate function |
| 28 |
explain format = json select a from t1 group by b order by sum(a); |
| 29 |
select a from t1 group by b order by sum(a); |
| 30 |
|
| 31 |
# order by aggregate -- In order to make it simple, we make group by index |
| 32 |
explain format = json select a from t7 group by b order by sum(a); |
| 33 |
select a from t7 group by b order by sum(a); |
| 34 |
|
| 35 |
# |
| 36 |
# a select with join and sql_big_result(only can work with group by or distinct) |
| 37 |
explain format = json select sql_big_result distinct * from t1, t2; |
| 38 |
--sorted_result |
| 39 |
select sql_big_result distinct * from t1, t2; |
| 40 |
explain format = json select sql_big_result t1.b from t1, t2 group by t1.b; |
| 41 |
--sorted_result |
| 42 |
select sql_big_result t1.b from t1, t2 group by t1.b; |
| 43 |
explain format = json select sql_big_result distinct t1.a from t1, t2 group by t1.a, t1.b; |
| 44 |
--sorted_result |
| 45 |
select sql_big_result distinct t1.a from t1, t2 group by t1.a, t1.b; |
| 46 |
|
| 47 |
# |
| 48 |
# a select with join and sorting |
| 49 |
explain format = json select t1.* from t1 inner join t2 on t1.a = t2.a order by t2.a; |
| 50 |
select t1.* from t1 inner join t2 on t1.a = t2.a order by t2.a; |
| 51 |
explain format = json select t1.* from t1, t2 where t1.a = t2.a order by t1.a; |
| 52 |
select t1.* from t1, t2 where t1.a = t2.a order by t1.a; |
| 53 |
|
| 54 |
# |
| 55 |
# a select with join and grouping that uses tmp table for grouping |
| 56 |
explain format = json select t1.* from t1 inner join t2 where t1.a = t2.a group by t1.a; |
| 57 |
select t1.* from t1 inner join t2 where t1.a = t2.a group by t1.a; |
| 58 |
explain format = json select t1.* from t1 inner join t2 where t1.a = t2.a group by t2.a; |
| 59 |
select t1.* from t1 inner join t2 where t1.a = t2.a group by t2.a; |
| 60 |
|
| 61 |
# |
| 62 |
# there is an order by clause and a different group by clause |
| 63 |
explain format = json select a from t1 group by a order by a; |
| 64 |
select a from t1 group by a order by a; |
| 65 |
explain format = json select b from t1 group by b order by a; |
| 66 |
select b from t1 group by b order by a; |
| 67 |
|
| 68 |
# |
| 69 |
# select distinct that uses tmp table for duplicate elimination |
| 70 |
explain format = json select distinct t1.a from t1, t2 where t1.b = t2.b; |
| 71 |
select distinct t1.a from t1, t2 where t1.b = t2.b; |
| 72 |
|
| 73 |
# |
| 74 |
# remove_dup_with_hash_index |
| 75 |
explain format = json select distinct a from t1 group by b,a; |
| 76 |
--sorted_result |
| 77 |
select distinct a from t1 group by b,a; |
| 78 |
|
| 79 |
# |
| 80 |
# remove_dup_with_compare |
| 81 |
set @old_sort_buffer_size = @@sort_buffer_size; |
| 82 |
set @@sort_buffer_size = 32804; |
| 83 |
explain format = json select distinct b from t3 group by a having count(*) > 1; |
| 84 |
--sorted_result |
| 85 |
select distinct b from t3 group by a having count(*) > 1; |
| 86 |
--sorted_result |
| 87 |
select distinct b from t3 group by a; |
| 88 |
set @@sort_buffer_size = @old_sort_buffer_size; |
| 89 |
|
| 90 |
# |
| 91 |
# special case for distinct |
| 92 |
explain format = json select distinct t4.a,e from t4, t1 order by t4.b; |
| 93 |
select distinct t4.a,e from t4, t1 order by t4.b; |
| 94 |
|
| 95 |
# |
| 96 |
# union |
| 97 |
explain format = json select * from t1 union all select * from t2; |
| 98 |
select * from t1 union all select * from t2; |
| 99 |
|
| 100 |
# union distinct with global order by filesort |
| 101 |
explain format = json select * from t1 union select * from t2 order by b; |
| 102 |
select * from t1 union select * from t2 order by b; |
| 103 |
|
| 104 |
# |
| 105 |
# select with a single derived table |
| 106 |
explain format = json select tt.* from (select * from t1) as tt; |
| 107 |
select tt.* from (select * from t1) as tt; |
| 108 |
# Cost estimates are unstable since t5 pages may have been flushed from buffer |
| 109 |
# pool since table was created. Do a table scan to reload pages. |
| 110 |
select * from t5; |
| 111 |
# select with a derived table that builds index over the derived table |
| 112 |
explain format = json select b from (select b from t5 where b > 5) tt; |
| 113 |
select b from (select b from t5 where b > 5) tt; |
| 114 |
# select with mutiple derived tables |
| 115 |
--sorted_result |
| 116 |
explain format = json select * from (select b from t1) as t1, (select b from t2) as t2; |
| 117 |
--sorted_result |
| 118 |
select * from (select b from t1) as t1, (select b from t2) as t2; |
| 119 |
# derived table uses 'ref' scan |
| 120 |
explain format = json select * from t1, (select * from t6) as t where t.a =5; |
| 121 |
select * from t1, (select * from t6) as t where t.a =5; |
| 122 |
|
| 123 |
# |
| 124 |
# materialized semi-join |
| 125 |
explain format = json select * from t1 where t1.a in (select a from t2) ; |
| 126 |
select * from t1 where t1.a in (select a from t2) ; |
| 127 |
# tmp table for semi-join weedout |
| 128 |
if (`select locate('materialization', @@optimizer_switch) > 0`) |
| 129 |
{ |
| 130 |
set optimizer_switch='materialization=off'; |
| 131 |
} |
| 132 |
if (`select locate('firstmatch', @@optimizer_switch) > 0`) |
| 133 |
{ |
| 134 |
set optimizer_switch='firstmatch=off'; |
| 135 |
} |
| 136 |
if (`select locate('loosescan', @@optimizer_switch) > 0`) |
| 137 |
{ |
| 138 |
set optimizer_switch='loosescan=off'; |
| 139 |
} |
| 140 |
explain format = json select * from t1 where t1.a in (select a from t2) ; |
| 141 |
select * from t1 where t1.a in (select a from t2) ; |
| 142 |
set optimizer_switch='default,derived_merge=off'; |
| 143 |
|
| 144 |
# |
| 145 |
# muti-update |
| 146 |
if (!`select @@innodb_read_only`) |
| 147 |
{ |
| 148 |
explain format = json update t1, t2 set t1.a = 9, t2.a = 10 where t1.b = 'a'; |
| 149 |
update t1, t2 set t1.a = 9, t2.a = 10 where t1.b = 'a'; |
| 150 |
} |
| 151 |
|
| 152 |
# |
| 153 |
# count with distinct |
| 154 |
explain format = json select count(distinct a) from t1; |
| 155 |
select count(distinct a) from t1; |
| 156 |
|
| 157 |
# |
| 158 |
# group_concat |
| 159 |
explain format = json select b, group_concat(a) from t1; |
| 160 |
select b, group_concat(a) from t1; |
| 161 |
|
| 162 |
# |
| 163 |
# quick group |
| 164 |
select * from t1; |
| 165 |
explain format = json select b, count(a), sum(a), avg(a), std(a), |
| 166 |
min(a), max(a), bit_and(a), bit_or(a) from t1 group by b; |
| 167 |
select b, count(a), sum(a), avg(a), std(a), min(a), |
| 168 |
max(a), bit_and(a), bit_or(a) from t1 group by b; |
| 169 |
|
| 170 |
# |
| 171 |
# choose innodb to create tmp table |
| 172 |
--disable_warnings |
| 173 |
select sql_big_result * from performance_schema.global_variables where variable_name like 'internal_tmp_mem_storage_engine'; |
| 174 |
--enable_warnings |
| 175 |
|
| 176 |
# |
| 177 |
# union distinct and all |
| 178 |
select 1 union distinct select 1 union all select 1; |
| 179 |
|
| 180 |
# |
| 181 |
# enable and disable indexes |
| 182 |
select alias1.`col_int_key` AS field1 from (t8 as alias1) where (5) in |
| 183 |
(select 7 union select 1) order by alias1.`col_varchar_key` asc; |
| 184 |
|
| 185 |
# |
| 186 |
# Exercises select from multiple non-temporary tables that result in |
| 187 |
# insert .... select kind of semantics on intrinsic temporary table. |
| 188 |
select count(table2.col_date_key) as field1 from |
| 189 |
(t10 as table1 inner join |
| 190 |
((t10 as table2 right join |
| 191 |
t9 as table3 on |
| 192 |
((table3.pk = table2.pk) or |
| 193 |
(table3.pk = table2.col_int_key)))) |
| 194 |
on ((table3.pk < table2.col_int_nokey))) |
| 195 |
order by field1 ASC; |
| 196 |
|
| 197 |
# |
| 198 |
# trying to create index on blob column |
| 199 |
select distinct table1.col_varchar_1024_utf8_key as field1 |
| 200 |
from t11 as table1 left join t11 as table2 on |
| 201 |
table1.col_varchar_1024_utf8 = table2.col_varchar_1024_latin1_key |
| 202 |
order by field1; |
| 203 |
|
| 204 |
# |
| 205 |
# nulls_equal use-case |
| 206 |
select t from t12 group by c; |
| 207 |
|
| 208 |
# |
| 209 |
# complex queries resulting in multiple intrinsic temporary tables |
| 210 |
select count(outr.col_varchar_nokey) AS x |
| 211 |
from t13 as outr2 left join |
| 212 |
(select * from t13) as outr |
| 213 |
on (outr2.col_time_key > outr.col_date_key) |
| 214 |
where outr.pk in |
| 215 |
(select innr.col_int_nokey AS y from |
| 216 |
(select * from t14) as innr2 left join |
| 217 |
(select * from t14) AS innr |
| 218 |
ON (innr2.col_int_nokey = innr.col_int_key) |
| 219 |
where innr.col_varchar_nokey > innr.col_varchar_key |
| 220 |
or not outr.pk > 9) |
| 221 |
or outr.col_varchar_key is null; |
| 222 |
|
| 223 |
--source include/restore_sql_mode_after_turn_off_only_full_group_by.inc |
| 224 |
|
| 225 |
SET @@optimizer_switch=@optimizer_switch_saved; |