File: vcol_sargable.result

package info (click to toggle)
mariadb 1%3A11.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 765,428 kB
  • sloc: ansic: 2,382,827; cpp: 1,803,532; asm: 378,315; perl: 63,176; sh: 46,496; pascal: 40,776; java: 39,363; yacc: 20,428; python: 19,506; sql: 17,864; xml: 12,463; ruby: 8,544; makefile: 6,059; cs: 5,855; ada: 1,700; lex: 1,193; javascript: 1,039; objc: 80; tcl: 73; awk: 46; php: 22
file content (401 lines) | stat: -rw-r--r-- 14,316 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Check various condition types
create table t1 (a int, vcol1 int as (a+1), index(vcol1));
insert into t1 (a) select seq from seq_1_to_100;
explain select * from t1 where a+1=2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	vcol1	vcol1	5	const	1	
# Try renaming the table
explain select * from t1 as TBL where TBL.a+1=2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	TBL	ref	vcol1	vcol1	5	const	1	
explain select * from t1 where a+1<=2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	1	Using index condition
explain select * from t1 where a+1<2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	1	Using index condition
explain select * from t1 where a+1>100;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	6	Using index condition
explain select * from t1 where a+1>=100;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	13	Using index condition
explain select * from t1 where a+1 between 10 and 12;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	2	Using index condition
explain select * from t1 where (a+1) IS NULL;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	vcol1	vcol1	5	const	1	Using index condition
explain select * from t1 force index(vcol1) where (a+1) IS NOT NULL;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	100	Using index condition
explain select * from t1 where (a+1) in (1,2,3,4);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	4	Using index condition
# Check UPDATE/DELETE:
explain delete from t1 where a+1=2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	1	Using where
explain update t1 set a=a+1 where a+1=2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	vcol1	vcol1	5	NULL	1	Using where; Using buffer
# Try merged VIEWs:
create view v1 as select * from t1;
explain select * from v1 where a+1=2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	vcol1	vcol1	5	const	1	
create view v2 as select a as A_COL from t1;
explain select * from v2 where A_COL+1=2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	vcol1	vcol1	5	const	1	
drop view v1;
drop view v2;
set names utf8mb4;
select @@collation_connection;
@@collation_connection
utf8mb4_uca1400_ai_ci
# Check VARCHAR
create table t2 (
a varchar(32),
vcol1 varchar(32) as (concat('hello-',a)),
index(vcol1)
);
insert into t2 (a) select seq from seq_1_to_100;
select collation('aaa'), collation(vcol1) from t2 limit 1;
collation('aaa')	collation(vcol1)
utf8mb4_uca1400_ai_ci	utf8mb4_uca1400_ai_ci
set @tmp_trace=@@optimizer_trace;
set optimizer_trace=1;
# This won't work:
explain select * from t2 where concat('bye-', a)='hello-5';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	100	Using where
# This will work:
explain select * from t2 where concat('hello-', a)='hello-5';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ref	vcol1	vcol1	131	const	1	Using index condition
select
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
from
information_schema.optimizer_trace;
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
[
    {
        "condition": "WHERE",
        "resulting_condition": "t2.vcol1 = 'hello-5'"
    }
]
# Try also ON expressions
explain
select *
from t1 left join t2 on concat('hello-', t2.a)='hello-5'
where
t1.a+1=2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	vcol1	vcol1	5	const	1	
1	SIMPLE	t2	ref	vcol1	vcol1	131	const	1	Using where
select
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
from
information_schema.optimizer_trace;
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
[
    {
        "condition": "WHERE",
        "resulting_condition": "t1.vcol1 = 2"
    },
    {
        "condition": "ON expression",
        "resulting_condition": "t2.vcol1 = 'hello-5'"
    }
]
create table t3 (a int);
insert into t3 values (1),(2);
explain
select *
from
t3 left join
(t1 join t2 on concat('hello-', t2.a)='hello-5' and t1.a+1=2)
on t3.a<3;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	2	
1	SIMPLE	t1	ref	vcol1	vcol1	5	const	1	Using where
1	SIMPLE	t2	ref	vcol1	vcol1	131	const	1	Using where
select
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
from
information_schema.optimizer_trace;
json_detailed(json_extract(trace, '$**.virtual_column_substitution'))
[
    {
        "condition": "ON expression",
        "resulting_condition": "t3.a < 3 and t2.vcol1 = 'hello-5' and t1.vcol1 = 2"
    }
]
drop table t1,t2,t3;
set optimizer_trace=@tmp_trace;
#
# Implicit type/charset conversions
#
create table t3 (
a varchar(32) collate utf8mb4_general_ci,
vcol1 int as (concat('100',a)),
vcol2 varchar(32) collate utf8mb4_unicode_ci as (concat('hello-',a)),
index(vcol1),
index(vcol2)
);
insert into t3 (a) select seq from seq_1_to_100;
# Type conversion
explain select * from t3 where concat('100', a)=10010;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	100	Using where
Warnings:
Note	1105	Cannot substitute virtual column expression concat('100',`t3`.`a`) -> vcol1 due to type mismatch
# Character set change
explain select * from t3 where concat('hello-', a)='abcd';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	100	Using where
Warnings:
Note	1105	Cannot substitute virtual column expression concat('hello-',`t3`.`a`) -> vcol2 due to collation mismatch
drop table t3;
# Try JSON_EXTRACT
create table t1 (a int, js1 blob);
insert into t1
select seq, concat('{"size":', seq, ', "color":"hue', seq ,'"}') from seq_1_to_100;
select * from t1 limit 3;
a	js1
1	{"size":1, "color":"hue1"}
2	{"size":2, "color":"hue2"}
3	{"size":3, "color":"hue3"}
alter table t1 add size1 int as (cast(json_extract(js1, '$.size') as int));
alter table t1 add index(size1);
explain select * from t1 where cast(json_extract(js1,'$.size') as int)=5 ;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	size1	size1	5	const	1	
alter table t1 add
color varchar(100) COLLATE utf8mb4_bin
as (json_unquote(json_extract(js1, '$.color')));
alter table t1 add index(color);
select * from t1 limit 3;
a	js1	size1	color
1	{"size":1, "color":"hue1"}	1	hue1
2	{"size":2, "color":"hue2"}	2	hue2
3	{"size":3, "color":"hue3"}	3	hue3
# Index is used:
explain select * from t1 where json_unquote(json_extract(js1, '$.color'))='hue5';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	color	color	403	const	1	Using index condition
explain select * from t1 where json_unquote(json_extract(js1, '$.color')) IS NULL;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	color	color	403	const	1	Using index condition
explain select * from t1 force index(color)
where json_unquote(json_extract(js1, '$.color')) IS NOT NULL;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	range	color	color	403	NULL	100	Using index condition
alter table t1 drop column color;
alter table t1 add
color2 varchar(100)
as (json_unquote(json_extract(js1, '$.color')));
alter table t1 add index(color2);
# Index is not used due to collation mismatch:
explain select * from t1 where json_unquote(json_extract(js1, '$.color'))='hue5';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	100	Using where
Warnings:
Note	1105	Cannot substitute virtual column expression json_unquote(json_extract(`t1`.`js1`,'$.color')) -> color2 due to collation mismatch
drop table t1;
#
# Tests with JSON_VALUE
#
create table t1 (a int, js1 json);
insert into t1
select seq, concat('{"size":', seq, ', "color":"hue', seq ,'"}') from seq_1_to_100;
select * from t1 limit 3;
a	js1
1	{"size":1, "color":"hue1"}
2	{"size":2, "color":"hue2"}
3	{"size":3, "color":"hue3"}
alter table t1
add size1 int as (cast(json_value(js1, '$.size') as int)),
add index(size1);
explain
select * from t1 where size1=10;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	size1	size1	5	const	1	
# The "JSON" datatype uses binary collation.
#  JSON_VALUE will produce output in binary collation, too:
select collation(json_value(js1, '$.color')) from t1 limit 1;
collation(json_value(js1, '$.color'))
utf8mb4_bin
# If one is fine with _bin comparisons, they can use index access:
alter table t1
add color1 varchar(100) collate utf8mb4_bin as (json_value(js1, '$.color')),
add index(color1);
explain select * from t1 where json_value(js1, '$.color')='hue10';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	color1	color1	403	const	1	Using index condition
alter table t1 drop column color1;
# Using different collation in column substitution prevents
# the optimization from working:
alter table t1
add color2 varchar(100) collate utf8mb4_unicode_ci as (json_value(js1, '$.color')),
add index(color2);
explain select * from t1 where json_value(js1, '$.color')='hue10';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	100	Using where
Warnings:
Note	1105	Cannot substitute virtual column expression json_value(`t1`.`js1`,'$.color') -> color2 due to collation mismatch
alter table t1 drop column color2;
# Explicitly specifying the collation helps:
alter table t1
add color3 varchar(100) collate utf8mb4_unicode_ci as
(json_value(js1, '$.color') collate utf8mb4_unicode_ci),
add index(color3);
explain select * from t1
where
json_value(js1, '$.color') collate utf8mb4_unicode_ci='hue10';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	color3	color3	403	const	1	Using index condition
#
# Alternatively, one can store JSON in a column with the same
# collation as default and then casts are not needed:
#
create table t2(
js1 longtext COLLATE utf8mb4_uca1400_ai_ci DEFAULT NULL CHECK (json_valid(`js1`))
);
insert into t2 select js1 from t1;
alter table t2
add color3 varchar(100) as (json_value(js1, '$.color')),
add index(color3);
explain select * from t2
where
json_value(js1, '$.color')='hue10';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ref	color3	color3	403	const	1	Using index condition
drop table t1,t2;
#
# Test interplay with sargable_casefold optimization:
#
create table t1 (
a varchar(100) collate utf8mb3_general_ci,
vcol varchar(100) collate utf8mb3_general_ci as (UPPER(a)),
index(a),
index(vcol)
);
insert into t1 (a) select seq from seq_1_to_100;
# Note that possible_keys doesn't include 'vcol'.
#  Sargable_casefold is applied before vcol substitution:
explain select * from t1 where UPPER(a)='abc';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	a	a	303	const	1	Using index condition
explain select * from t1 ignore index(vcol) where UPPER(a)='abc';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	a	a	303	const	1	Using index condition
explain select * from t1 ignore index(a) where UPPER(a)='abc';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	100	Using where
set optimizer_switch='sargable_casefold=off';
explain select * from t1 ignore index(a) where UPPER(a)='abc';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	vcol	vcol	303	const	1	Using index condition
set optimizer_switch=default;
drop table t1;
#
# Test interplay with Sargable YEAR/DATE optimization:
#
create table t1 (
a date,
vcol int as (year(a)),
index(a),
index(vcol)
);
insert into t1 (a) select date_add('2024-01-01', interval (seq*365) day) from seq_1_to_100;
# Note that possible_keys doesn't include 'vcol'.
#  Sargable Year is applied before vcol substitution:
explain format=json select * from t1 where year(a)=2025;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": 0.002574553,
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "range",
          "possible_keys": ["a"],
          "key": "a",
          "key_length": "4",
          "used_key_parts": ["a"],
          "loops": 1,
          "rows": 1,
          "cost": 0.002574553,
          "filtered": 100,
          "index_condition": "t1.a between '2025-01-01' and '2025-12-31'"
        }
      }
    ]
  }
}
# Check that vcol would work if Sargable Year didn't disable it:
alter table t1
add vcol2 int as (year(a)+1),
add index(vcol2);
explain format=json select * from t1 where year(a)+1=2025;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "cost": 0.002024411,
    "nested_loop": [
      {
        "table": {
          "table_name": "t1",
          "access_type": "ref",
          "possible_keys": ["vcol2"],
          "key": "vcol2",
          "key_length": "5",
          "used_key_parts": ["vcol2"],
          "ref": ["const"],
          "loops": 1,
          "rows": 1,
          "cost": 0.002024411,
          "filtered": 100
        }
      }
    ]
  }
}
drop table t1;
#
# MDEV-35833: Assertion `marked_for_read()' failed for query with vcols
#
CREATE TABLE t1 (
id int,
v2 int AS (id),
v3 int AS (id+0),
a1 int AS (v2 + v3),
KEY a1 (a1)
);
insert t1(id) values (1),(2);
explain format=json DELETE FROM t1 WHERE v2+v3 > 'a';
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "table": {
      "delete": 1,
      "table_name": "t1",
      "access_type": "range",
      "possible_keys": ["a1"],
      "key": "a1",
      "key_length": "5",
      "used_key_parts": ["a1"],
      "rows": 2,
      "attached_condition": "t1.a1 > 'a'"
    }
  }
}
Warnings:
Warning	1292	Truncated incorrect DECIMAL value: 'a'
drop table t1;