Skip to content

Commit 4938b82

Browse files
committed
MDEV-7836: ANALYZE FORMAT=JSON should provide info about GROUP/ORDER BY
Provide basic info about sorting/grouping done by the queries.
1 parent 66ff163 commit 4938b82

23 files changed

+1110
-180
lines changed

libmysqld/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ SET(SQL_EMBEDDED_SOURCES emb_qcache.cc libmysqld.c lib_sql.cc
102102
../sql/my_json_writer.cc ../sql/my_json_writer.h
103103
../sql/rpl_gtid.cc
104104
../sql/sql_explain.cc ../sql/sql_explain.h
105+
../sql/sql_analyze_stmt.cc ../sql/sql_analyze_stmt.h
105106
../sql/compat56.cc
106107
../sql/table_cache.cc
107108
../sql/item_inetfunc.cc

mysql-test/r/analyze_format_json.result

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ ANALYZE
281281
{
282282
"query_block": {
283283
"select_id": 1,
284+
"r_total_time_ms": "REPLACED",
284285
"table": {
285286
"update": 1,
286287
"table_name": "t1",
@@ -324,6 +325,7 @@ ANALYZE
324325
{
325326
"query_block": {
326327
"select_id": 1,
328+
"r_total_time_ms": "REPLACED",
327329
"table": {
328330
"delete": 1,
329331
"table_name": "t1",
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
create table t0(a int);
2+
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
3+
create table t1(a int);
4+
insert into t1 select A.a + B.a* 10 + C.a * 100 from t0 A, t0 B, t0 C;
5+
create table t2 (
6+
a int,
7+
b int,
8+
key (a)
9+
);
10+
insert into t2 select A.a*1000 + B.a, A.a*1000 + B.a from t0 A, t1 B;
11+
#
12+
# Try an UPDATE that uses filesort:
13+
#
14+
explain
15+
update t2 set b=b+1 order by b limit 5;
16+
id select_type table type possible_keys key key_len ref rows Extra
17+
1 SIMPLE t2 ALL NULL NULL NULL NULL 10000 Using filesort
18+
explain format=json
19+
update t2 set b=b+1 order by b limit 5;
20+
EXPLAIN
21+
{
22+
"query_block": {
23+
"select_id": 1,
24+
"filesort": {
25+
"table": {
26+
"update": 1,
27+
"table_name": "t2",
28+
"access_type": "ALL",
29+
"rows": 10000
30+
}
31+
}
32+
}
33+
}
34+
analyze format=json
35+
update t2 set b=b+1 order by b limit 5;
36+
ANALYZE
37+
{
38+
"query_block": {
39+
"select_id": 1,
40+
"r_total_time_ms": "REPLACED",
41+
"filesort": {
42+
"r_loops": 1,
43+
"r_limit": 5,
44+
"r_used_priority_queue": true,
45+
"r_output_rows": 6,
46+
"table": {
47+
"update": 1,
48+
"table_name": "t2",
49+
"access_type": "ALL",
50+
"rows": 10000,
51+
"r_rows": 10000,
52+
"r_filtered": 100,
53+
"r_total_time_ms": "REPLACED"
54+
}
55+
}
56+
}
57+
}
58+
#
59+
# Try an UPDATE that uses buffering:
60+
#
61+
explain
62+
update t2 set a=a+1 where a<10;
63+
id select_type table type possible_keys key key_len ref rows Extra
64+
1 SIMPLE t2 range a a 5 NULL 8 Using where; Using buffer
65+
explain format=json
66+
update t2 set a=a+1 where a<10;
67+
EXPLAIN
68+
{
69+
"query_block": {
70+
"select_id": 1,
71+
"buffer": {
72+
"table": {
73+
"update": 1,
74+
"table_name": "t2",
75+
"access_type": "range",
76+
"possible_keys": ["a"],
77+
"key": "a",
78+
"key_length": "5",
79+
"used_key_parts": ["a"],
80+
"rows": 8,
81+
"attached_condition": "(t2.a < 10)"
82+
}
83+
}
84+
}
85+
}
86+
analyze format=json
87+
update t2 set a=a+1 where a<10;
88+
ANALYZE
89+
{
90+
"query_block": {
91+
"select_id": 1,
92+
"r_total_time_ms": "REPLACED",
93+
"buffer": {
94+
"table": {
95+
"update": 1,
96+
"table_name": "t2",
97+
"access_type": "range",
98+
"possible_keys": ["a"],
99+
"key": "a",
100+
"key_length": "5",
101+
"used_key_parts": ["a"],
102+
"rows": 8,
103+
"r_rows": 10,
104+
"r_filtered": 100,
105+
"r_total_time_ms": "REPLACED",
106+
"attached_condition": "(t2.a < 10)"
107+
}
108+
}
109+
}
110+
}
111+
#
112+
# Try a DELETE that uses filesort:
113+
#
114+
explain
115+
delete from t2 order by b limit 5;
116+
id select_type table type possible_keys key key_len ref rows Extra
117+
1 SIMPLE t2 ALL NULL NULL NULL NULL 10000 Using filesort
118+
explain format=json
119+
delete from t2 order by b limit 5;
120+
EXPLAIN
121+
{
122+
"query_block": {
123+
"select_id": 1,
124+
"filesort": {
125+
"table": {
126+
"delete": 1,
127+
"table_name": "t2",
128+
"access_type": "ALL",
129+
"rows": 10000
130+
}
131+
}
132+
}
133+
}
134+
analyze format=json
135+
delete from t2 order by b limit 5;
136+
ANALYZE
137+
{
138+
"query_block": {
139+
"select_id": 1,
140+
"r_total_time_ms": "REPLACED",
141+
"filesort": {
142+
"r_loops": 1,
143+
"r_used_priority_queue": false,
144+
"r_output_rows": 10000,
145+
"r_buffer_size": "195Kb",
146+
"table": {
147+
"delete": 1,
148+
"table_name": "t2",
149+
"access_type": "ALL",
150+
"rows": 10000,
151+
"r_rows": 10000,
152+
"r_filtered": 100,
153+
"r_total_time_ms": "REPLACED"
154+
}
155+
}
156+
}
157+
}
158+
#
159+
# Try a SELECT with QEP in form: filesort { tmp_table { join } }
160+
#
161+
explain
162+
select * from t0,t2 where t2.a=t0.a order by t2.b limit 4;
163+
id select_type table type possible_keys key key_len ref rows Extra
164+
1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using where; Using temporary; Using filesort
165+
1 SIMPLE t2 ref a a 5 test.t0.a 1
166+
explain format=json
167+
select * from t0,t2 where t2.a=t0.a order by t2.b limit 4;
168+
EXPLAIN
169+
{
170+
"query_block": {
171+
"select_id": 1,
172+
"temporary_table": {
173+
"function": "buffer",
174+
"table": {
175+
"table_name": "t0",
176+
"access_type": "ALL",
177+
"rows": 10,
178+
"filtered": 100,
179+
"attached_condition": "(t0.a is not null)"
180+
},
181+
"table": {
182+
"table_name": "t2",
183+
"access_type": "ref",
184+
"possible_keys": ["a"],
185+
"key": "a",
186+
"key_length": "5",
187+
"used_key_parts": ["a"],
188+
"ref": ["test.t0.a"],
189+
"rows": 1,
190+
"filtered": 100
191+
}
192+
}
193+
}
194+
}
195+
analyze format=json
196+
select * from t0,t2 where t2.a=t0.a order by t2.b limit 4;
197+
ANALYZE
198+
{
199+
"query_block": {
200+
"select_id": 1,
201+
"r_loops": 1,
202+
"r_total_time_ms": "REPLACED",
203+
"filesort": {
204+
"temporary_table": {
205+
"table": {
206+
"table_name": "t0",
207+
"access_type": "ALL",
208+
"r_loops": 1,
209+
"rows": 10,
210+
"r_rows": 10,
211+
"r_total_time_ms": "REPLACED",
212+
"filtered": 100,
213+
"r_filtered": 100,
214+
"attached_condition": "(t0.a is not null)"
215+
},
216+
"table": {
217+
"table_name": "t2",
218+
"access_type": "ref",
219+
"possible_keys": ["a"],
220+
"key": "a",
221+
"key_length": "5",
222+
"used_key_parts": ["a"],
223+
"ref": ["test.t0.a"],
224+
"r_loops": 10,
225+
"rows": 1,
226+
"r_rows": 0.4,
227+
"r_total_time_ms": "REPLACED",
228+
"filtered": 100,
229+
"r_filtered": 100
230+
}
231+
}
232+
}
233+
}
234+
}
235+
#
236+
# Try a SELECT with QEP in form: join { filesort { table0 }, table2 }
237+
#
238+
explain
239+
select * from t0,t2 where t2.a=t0.a order by t0.a limit 4;
240+
id select_type table type possible_keys key key_len ref rows Extra
241+
1 SIMPLE t0 ALL NULL NULL NULL NULL 10 Using where; Using filesort
242+
1 SIMPLE t2 ref a a 5 test.t0.a 1
243+
analyze format=json
244+
select * from t0,t2 where t2.a=t0.a order by t0.a limit 4;
245+
ANALYZE
246+
{
247+
"query_block": {
248+
"select_id": 1,
249+
"r_loops": 1,
250+
"r_total_time_ms": "REPLACED",
251+
"read_sorted_file": {
252+
"r_rows": 10,
253+
"filesort": {
254+
"r_loops": 1,
255+
"r_used_priority_queue": false,
256+
"r_output_rows": 10,
257+
"r_buffer_size": "360",
258+
"table": {
259+
"table_name": "t0",
260+
"access_type": "ALL",
261+
"r_loops": 1,
262+
"rows": 10,
263+
"r_rows": 10,
264+
"r_total_time_ms": "REPLACED",
265+
"filtered": 100,
266+
"r_filtered": 1,
267+
"attached_condition": "(t0.a is not null)"
268+
}
269+
}
270+
},
271+
"table": {
272+
"table_name": "t2",
273+
"access_type": "ref",
274+
"possible_keys": ["a"],
275+
"key": "a",
276+
"key_length": "5",
277+
"used_key_parts": ["a"],
278+
"ref": ["test.t0.a"],
279+
"r_loops": 10,
280+
"rows": 1,
281+
"r_rows": 0.4,
282+
"r_total_time_ms": "REPLACED",
283+
"filtered": 100,
284+
"r_filtered": 100
285+
}
286+
}
287+
}
288+
drop table t2;
289+
drop table t0, t1;

mysql-test/r/explain_json.result

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,14 @@ EXPLAIN
478478
"materialized": {
479479
"query_block": {
480480
"select_id": 2,
481-
"table": {
482-
"table_name": "t1",
483-
"access_type": "ALL",
484-
"rows": 10,
485-
"filtered": 100
481+
"temporary_table": {
482+
"function": "buffer",
483+
"table": {
484+
"table_name": "t1",
485+
"access_type": "ALL",
486+
"rows": 10,
487+
"filtered": 100
488+
}
486489
}
487490
}
488491
}
@@ -517,11 +520,14 @@ EXPLAIN
517520
"materialized": {
518521
"query_block": {
519522
"select_id": 2,
520-
"table": {
521-
"table_name": "t1",
522-
"access_type": "ALL",
523-
"rows": 10,
524-
"filtered": 100
523+
"temporary_table": {
524+
"function": "buffer",
525+
"table": {
526+
"table_name": "t1",
527+
"access_type": "ALL",
528+
"rows": 10,
529+
"filtered": 100
530+
}
525531
}
526532
}
527533
}
@@ -558,11 +564,14 @@ EXPLAIN
558564
"unique": 1,
559565
"query_block": {
560566
"select_id": 2,
561-
"table": {
562-
"table_name": "t1",
563-
"access_type": "ALL",
564-
"rows": 10,
565-
"filtered": 100
567+
"temporary_table": {
568+
"function": "buffer",
569+
"table": {
570+
"table_name": "t1",
571+
"access_type": "ALL",
572+
"rows": 10,
573+
"filtered": 100
574+
}
566575
}
567576
}
568577
}

0 commit comments

Comments
 (0)