0%

优化器

优化器

MySQL存储引擎中存在了一个可插拔的优化器OPTIMIZER_TRACE,可以看到内部查询计划的TRACE信息,从而可以知道MySQL内部执行过程

查询优化器状态

1
2
3
4
show variables like 'optimizer_trace';

Variable_name Value
optimizer_trace enabled=off,one_line=off

开启优化器

1
set session optimizer_trace="enabled=on,one_line=on",end_markers_in_json=on;

查看优化器追踪内存大小

1
2
3
4
show variables like 'optimizer_trace_max_mem_size';

Variable_name Value
optimizer_trace_max_mem_size 16384

可以在information_schema数据库中的OPTIMIZER_TRACE查看sql执行情况

1
SELECT trace FROM information_schema.OPTIMIZER_TRACE;

示例:

1
2
3
 explain select id,sum(cost) from jr_form where id in (
select max(id) from jr_form group by jr_code) group by id;
SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;

这里注意一下,我在Navicat中执行时如果两个语句分开执行会导致TRACE中内容为

错误的Trace操作

需要选中两条sql语句一块执行才可以。每个session只能跟踪自己执行的语句

trace结果为

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
{
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"join_preparation": {
"select#": 2,
"steps": [
{
"expanded_query": "/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code`"
},
{
"transformation": {
"select#": 2,
"from": "IN (SELECT)",
"to": "semijoin",
"chosen": false
}
},
{
"transformation": {
"select#": 2,
"from": "IN (SELECT)",
"to": "EXISTS (CORRELATED SELECT)",
"chosen": true,
"evaluating_constant_having_conditions": []
}
}
]
}
},
{
"expanded_query": "/* select#1 */ select `jr_form`.`id` AS `id`,sum(`jr_form`.`cost`) AS `sum(cost)` from `jr_form` where <in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`))))) group by `jr_form`.`id`"
}
]
}
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))",
"steps": [
{
"transformation": "equality_propagation",
"subselect_evaluation": [],
"resulting_condition": "<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))"
},
{
"transformation": "constant_propagation",
"subselect_evaluation": [],
"resulting_condition": "<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))"
},
{
"transformation": "trivial_condition_removal",
"subselect_evaluation": [],
"resulting_condition": "<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))"
}
]
}
},
{
"substitute_generated_columns": {}
},
{
"table_dependencies": [
{
"table": "`jr_form`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": []
}
]
},
{
"ref_optimizer_key_uses": []
},
{
"rows_estimation": [ // 估算需要扫描的记录数
{
"table": "`jr_form`",
"const_keys_added": {
"keys": [
"PRIMARY",
"jr_code"
],
"cause": "group_by"
},
"range_analysis": {
"table_scan": {
"rows": 184,
"cost": 44.9
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": true,
"key_parts": [
"id"
]
},
{
"index": "jr_code",
"usable": true,
"key_parts": [
"jr_code",
"id"
]
}
],
"setup_range_conditions": [],
"group_index_range": {
"chosen": false,
"cause": "not_applicable_aggregate_function"
}
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [],
"table": "`jr_form`",
"best_access_path": {
"considered_access_paths": [
{
"rows_to_scan": 184,
"access_type": "scan",
"resulting_rows": 184,
"cost": 42.8,
"chosen": true,
"use_tmp_table": true
}
]
},
"condition_filtering_pct": 100,
"rows_for_plan": 184,
"cost_for_plan": 42.8,
"sort_cost": 184,
"new_cost_for_plan": 226.8,
"chosen": true
}
]
},
{
"attaching_conditions_to_tables": {
"original_condition": "<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))",
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
"table": "`jr_form`",
"attached": "<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))"
}
]
}
},
{
"clause_processing": {
"clause": "GROUP BY",
"original_clause": "`jr_form`.`id`",
"items": [
{
"item": "`jr_form`.`id`"
}
],
"resulting_clause_is_simple": true,
"resulting_clause": "`jr_form`.`id`"
}
},
{
"reconsidering_access_paths_for_index_ordering": {
"clause": "GROUP BY",
"index_order_summary": {
"table": "`jr_form`",
"index_provides_order": true,
"order_direction": "asc",
"index": "PRIMARY",
"plan_changed": true,
"access_type": "index"
}
}
},
{
"refine_plan": [
{
"table": "`jr_form`"
}
]
}
]
}
},
{
"join_optimization": {
"select#": 2,
"steps": [
{
"condition_processing": {
"condition": "HAVING",
"original_condition": "(<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))",
"steps": [
{
"transformation": "constant_propagation",
"resulting_condition": "(<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "(<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))"
}
]
}
},
{
"substitute_generated_columns": {}
},
{
"table_dependencies": [
{
"table": "`jr_form`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": []
}
]
},
{
"rows_estimation": [
{
"table": "`jr_form`",
"const_keys_added": {
"keys": [
"jr_code"
],
"cause": "group_by"
},
"range_analysis": {
"table_scan": {
"rows": 184,
"cost": 44.9
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "jr_code",
"usable": true,
"key_parts": [
"jr_code",
"id"
]
}
],
"best_covering_index_scan": {
"index": "jr_code",
"cost": 41.319,
"chosen": true
},
"group_index_range": {
"potential_group_range_indexes": [
{
"index": "jr_code",
"covering": true,
"rows": 152,
"cost": 64.8
}
]
},
"best_group_range_summary": {
"type": "index_group",
"index": "jr_code",
"group_attribute": "id",
"min_aggregate": false,
"max_aggregate": true,
"distinct_aggregate": false,
"rows": 152,
"cost": 64.8,
"key_parts_used_for_access": [
"jr_code"
],
"ranges": [],
"chosen": false,
"cause": "cost"
}
}
}
]
},
{
"considered_execution_plans": [
{
"plan_prefix": [],
"table": "`jr_form`",
"best_access_path": {
"considered_access_paths": [
{
"rows_to_scan": 184,
"access_type": "scan",
"resulting_rows": 184,
"cost": 42.8,
"chosen": true,
"use_tmp_table": true
}
]
},
"condition_filtering_pct": 100,
"rows_for_plan": 184,
"cost_for_plan": 42.8,
"sort_cost": 184,
"new_cost_for_plan": 226.8,
"chosen": true
}
]
},
{
"transformation": {
"select#": 2,
"from": "IN (SELECT)",
"to": "materialization",
"has_nullable_expressions": true,
"treat_UNKNOWN_as_FALSE": true,
"possible": true
}
},
{
"execution_plan_for_potential_materialization": {
"surely_same_plan_as_EXISTS": true,
"cause": "EXISTS_did_not_change_WHERE",
"subq_mat_decision": {
"parent_fanouts": [
{
"select#": 1,
"subq_attached_to_table": true,
"table": "`jr_form`",
"fanout": 184,
"cacheable": true
}
],
"cost_to_create_and_fill_materialized_table": 265.6,
"cost_of_one_EXISTS": 226.8,
"number_of_subquery_evaluations": 184,
"cost_of_materialization": 302.4,
"cost_of_EXISTS": 41731,
"chosen": true
}
}
},
{
"transformation": {
"select#": 2,
"from": "IN (SELECT)",
"to": "materialization",
"chosen": true,
"unknown_key_1": {
"creating_tmp_table": {
"tmp_table_info": {
"row_length": 9,
"key_length": 9,
"unique_constraint": false,
"location": "memory (heap)",
"row_limit_estimate": 1864135
}
}
}
}
},
{
"attaching_conditions_to_tables": {
"original_condition": null,
"attached_conditions_computation": [],
"attached_conditions_summary": [
{
"table": "`jr_form`",
"attached": null
}
]
}
},
{
"clause_processing": {
"clause": "GROUP BY",
"original_clause": "`jr_form`.`jr_code`",
"items": [
{
"item": "`jr_form`.`jr_code`"
}
],
"resulting_clause_is_simple": true,
"resulting_clause": "`jr_form`.`jr_code`"
}
},
{
"reconsidering_access_paths_for_index_ordering": {
"clause": "GROUP BY",
"index_order_summary": {
"table": "`jr_form`",
"index_provides_order": true,
"order_direction": "asc",
"index": "jr_code",
"plan_changed": false
}
}
},
{
"refine_plan": [
{
"table": "`jr_form`"
}
]
}
]
}
},
{
"join_explain": {
"select#": 1,
"steps": [
{
"join_explain": {
"select#": 2,
"steps": []
}
}
]
}
}
]
}

内容很长,之后在详细说明

欢迎关注我的其它发布渠道