SlideShare a Scribd company logo
<Insert Picture Here>




The Power of MySQL EXPLAIN
Manyi Lu
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
The Power of MySQL Explain

More Related Content

PPT
SQL Tutorial - Basic Commands
PPTX
Oracle: Basic SQL
PDF
Quebec pdo
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PPTX
SQL Sort Notes
PDF
Data Manipulation(DML) and Transaction Control (TCL)
PPTX
Creating database using sql commands
PPT
Chapter 07 ddl_sql
SQL Tutorial - Basic Commands
Oracle: Basic SQL
Quebec pdo
Database Systems - SQL - DDL Statements (Chapter 3/3)
SQL Sort Notes
Data Manipulation(DML) and Transaction Control (TCL)
Creating database using sql commands
Chapter 07 ddl_sql

What's hot (19)

PPTX
Sql basics
PPTX
Spring framework part 2
PPTX
Database Systems - SQL - DCL Statements (Chapter 3/4)
PPT
Advanced Sql Training
PDF
S313431 JPA 2.0 Overview
PDF
Database Systems - SQL - DDL Statements (Chapter 3/2)
PPT
Quebec pdo
PDF
Sql tutorial
PPTX
Oraclesql
PDF
Assignment#02
DOCX
Lecture5-SQL.docx
PPTX
Starting with JSON Path Expressions in Oracle 12.1.0.2
PDF
SQL Fundamentals - Lecture 2
PPTX
SQL - Structured query language introduction
PDF
Sql tutorial
PPTX
Sql basic things
PDF
Assignment#04
PDF
UKOUG Tech14 - Getting Started With JSON in the Database
Sql basics
Spring framework part 2
Database Systems - SQL - DCL Statements (Chapter 3/4)
Advanced Sql Training
S313431 JPA 2.0 Overview
Database Systems - SQL - DDL Statements (Chapter 3/2)
Quebec pdo
Sql tutorial
Oraclesql
Assignment#02
Lecture5-SQL.docx
Starting with JSON Path Expressions in Oracle 12.1.0.2
SQL Fundamentals - Lecture 2
SQL - Structured query language introduction
Sql tutorial
Sql basic things
Assignment#04
UKOUG Tech14 - Getting Started With JSON in the Database
Ad

Viewers also liked (19)

PDF
Como migrar una base de datos de mysql a power designer
PDF
MySQL 5.7: Focus on InnoDB
PPTX
The nightmare of locking, blocking and isolation levels!
PDF
Mvcc Unmasked (Bruce Momjian)
ODP
Mysql For Developers
PDF
Mv unmasked.w.code.march.2013
 
ODP
PDF
Inno db internals innodb file formats and source code structure
PPT
A brief introduction to PostgreSQL
PPTX
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
PDF
InnoDB Internal
PDF
Introdução ao MySQL
PDF
Recursos e Benefícios do MySQL
PDF
MySQL EXPLAIN Explained-Norvald H. Ryeng
PDF
MySQL para Desenvolvedores de Produto
PDF
SQL Transactions - What they are good for and how they work
PDF
Mysql Explain Explained
PDF
Explaining the MySQL Explain
PDF
Powerful Explain in MySQL 5.6
Como migrar una base de datos de mysql a power designer
MySQL 5.7: Focus on InnoDB
The nightmare of locking, blocking and isolation levels!
Mvcc Unmasked (Bruce Momjian)
Mysql For Developers
Mv unmasked.w.code.march.2013
 
Inno db internals innodb file formats and source code structure
A brief introduction to PostgreSQL
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
InnoDB Internal
Introdução ao MySQL
Recursos e Benefícios do MySQL
MySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL para Desenvolvedores de Produto
SQL Transactions - What they are good for and how they work
Mysql Explain Explained
Explaining the MySQL Explain
Powerful Explain in MySQL 5.6
Ad

Similar to The Power of MySQL Explain (20)

PDF
Optimizer percona live_ams2015
PDF
Non-Relational Postgres / Bruce Momjian (EnterpriseDB)
ODP
San diegophp
PDF
Percona Live 2012PPT: MySQL Query optimization
PDF
Troubleshooting MySQL Performance add-ons
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
PDF
Understanding query-execution806
PDF
Understanding Query Execution
PDF
Advanced query optimization
PDF
Scaling MySQL Strategies for Developers
PDF
How to Analyze and Tune MySQL Queries for Better Performance
PDF
What's New MySQL 8.0?
PPTX
MySQL Optimizer Overview
PDF
Quick Wins
PDF
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
PDF
Goldilocks and the Three MySQL Queries
PDF
Covering indexes
PDF
Fosdem 2012 practical_indexing
PDF
Optimizer overviewoow2014
Optimizer percona live_ams2015
Non-Relational Postgres / Bruce Momjian (EnterpriseDB)
San diegophp
Percona Live 2012PPT: MySQL Query optimization
Troubleshooting MySQL Performance add-ons
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
MySQL Indexes and Histograms - RMOUG Training Days 2022
Understanding query-execution806
Understanding Query Execution
Advanced query optimization
Scaling MySQL Strategies for Developers
How to Analyze and Tune MySQL Queries for Better Performance
What's New MySQL 8.0?
MySQL Optimizer Overview
Quick Wins
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Goldilocks and the Three MySQL Queries
Covering indexes
Fosdem 2012 practical_indexing
Optimizer overviewoow2014

More from MYXPLAIN (14)

PDF
Query Optimization with MySQL 5.6: Old and New Tricks
PDF
Need for Speed: MySQL Indexing
PDF
Advanced Query Optimizer Tuning and Analysis
PDF
MySQL Index Cookbook
PDF
Advanced MySQL Query and Schema Tuning
PDF
Are You Getting the Best of your MySQL Indexes
PDF
How to Design Indexes, Really
PDF
MySQL 5.6 Performance
PPTX
MySQL Indexing - Best practices for MySQL 5.6
PDF
56 Query Optimization
PDF
Tools and Techniques for Index Design
PDF
Optimizing Queries with Explain
PDF
Improving Performance with Better Indexes
PDF
MySQL Optimizer Overview
Query Optimization with MySQL 5.6: Old and New Tricks
Need for Speed: MySQL Indexing
Advanced Query Optimizer Tuning and Analysis
MySQL Index Cookbook
Advanced MySQL Query and Schema Tuning
Are You Getting the Best of your MySQL Indexes
How to Design Indexes, Really
MySQL 5.6 Performance
MySQL Indexing - Best practices for MySQL 5.6
56 Query Optimization
Tools and Techniques for Index Design
Optimizing Queries with Explain
Improving Performance with Better Indexes
MySQL Optimizer Overview

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Cloud computing and distributed systems.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
A Presentation on Artificial Intelligence
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Unlocking AI with Model Context Protocol (MCP)
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Spectral efficient network and resource selection model in 5G networks
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Advanced methodologies resolving dimensionality complications for autism neur...
NewMind AI Monthly Chronicles - July 2025
Review of recent advances in non-invasive hemoglobin estimation
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
MYSQL Presentation for SQL database connectivity
Understanding_Digital_Forensics_Presentation.pptx
Cloud computing and distributed systems.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
A Presentation on Artificial Intelligence
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
Unlocking AI with Model Context Protocol (MCP)

The Power of MySQL Explain

  • 1. <Insert Picture Here> The Power of MySQL EXPLAIN Manyi Lu
  • 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.