The Power of MySQL Explain

1,705 views
1,526 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,705
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

The Power of MySQL Explain

  1. 1. <Insert Picture Here>The Power of MySQL EXPLAINManyi Lu
  2. 2. EXPLAIN: The Query Execution Plancategory JOIN film JOINcategory filmEXPLAIN returns a row of informationfor each "table" used in the SELECTstatement.The "table" can be a real table, a derivedor temporary table, a subquery, or a unionresult. Copyright © 2012 Oracle and/or its affiliates. All rights reserved.
  3. 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. 4. MySQL 5.6: EXPLAIN for Data-Modifying StatementsEXPLAIN UPDATE t1 SET b = b WHERE a > 2 G id: 1 select_type: SIMPLE table: t1 type: rangepossible_keys: a,a_2 key: a key_len: 16 ref: null rows: 2 Extra: Using where; Using temporaryCopyright © 2012 Oracle and/or its affiliates. All rights reserved.
  5. 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: rangepossible_keys: t2i1 key: t2i1 key_len: 4 ref: null rows: 3 Extra: Using index conditionCopyright © 2012 Oracle and/or its affiliates. All rights reserved.
  6. 6. MySQL 5.6: Structured EXPLAINEXPLAIN 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. 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. 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. 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. 10. Structured EXPLAIN: SubqueriesEXPLAIN 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. 11. Optimizer Traces: Query Plan DebuggingSET 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.

×