Contenu connexe
Similaire à The Power of MySQL Explain
Similaire à The Power of MySQL Explain (20)
The Power of MySQL Explain
- 2. EXPLAIN: The Query Execution Plan
category
JOIN
film
JOIN
category
film
EXPLAIN returns a row of information
for each "table" used in the SELECT
statement.
The "table" can be a real table, a derived
or temporary table, a subquery, or a union
result.
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 3. EXPLAIN in MySQL 5.5: Room for Improvement
SELECT statements only
What about INSERT, UPDATE, DELETE ?
Tabular output
Difficult to see the structure of the query plan
More information would be useful
E.g., When are the WHERE conditions evaluated?
Shows the chosen plan, but does not tell you why this plan
was chosen.
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 4. MySQL 5.6: EXPLAIN for Data-Modifying Statements
EXPLAIN UPDATE t1 SET b = 'b' WHERE a > 2 G
id: 1
select_type: SIMPLE
table: t1
type: range
possible_keys: a,a_2
key: a
key_len: 16
ref: null
rows: 2
Extra: Using where; Using temporary
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 5. MySQL 5.6: EXPLAIN for Data-Modifying Statements, cont.
EXPLAIN INSERT INTO t1
SELECT * FROM t2 WHERE a IN (1, 3, 5) G
id: 1
select_type: SIMPLE
table: t2
type: range
possible_keys: t2i1
key: t2i1
key_len: 4
ref: null
rows: 3
Extra: Using index condition
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 6. MySQL 5.6: Structured EXPLAIN
EXPLAIN FORMAT=JSON
SELECT * FROM t2 WHERE i > 1 AND j < 3;
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t2",
"access_type": "range",
"possible_keys": [
"PRIMARY"
] /* possible_keys */,
...
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 7. MySQL 5.6: Structured EXPLAIN, cont.
...
"key": "PRIMARY",
"key_length": "4",
"rows": 2,
"filtered": 100,
"index_condition": "(`test`.`t2`.`i` > 1)",
"attached_condition": "(`test`.`t2`.`j` < 3)"
} /* table */
} /* query_block */
}
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 8. Structured EXPLAIN:
View with JSON Plugin in Browser for Expand/Collapse
{
- query_block: {
select_id: 1,
- nested_loop: [
- {
+ table: { … }
},
- {
+ table: { … }
}
]
}
}
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 9. Structured EXPLAIN:
View with JSON Plugin in Browser for Expand/Collapse, cont.
{
- query_block: {
select_id: 1,
- nested_loop: [
- {
- table: {
table_name: "t1",
access_type: "ALL",
rows: 3,
filtered: 100,
attached_condition: "(`test`.`t1`.`j` > 1)"
}
},
...
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 10. Structured EXPLAIN: Subqueries
EXPLAIN FORMAT=JSON SELECT * FROM (SELECT * FROM t1) t;
{
- query_block: {
select_id: 1,
- table: {
table_name: "t",
access_type: "ALL",
rows: 3,
filtered: 100,
+ materialized_from_subquery: { … }
}
}
}
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
- 11. Optimizer Traces: Query Plan Debugging
SET SESSION.OPTIMIZER_TRACE=‘enabled=on’; "rows_estimation": [
{
SELECT v FROM t1 WHERE i1 = 1 AND v = 'a'; "table": "`t1`",
SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE; "range_analysis": {
"table_scan": {
"rows": 5,
"cost": 4.1
},
"potential_range_indices": [
{
"index": "v_idx",
• EXPLAIN shows the generated plan "usable": true,
"key_parts": [
• TRACE shows how the plan was generated, "v",
"i1"
decision points etc. ]
}
• JSON format ],
"best_covering_index_scan": {
• Developers, support, advanced customers "index": "v_idx",
"cost": 2.0063,
"chosen": true
} ,
Copyright © 2012 Oracle and/or its affiliates. All rights reserved.