summaryrefslogtreecommitdiff
path: root/mysql-test/main/analyze_stmt_orderby.result
blob: 23f4c39dd5bd0a3acc8793e097ce3ffedb87d25e (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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
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 + B.a* 10 + C.a * 100 from t0 A, t0 B, t0 C;
create table t2 (
a int, 
b int, 
key (a)
);
insert into t2 select A.a*1000 + B.a, A.a*1000 + B.a from t0 A, t1 B;
#
# Try an UPDATE that uses filesort:
#
explain 
update t2 set b=b+1 order by b limit 5;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	10000	Using filesort
explain format=json
update t2 set b=b+1 order by b limit 5;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "filesort": {
      "table": {
        "update": 1,
        "table_name": "t2",
        "access_type": "ALL",
        "rows": 10000
      }
    }
  }
}
analyze format=json
update t2 set b=b+1 order by b limit 5;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_total_time_ms": "REPLACED",
    "filesort": {
      "r_loops": 1,
      "r_total_time_ms": "REPLACED",
      "r_limit": 5,
      "r_used_priority_queue": true,
      "r_output_rows": 6,
      "table": {
        "update": 1,
        "table_name": "t2",
        "access_type": "ALL",
        "rows": 10000,
        "r_rows": 10000,
        "r_filtered": 100,
        "r_total_time_ms": "REPLACED"
      }
    }
  }
}
#
# Try an UPDATE that uses buffering:
#
explain
update t2 set a=a+1 where a<10;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	range	a	a	5	NULL	9	Using where; Using buffer
explain format=json
update t2 set a=a+1 where a<10;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "buffer": {
      "table": {
        "update": 1,
        "table_name": "t2",
        "access_type": "range",
        "possible_keys": ["a"],
        "key": "a",
        "key_length": "5",
        "used_key_parts": ["a"],
        "rows": 9,
        "attached_condition": "t2.a < 10"
      }
    }
  }
}
analyze format=json
update t2 set a=a+1 where a<10;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_total_time_ms": "REPLACED",
    "buffer": {
      "table": {
        "update": 1,
        "table_name": "t2",
        "access_type": "range",
        "possible_keys": ["a"],
        "key": "a",
        "key_length": "5",
        "used_key_parts": ["a"],
        "rows": 9,
        "r_rows": 10,
        "r_filtered": 100,
        "r_total_time_ms": "REPLACED",
        "attached_condition": "t2.a < 10"
      }
    }
  }
}
#
# Try a DELETE that uses filesort:
#
explain 
delete from t2 order by b limit 5;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	10000	Using filesort
explain format=json
delete from t2 order by b limit 5;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "filesort": {
      "table": {
        "delete": 1,
        "table_name": "t2",
        "access_type": "ALL",
        "rows": 10000
      }
    }
  }
}
analyze format=json
delete from t2 order by b limit 5;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_total_time_ms": "REPLACED",
    "filesort": {
      "r_loops": 1,
      "r_total_time_ms": "REPLACED",
      "r_used_priority_queue": false,
      "r_output_rows": 10000,
      "r_buffer_size": "REPLACED",
      "table": {
        "delete": 1,
        "table_name": "t2",
        "access_type": "ALL",
        "rows": 10000,
        "r_rows": 10000,
        "r_filtered": 100,
        "r_total_time_ms": "REPLACED"
      }
    }
  }
}
#
# Try a SELECT with QEP in form: filesort { tmp_table { join } } 
#
explain
select * from t0,t2 where t2.a=t0.a order by t2.b limit 4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t0	ALL	NULL	NULL	NULL	NULL	10	Using where; Using temporary; Using filesort
1	SIMPLE	t2	ref	a	a	5	test.t0.a	1	
explain format=json
select * from t0,t2 where t2.a=t0.a order by t2.b limit 4;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "filesort": {
      "sort_key": "t2.b",
      "temporary_table": {
        "table": {
          "table_name": "t0",
          "access_type": "ALL",
          "rows": 10,
          "filtered": 100,
          "attached_condition": "t0.a is not null"
        },
        "table": {
          "table_name": "t2",
          "access_type": "ref",
          "possible_keys": ["a"],
          "key": "a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t0.a"],
          "rows": 1,
          "filtered": 100
        }
      }
    }
  }
}
analyze format=json
select * from t0,t2 where t2.a=t0.a order by t2.b limit 4;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_loops": 1,
    "r_total_time_ms": "REPLACED",
    "filesort": {
      "sort_key": "t2.b",
      "r_loops": 1,
      "r_total_time_ms": "REPLACED",
      "r_limit": 4,
      "r_used_priority_queue": true,
      "r_output_rows": 4,
      "temporary_table": {
        "table": {
          "table_name": "t0",
          "access_type": "ALL",
          "r_loops": 1,
          "rows": 10,
          "r_rows": 10,
          "r_total_time_ms": "REPLACED",
          "filtered": 100,
          "r_filtered": 100,
          "attached_condition": "t0.a is not null"
        },
        "table": {
          "table_name": "t2",
          "access_type": "ref",
          "possible_keys": ["a"],
          "key": "a",
          "key_length": "5",
          "used_key_parts": ["a"],
          "ref": ["test.t0.a"],
          "r_loops": 10,
          "rows": 1,
          "r_rows": 0.4,
          "r_total_time_ms": "REPLACED",
          "filtered": 100,
          "r_filtered": 100
        }
      }
    }
  }
}
#
# Try a SELECT with QEP in form: join { filesort { table0 }, table2 }
#
explain
select * from t0,t2 where t2.a=t0.a order by t0.a limit 4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t0	ALL	NULL	NULL	NULL	NULL	10	Using where; Using filesort
1	SIMPLE	t2	ref	a	a	5	test.t0.a	1	
explain format=json
select * from t0,t2 where t2.a=t0.a order by t0.a limit 4;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "read_sorted_file": {
      "filesort": {
        "sort_key": "t0.a",
        "table": {
          "table_name": "t0",
          "access_type": "ALL",
          "rows": 10,
          "filtered": 100,
          "attached_condition": "t0.a is not null"
        }
      }
    },
    "table": {
      "table_name": "t2",
      "access_type": "ref",
      "possible_keys": ["a"],
      "key": "a",
      "key_length": "5",
      "used_key_parts": ["a"],
      "ref": ["test.t0.a"],
      "rows": 1,
      "filtered": 100
    }
  }
}
analyze format=json 
select * from t0,t2 where t2.a=t0.a order by t0.a limit 4;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_loops": 1,
    "r_total_time_ms": "REPLACED",
    "read_sorted_file": {
      "r_rows": 10,
      "filesort": {
        "sort_key": "t0.a",
        "r_loops": 1,
        "r_total_time_ms": "REPLACED",
        "r_used_priority_queue": false,
        "r_output_rows": 10,
        "r_buffer_size": "REPLACED",
        "table": {
          "table_name": "t0",
          "access_type": "ALL",
          "r_loops": 1,
          "rows": 10,
          "r_rows": 10,
          "r_total_time_ms": "REPLACED",
          "filtered": 100,
          "r_filtered": 100,
          "attached_condition": "t0.a is not null"
        }
      }
    },
    "table": {
      "table_name": "t2",
      "access_type": "ref",
      "possible_keys": ["a"],
      "key": "a",
      "key_length": "5",
      "used_key_parts": ["a"],
      "ref": ["test.t0.a"],
      "r_loops": 10,
      "rows": 1,
      "r_rows": 0.4,
      "r_total_time_ms": "REPLACED",
      "filtered": 100,
      "r_filtered": 100
    }
  }
}
drop table t2;
create table t2 (
a int,
b int,
c int
);
insert into t2 
select 
a.a+10*b.a+100*c.a, 
b.a+10*c.a, 
c.a
from t0 a, t0 b, t0 c;
analyze format=json 
select MAX(b) from t2 where mod(a,2)=0 group by c;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_loops": 1,
    "r_total_time_ms": "REPLACED",
    "filesort": {
      "sort_key": "t2.c",
      "r_loops": 1,
      "r_total_time_ms": "REPLACED",
      "r_used_priority_queue": false,
      "r_output_rows": 10,
      "r_buffer_size": "REPLACED",
      "temporary_table": {
        "table": {
          "table_name": "t2",
          "access_type": "ALL",
          "r_loops": 1,
          "rows": 1000,
          "r_rows": 1000,
          "r_total_time_ms": "REPLACED",
          "filtered": 100,
          "r_filtered": 50,
          "attached_condition": "t2.a MOD 2 = 0"
        }
      }
    }
  }
}
drop table t2;
#
#  MDEV-8282: crash in filesort() with simple ordered delete
#
create table t3(a int) engine=innodb;
delete from t3 order by a;
# EXPLAIN thinks it will use delete_all_rows():
explain
delete from t3 order by a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	1	Deleting all rows
# ANALYZE shows that delete_all_rows() didn't work and we deleted rows
# one-by-one:
analyze
delete from t3 order by a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	r_rows	filtered	r_filtered	Extra
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	1	0.00	100.00	100.00	Using filesort
drop table t3;
#
# A test for duplicate_removal()
#
create table t3 (a int, b int);
insert into t3 select a, 123 from t0;
analyze format=json
select distinct max(t3.b) Q from t0, t3 where t0.a=t3.a group by t0.a order by null;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_loops": 1,
    "r_total_time_ms": "REPLACED",
    "duplicate_removal": {
      "temporary_table": {
        "table": {
          "table_name": "t0",
          "access_type": "ALL",
          "r_loops": 1,
          "rows": 10,
          "r_rows": 10,
          "r_total_time_ms": "REPLACED",
          "filtered": 100,
          "r_filtered": 100
        },
        "block-nl-join": {
          "table": {
            "table_name": "t3",
            "access_type": "ALL",
            "r_loops": 1,
            "rows": 10,
            "r_rows": 10,
            "r_total_time_ms": "REPLACED",
            "filtered": 100,
            "r_filtered": 100
          },
          "buffer_type": "flat",
          "buffer_size": "65",
          "join_type": "BNL",
          "attached_condition": "t3.a = t0.a",
          "r_filtered": 10
        }
      }
    }
  }
}
#
# A query with two filesort calls:
#   - first is needed to do group-by-group grouping to calculate COUNT(DISTINCT)
#   - the second is need to produce ORDER BY.
# (see MDEV-7836 for description of the query plan)
create table t5 (a int , b int) ;
create table t6 like t5 ;
create table t7 like t5 ;
insert into t5 values (0, 100), (1, 2), (1, 3), (2, 2), (2, 7),
(2, -1), (3, 10);
insert into t6 values (0, 0), (1, 1), (2, 1), (3, 1), (4, 1);
insert into t7 values (3, 3), (2, 2), (1, 1);
# TODO: This ANALYZE output doesn't make it clear what is used for what.
analyze format=json
select count(distinct t5.b) as sum from t5, t6
where t5.a=t6.a and t6.b > 0 and t5.a <= 5
group by t5.a order by sum limit 1;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_loops": 1,
    "r_total_time_ms": "REPLACED",
    "filesort": {
      "sort_key": "count(distinct t5.b)",
      "r_loops": 1,
      "r_total_time_ms": "REPLACED",
      "r_limit": 1,
      "r_used_priority_queue": true,
      "r_output_rows": 2,
      "temporary_table": {
        "filesort": {
          "sort_key": "t5.a",
          "r_loops": 1,
          "r_total_time_ms": "REPLACED",
          "r_used_priority_queue": false,
          "r_output_rows": 6,
          "r_buffer_size": "REPLACED",
          "temporary_table": {
            "table": {
              "table_name": "t6",
              "access_type": "ALL",
              "r_loops": 1,
              "rows": 5,
              "r_rows": 5,
              "r_total_time_ms": "REPLACED",
              "filtered": 100,
              "r_filtered": 80,
              "attached_condition": "t6.b > 0 and t6.a <= 5"
            },
            "block-nl-join": {
              "table": {
                "table_name": "t5",
                "access_type": "ALL",
                "r_loops": 1,
                "rows": 7,
                "r_rows": 7,
                "r_total_time_ms": "REPLACED",
                "filtered": 100,
                "r_filtered": 100
              },
              "buffer_type": "flat",
              "buffer_size": "119",
              "join_type": "BNL",
              "attached_condition": "t5.a = t6.a",
              "r_filtered": 21.429
            }
          }
        }
      }
    }
  }
}
explain format=json
select count(distinct t5.b) as sum from t5, t6
where t5.a=t6.a and t6.b > 0 and t5.a <= 5
group by t5.a order by sum limit 1;
EXPLAIN
{
  "query_block": {
    "select_id": 1,
    "filesort": {
      "sort_key": "count(distinct t5.b)",
      "temporary_table": {
        "filesort": {
          "sort_key": "t5.a",
          "temporary_table": {
            "table": {
              "table_name": "t6",
              "access_type": "ALL",
              "rows": 5,
              "filtered": 100,
              "attached_condition": "t6.b > 0 and t6.a <= 5"
            },
            "block-nl-join": {
              "table": {
                "table_name": "t5",
                "access_type": "ALL",
                "rows": 7,
                "filtered": 100
              },
              "buffer_type": "flat",
              "buffer_size": "119",
              "join_type": "BNL",
              "attached_condition": "t5.a = t6.a"
            }
          }
        }
      }
    }
  }
}
drop table t5,t6,t7;
drop table t3;
# 
# Tabular ANALYZE must get its data from execution tracker (and not from
#   the query plan)
# 
CREATE TABLE t2(
col1 int, 
col2 int, 
UNIQUE INDEX idx (col1, col2)) engine=myisam;
INSERT INTO t2(col1, col2) VALUES
(1,20),(2,19),(3,18),(4,17),(5,16),(6,15),(7,14),(8,13),(9,12),(10,11),
(11,10),(12,9),(13,8),(14,7),(15,6),(16,5),(17,4),(18,3),(19,2),(20,1);
flush status;
explain
select col1 f1, col2 f2, col1 f3 from t2 group by f1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	range	NULL	idx	5	NULL	7	Using index for group-by
analyze
select col1 f1, col2 f2, col1 f3 from t2 group by f1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	r_rows	filtered	r_filtered	Extra
1	SIMPLE	t2	range	NULL	idx	5	NULL	7	20.00	100.00	100.00	Using index for group-by
analyze format=json
select col1 f1, col2 f2, col1 f3 from t2 group by f1;
ANALYZE
{
  "query_block": {
    "select_id": 1,
    "r_loops": 1,
    "r_total_time_ms": "REPLACED",
    "table": {
      "table_name": "t2",
      "access_type": "range",
      "key": "idx",
      "key_length": "5",
      "used_key_parts": ["col1"],
      "r_loops": 1,
      "rows": 7,
      "r_rows": 20,
      "r_total_time_ms": "REPLACED",
      "filtered": 100,
      "r_filtered": 100,
      "using_index_for_group_by": true
    }
  }
}
drop table t2;
drop table t0,t1;