1   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Powerful EXPLAIN in
MySQL 5.6
Evgeny Potemkin
MySQL Optimizer team




2   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Program Agenda


         Introduction
         Optimizer trace
         EXPLAIN in JSON format
         Explaining INSERT, UPDATE, DELETE




3   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Graphic Section Divider




4   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer Trace




5   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: quick start
mysql> SET optimizer_trace= “enabled=on“,
             end_markers_in_json=on;
mysql> SELECT * FROM orders WHERE customer_id <> 6;
mysql> SELECT trace FROM
   information_schema.OPTIMIZER_TRACE;
mysql> SET optimizer_trace="enabled=off";
    QUERY                                                                                                         SELECT * FROM orders WHERE customer_id <> 6
    TRACE                                                                                                          “steps”: [ { "join_preparation": { "select#": 1,… } … } …]
    MISSING_BYTES_BEYOND_MAX_MEM_SIZE                                                                              0
    INSUFFICIENT_PRIVILEGES                                                                                        0


6    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: what’s inside
 Steps
         – Join preparation
                       Modifications to subquery
                       Expanded query
         – Join optimization
                       Range optimizer info
                       Plan generation process
                       Condition optimization
                       Processing of ORDER/GROUP BY
         – Join execution
                       Tmp tables related info
7   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: example
mysql> SET optimizer_trace="enabled=on";
mysql> SELECT * FROM t1,t2 WHERE f1=1 AND f1=f2 AND f2>0;
mysql> SELECT trace FROM information_schema.optimizer_trace;
mysql> SET optimizer_trace="enabled=off";




8   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: example
    join_optimization / steps / condition_processing
    "condition": "WHERE",
    "original_condition": "((`t1`.`f1` = 1) and (`t1`.`f1` = `t2`.`f2`) and (`t2`.`f2` > 0))",
    "steps": [
     {
         "transformation": "equality_propagation",
         "resulting_condition": "((`t2`.`f2` > 0) and multiple equal(1, `t1`.`f1`, `t2`.`f2`))"
     }, {
         "transformation": "constant_propagation",
         "resulting_condition": "((1 > 0) and multiple equal(1, `t1`.`f1`, `t2`.`f2`))"
     }, {
         "transformation": "trivial_condition_removal",
         "resulting_condition": "multiple equal(1, `t1`.`f1`, `t2`.`f2`)"
     }
    ] /* steps */


9    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: example
     join_optimization / row_estimation / table : t1, range_analysis
     "table_scan": {                                                                                                             "analyzing_range_alternatives": {
      "rows": 5,                                                                                                                    "range_scan_alternatives": [ {
      "cost": 5.1085                                                                                                                      "index": "f1",
     } /* table_scan */,                                                                                                                  "ranges": [ "1 <= f1 <= 1“ ] /* ranges */,
     "potential_range_indices": [ {                                                                                                       "index_dives_for_eq_ranges": true,
        "index": "f1",                  "usable": true,                                                                                   "rows": 2, "cost": 3.41,
        "key_parts": [ "f1“ ] /* key_parts */ }                                                                                           "chosen": false,
     ] /* potential_range_indices */,                                                                                                     "cause": "cost"
     "best_covering_index_scan": {                                                                                                    }
      "index": "f1", "cost": 2.093, "chosen": true                                                                                  ] /* range_scan_alternatives */,
     } /* best_covering_index_scan */,                                                                                              "analyzing_roworder_intersect": {
     "group_index_range": {                                                                                                           "usable": false, "cause": "too_few_roworder_scans"
      "chosen": false, "cause": "not_single_table"                                                                                  } /* analyzing_roworder_intersect */
     } /* group_index_range */,                                                                                                  } /* analyzing_range_alternatives */


10    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: example
     join_optimization / considered_execution_plan
     "plan_prefix": [ ] /* plan_prefix */,                                                                                        "plan_prefix": [ "`t2`“ ] /* plan_prefix */,
     "table": "`t2`",                                                                                                                 "table": "`t1`",
     "best_access_path": {                                                                                                            "best_access_path": {
      "considered_access_paths": [ {                                                                                                     "considered_access_paths": [ {
          "access_type": "scan", "rows": 3,                                                                                                      "access_type": "ref", "index": "f1",
          "cost": 2.6051, "chosen": true                                                                                                         "rows": 2, "cost": 3.4698, "chosen": true
      } ] /* considered_access_paths */                                                                                                    }, {
     } /* best_access_path */,                                                                                                                   "access_type": "scan", "using_join_cache": true,
     "cost_for_plan": 2.6051,                                                                                                                    "rows": 2, "cost": 3.8087,
     "rows_for_plan": 3,                                                                                                                         "chosen": true
     "rest_of_plan": [ {                                                                                                              } ] } /* best_access_path */,
     … next iteration … =>                                                                                                            "cost_for_plan": 6.4138,
     } ] /* rest_of_plan */                                                                                                           "rows_for_plan": 6,
                                                                                                                                      "chosen": true


11    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: example
     join_optimization / plan refinement
     {                                                                                                                               {
            "attaching_conditions_to_tables": {                                                                                          "refine_plan": [
            "original_condition":                                                                                                         {
              "((`t1`.`f1` = 1) and (`t2`.`f2` = 1))",                                                                                        "table": "`t1`"
             "attached_conditions_summary": [ {                                                                                           },
                  "table": "`t1`", "attached": null                                                                                       {
              }, {                                                                                                                            "table": "`t2`",
                  "table": "`t2`", "attached": "(`t2`.`f2` = 1)"                                                                              "access_type": "table_scan"
              }                                                                                                                           }
            ] /* attached_conditions_summary */                                                                                          ] /* refine_plan */
         } /* attaching_conditions_to_tables */                                                                                      }
     },




12        Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: controls
  optimizer-trace
  optimizer-trace-features
          – greedy_search
          – range_optimizer
          – dynamic_range
          – repeated_subselect
  optimizer-trace-limit
  optimizer-trace-offset
  optimizer-trace-max-mem-size
  end-markers-in-json

13   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Optimizer trace: exploring
  mysql> pager less
  SELECT TRACE INTO DUMPFILE <filename> FROM
   INFORMATION_SCHEMA.OPTIMIZER_TRACE;
  JSONView for Firefox
  Pretty JSON for Chrome
  A registry patch for IE




14   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON format




15   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Traditional EXPLAIN
id select type table                                            type              possible                       key                             key len ref        rows   extra
                                                                                  keys
     1 PRIMARY                        cust                       index            cust_id,                        cust_name                      40      const      10    Using where; Start
                                                                                  cust_name                                                                               materialize; Scan
     1 PRIMARY                        orders                     ref              order_id                        order_id                       8       ordid      32    Using where; End
                                                                                                                                                                          materialize; Using join
                                                                                                                                                                          buffer (Block Nested
                                                                                                                                                                          Loop)
     1 PRIMARY                        <derived2> ref                               <auto_key0> <auto_key0>                                       23      vc1        100   Using index
     2 DERIVED                        flights    ALL                               NULL        NULL                                              NULL    NULL       10000 Using where; Start
                                                                                                                                                                          materialize; Scan
     2 DERIVED                        storage                    eq_ref            PRIMARY                        PRIMARY                        8       fl_store   1     Using where; End
                                                                                                                                                                          materialize; Using join
                                                                                                                                                                          buffer (Block Nested
                                                                                                                                                                          Loop)
     4 SUBQUERY buzz                                             range             buzzez                         buzzez                         11      NULL       42    Using index; Using
                                                                                                                                                                          where
     6 SUBQUERY shortage                                         index             shrt_idx                       shrt_idx                       100     NULL       243   Using index; Using
                                                                                                                                                                          where

16    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
Traditional EXPLAIN: Cons
  Hard to read in complex cases
  Unclear/non-obvious query structure in complex cases
          – Could take quite some time to see what’s going on
  Limited information about query plan
          – How conditions are split? When a subquery is evaluated?
  Hard to impossible to extend
          – Indication of a feature being used usually is limited to “Using <something>”




17   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
 mysql> EXPLAIN SELECT * FROM t1;
     id          select type                            table                   type            possible keys                               key     key len   ref    rows   extra
     1            SIMPLE                                 t1                     index           NULL                                         i      5         NULL   3      NULL




18       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
                                                                                                                                          EXPLAIN
                                                                                                                                                "query_block": {
                                                                                                                                                 "select_id": 1,
                                                                                                                                                 "table": {
                                                                                                                                                  "table_name": "t1",
 mysql> EXPLAIN FORMAT=JSON                                                                                                                       "access_type": "index",
 SELECT * FROM t1;                                                                                                                                "key": "i",
                                                                                                                                                  "key_length": "5",
                                                                                                                                                  "rows": 3,
                                                                                                                                                  "filtered": 100,
                                                                                                                                                  "using_index": true
                                                                                                                                                 } /* table */
                                                                                                                                                } /* query_block */




19   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
 mysql> set end_markers_in_json = on;
     end_markers_in_json = off                                                                                                  end_markers_in_json = on
                                              }                                                                                                             } /* table */
                                          }                                                                                                               } /* query_block */
                                      }                                                                                                               }
                                  ]                                                                                                                 ] /* query_specifications */
                              }                                                                                                                    } /* union_result */
                          }                                                                                                                       } /* query_block */
                      }                                                                                                                          } /* materialized_from_subquery */
                  }                                                                                                                        } /* table */
              }                                                                                                                          } /* grouping_operation */
          }                                                                                                                           } /* ordering_operation */
      }                                                                                                                             } /* query_block */



20    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
 mysql> EXPLAIN SELECT * FROM t1 WHERE str IS NULL AND id > 0;
     id          select type                            table                   type             possible keys                               key     key len   ref     rows   extra
     1            SIMPLE                                 t1                     ref              str                                          str    11        const   1      Using index
                                                                                                                                                                              condition;
                                                                                                                                                                              Using where




21       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.    Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
                                                                                                                                          EXPLAIN
                                                                                                                                            "query_block": {
                                                                                                                                                 "select_id": 1,
                                                                                                                                                 "table": {
                                                                                                                                                  "table_name": "t1",
 mysql> EXPLAIN FORMAT=JSON                                                                                                                       "access_type": "ref",
 SELECT * FROM t1 WHERE str IS                                                                                                                    "possible_keys": [ "str“ ],
                                                                                                                                                  "key": "str", "key_length": "11",
 NULL AND id > 0;
                                                                                                                                                  "ref": [ "const"],
                                                                                                                                                  "rows": 1, "filtered": 100,
                                                                                                                                                  "index_condition": "isnull(`test`.`t1`.`str`)“
                                                                                                                                                  "attached_condition": "(`test`.`t1`.`id` > 0)"
                                                                                                                                                 } /* table */
                                                                                                                                                } /* query_block */



22   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
 mysql> EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a
 BETWEEN '0000-00-00' AND '1001-01-01';
                                                                                                                             possible                     key
     id          select type                      partitions                          table               type                                      key         ref    rows   extra
                                                                                                                             keys                         len
     1            SIMPLE                           pNULL,                             t1                  index              a                      a     4     NULL   6      Using where;
                                                   p0001-01-01,                                                                                                               Using index
                                                   p1001-01-01




23       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
                                                                                                                                          EXPLAIN
                                                                                                                                            "query_block": {
                                                                                                                                                "select_id": 1,
                                                                                                                                                "table": {
                                                                                                                                                 "table_name": "t1",
 mysql> EXPLAIN FORMAT=JSON                                                                                                                      "partitions": [ "pNULL", "p0001-01-01",
 SELECT * FROM t1 WHERE a                                                                                                                            "p1001-01-01"],
                                                                                                                                                 "access_type": "index",
 BETWEEN '0000-00-00' AND '1001-
                                                                                                                                                 "possible_keys": [ "a" ],
 01-01';                                                                                                                                         "key": "a", "key_length": "4",
                                                                                                                                                 "rows": 6, "filtered": 33.333,
                                                                                                                                                 "using_index": true,
                                                                                                                                                 "attached_condition": "(`test`.`t1`.`a` between
                                                                                                                                                     '0000-00-00' and '1001-01-01')"
                                                                                                                                                }}


24   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
 mysql> EXPLAIN SELECT * FROM t1 JOIN t2 USING(i);
                                                                                              possible                                         key
     id          select type                          table                type                                           key                         ref    rows   extra
                                                                                              keys                                             len
     1            SIMPLE                               t2                  ALL                NULL                         NULL                NULL   NULL   3      NULL

     1            SIMPLE                               t1                  index              i                            i                   5      NULL   3      Using where; Using
                                                                                                                                                                    index; Using join
                                                                                                                                                                    buffer (Block Nested
                                                                                                                                                                    Loop)




25       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
     EXPLAIN
     {                                                                                                                               {
         "query_block": {                                                                                                                  "table": {
           "select_id": 1,                                                                                                                   "table_name": "t1",
           "nested_loop": [                                                                                                                  "access_type": "index",
             {                                                                                                                               "possible_keys": [ "i" ],
                 "table": {                                                                                                                  "key": "i", "key_length": "5",
                   "table_name": "t2",                                                                                                       "rows": 3, "filtered": 100,
                   "access_type": "ALL",                                                                                                     "using_index": true,
                   "rows": 3,                                                                                                                "using_join_buffer": "Block Nested Loop",
                   "filtered": 100                                                                                                           "attached_condition":
                 } /* table */                                                                                                                      "(`test`.`t1`.`i` = `test`.`t2`.`i`)"
             },                                                                                                                            } /* table */
            … next table … =>                                                                                                        }
         ] } /* nested_loop */ } /* query_block */


26       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: Basics
         MySQL Workbench 5.2




 SELECT * FROM t1 JOIN t2 USING(i);




27   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: more complex queries
 mysql> EXPLAIN FORMAT=JSON SELECT t1.a, MIN(t2.b)
 FROM t1 LEFT JOIN t2 ON t1.a=t2.a GROUP BY t1.a ORDER BY t1.b;

     id         select type                       table              type             possible keys                         key                     key len          ref   rows   extra
     1           SIMPLE                            t1                 ALL             NULL                                   NULL                   NULL      NULL         3      Using temporary;
                                                                                                                                                                                  Using filesort
     1           SIMPLE                            t2                 ref             PRIMARY                                PRIMARY                4         test.t1.a    1      Using index




28       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: more complex queries
                                                                                                                                          EXPLAIN
                                                                                                                                            "query_block": {
 mysql> EXPLAIN FORMAT=JSON                                                                                                                      "select_id": 1,

 SELECT t1.a, MIN(t2.b)                                                                                                                          "ordering_operation": {
                                                                                                                                                  "using_temporary_table": true,
 FROM t1 LEFT JOIN t2                                                                                                                             "using_filesort": true,
                                                                                                                                                  "grouping_operation": {
  ON t1.a=t2.a
                                                                                                                                                    "using_filesort": true,
 GROUP BY t1.a ORDER BY t1.b;                                                                                                                       "nested_loop": [
                                                                                                                                                      { “table” : {…} },
                                                                                                                                                      { “table” : {…} } ] /* nested_loop */
                                                                                                                                                  } /* grouping_operation */
                                                                                                                                                 } /* ordering_operation */
                                                                                                                                                } /* query_block */



29   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: more complex queries
         MySQL Workbench 5.2

 SELECT t1.a, MIN(t2.b)
 FROM t1 LEFT JOIN t2
  ON t1.a=t2.a
 GROUP BY t1.a ORDER BY t1.b;




30   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: more complex queries
 mysql> EXPLAIN SELECT a, b FROM
 (SELECT 1 AS a, 2 AS b UNION ALL SELECT 1 AS a, 2 AS b) t1
 GROUP BY a ORDER BY b DESC;
                                                                                                          possible                                  key
     id                  select type                      table                         type                                       key                     ref    rows   extra
                                                                                                          keys                                      len
     1                   PRIMARY                          <derived2>                     ALL               NULL                     NULL            NULL   NULL   2      Using temporary;
                                                                                                                                                                         Using filesort
     2                   DERIVED                          NULL                           NULL              NULL                     NULL            NULL   NULL   NULL   No tables used
     3                   UNION                            NULL                           NULL              NULL                     NULL            NULL   NULL   NULL   No tables used
     NULL                UNION                            <union2,3>                     ALL               NULL                     NULL            NULL   NULL   NULL   Using temporary
                         RESULT




31       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: more complex queries
     EXPLAIN
     "query_block": {                                                                                                              "union_result": {
          "ordering_operation": {                                                                                                        "using_temporary_table": true,
          "using_filesort": true,                                                                                                        "table_name": "<union2,3>",
          "grouping_operation": {                                                                                                        "query_specifications": [ {
             "using_temporary_table": true,                                                                                                 "query_block": {
             "using_filesort": false,                                                                                                            "table": {
             "table": {                                                                                                                           "message": "No tables used"
                "table_name": "t1",                                                                                                               } } }, {
                "materialized_from_subquery": {                                                                                             "query_block": {
                  "using_temporary_table": true,                                                                                                 "table": {
                  "query_block": {                                                                                                                "message": "No tables used"
                     … here goes union_result… =>                                                                                                } } } ] } /* union_result */
                  }}}}}}



32    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: more complex queries
         MySQL Workbench 5.2


 SELECT a, b FROM
 (SELECT 1 AS a, 2 AS b UNION ALL
 SELECT 1 AS a, 2 AS b) t1
 GROUP BY a ORDER BY b DESC;




33   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: subqueries
 mysql> EXPLAIN FORMAT=JSON SELECT derived.vc
 FROM (SELECT * FROM t1) AS derived WHERE
 derived.vc IN (SELECT t2.vc1 FROM t2 JOIN t3 ON t2.vc2=t3.vc);
               select                                                                       possible                                                 key
     id                                       table                             type                                          key                           ref      rows   extra
               type                                                                         keys                                                     len
     1          PRIMARY                        t2                               ALL          NULL                             NULL                   NULL   NULL     2      Using where;
                                                                                                                                                                            Start materialize;
                                                                                                                                                                            Scan
     1          PRIMARY                        t3                               ALL          NULL                             NULL                   NULL   NULL     3      Using where; End
                                                                                                                                                                            materialize; Using
                                                                                                                                                                            join buffer (Block
                                                                                                                                                                            Nested Loop)
     1          PRIMARY                        <derived2>                       ref          <auto_key0>                      <auto_key0>            23     t2.vc1   2      NULL
     2          DERIVED                        t1                               ALL          NULL                             NULL                   NULL   NULL     2      NULL


34       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.    Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: subqueries
     EXPLAIN
     "query_block": {                                                                                                             "table": {
        "select_id": 1,                                                                                                                          "table_name": "derived",
        "nested_loop": [                                                                                                                         "materialized_from_subquery": {
          { "table": {                                                                                                                            "using_temporary_table": true,
                "table_name": "<subquery3>",                                                                                                      "query_block": {
                "materialized_from_subquery": {                                                                                                    "select_id": 2,
                "query_block": {                                                                                                                   "table": {
                     "nested_loop": [                                                                                                                  "table_name": "t1“,
                        { "table": { "table_name": "t2“ } },                                                                                           …
                        { "table": { "table_name": "t3“ } }                                                                                        }
                     ] } /* query_block */ } } /* table */                                                                                        } /* query_block */
          }, { … next table goes here … => }                                                                                                     } /* materialized_from_subquery */
        ] /* nested_loop */ } /* query_block */                                                                                              } /* table */



35    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: subqueries
         MySQL Workbench 5.2


 SELECT derived.vc
 FROM (SELECT * FROM t1) AS
 derived WHERE
 derived.vc IN (SELECT t2.vc1 FROM
 t2 JOIN t3 ON t2.vc2=t3.vc);




36   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: subqueries
 mysql> EXPLAIN SELECT * FROM t1, t2 WHERE t2.a IN (SELECT a
 FROM t1 WHERE t1.b <> 30) AND t1.b=t2.a GROUP BY t1.a;
                select                                                            possible
     id                                        table            type                                        key            key len                  ref         rows    extra
                type                                                              keys
     1          SIMPLE                         t2               index             a                         a               5                       NULL        4       Using where; Using index;
                                                                                                                                                                        Using temporary; Using filesort
     1          SIMPLE                         t1               ref               a                         a               5                       test.t2.a   101     Using where; Using index; Start
                                                                                                                                                                        temporary; End temporary


     1          SIMPLE                         t1               index             NULL                      a               10                      NULL        10004   Using where; Using index;
                                                                                                                                                                        Using join buffer (Block Nested
                                                                                                                                                                        Loop)




37       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: subqueries
     EXPLAIN
     "query_block": {                                                                                                             {
        "select_id": 1,                                                                                                               "duplicates_removal": {
        "grouping_operation": {                                                                                                           "using_temporary_table": true,
          "using_temporary_table": true,                                                                                                  "table": {
          "nested_loop": [                                                                                                                   "table_name": "t1",
             {                                                                                                                               "attached_condition": "(`test`.`t1`.`b` <> 30)"
                 "table": {                                                                                                               } /* table */
                  "table_name": "t2",                                                                                                   } /* duplicates_removal */
                  "attached_condition": "(`test`.`t2`.`a` is not null)"                                                               }, {
                 } /* table */                                                                                                          "table": {
             },                                                                                                                           "table_name": "t1",
             … next 2 tables => …                                                                                                         "attached_condition": "(`test`.`t1`.`b` = `test`.`t2`.`a`)"
          ] /* nested_loop */ } /* grouping_operation */                                                                                  } /* table */
     } /* query_block */                                                                                                              }


38    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: subqueries
         MySQL Workbench 5.2



 SELECT * FROM t1, t2 WHERE t2.a
 IN (SELECT a FROM t1 WHERE t1.b
 <> 30) AND t1.b=t2.a GROUP BY t1.a;




39   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN in JSON: subqueries
     EXPLAIN
     "query_block": {                                                                                                              "query_block": {
        "select_id": 1,                                                                                                               "select_id": 1,
        "nested_loop": [                                                                                                              "nested_loop": [
          {                                                                                                                              {
              "table": {                                                                                                                     "table": {
                "table_name": "t2",                                                                                                              "table_name": "t1",
              } /* table */                                                                                                                      "access_type": "range", "loosescan": true,
          }, {                                                                                                                               } /* table */
              "table": {                                                                                                                 }, {
                "table_name": "t1",                                                                                                          "table": {
                "first_match": "t2",                                                                                                             "table_name": "t2",
                "attached_condition": "(`test`.`t1`.`b` <> 30)"                                                                              } /* table */
              } /* table */                                                                                                              }
     } ] /* nested_loop */ } /* query_block */                                                                                        ] /* nested_loop */ } /* query_block */


40    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAIN
         for non-select queries




41   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAINing non-select queries
  INSERT/REPLACE and INSERT/REPLACE SELECT
  Single- and multi-table UPDATE
  Single- and multi-table DELETE




42   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAINing non-select queries
 mysql> EXPLAIN UPDATE t1 SET a = 10 WHERE a < 10;
     id          select type                            table                   type            possible keys                               key     key len   ref    rows   extra
     1            SIMPLE                                 t1                     range           a,a_2                                        a      5         NULL   2      Using where;
                                                                                                                                                                            Using
                                                                                                                                                                            temporary




43       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAINing non-select queries
                                                                                                                                          EXPLAIN
                                                                                                                                            "query_block": {
                                                                                                                                                 "select_id": 1,
                                                                                                                                                 "table": {
                                                                                                                                                  "update": true,
 mysql> EXPLAIN FORMAT=JSON                                                                                                                       "table_name": "t1",
                                                                                                                                                  "access_type": "range",
 UPDATE t1 SET a = 10 WHERE
                                                                                                                                                  "possible_keys": [ "a", "a_2" ],
 a < 10;                                                                                                                                          "key": "a", "key_length": "5",
                                                                                                                                                  "rows": 2, "filtered": 100,
                                                                                                                                                  "using_temporary_table": "for update",
                                                                                                                                                  "attached_condition": "(`test`.`t1`.`a` < 10)"
                                                                                                                                                 } /* table */
                                                                                                                                                } /* query_block */



44   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAINing non-select queries
                                                                                                                                          EXPLAIN
                                                                                                                                            "query_block": {
                                                                                                                                                 "select_id": 1,
                                                                                                                                                 "table": {
                                                                                                                                                  "delete": true,
 mysql> EXPLAIN FORMAT=JSON                                                                                                                       "table_name": "t1",
                                                                                                                                                  "access_type": "range",
 DELETE FROM t1 WHERE a < 10;
                                                                                                                                                  "possible_keys": [ "a", "a_2“ ],
                                                                                                                                                  "key": "a", "key_length": "5",
                                                                                                                                                  "rows": 2, "filtered": 100,
                                                                                                                                                  "attached_condition": "(`test`.`t1`.`a` < 10)"
                                                                                                                                                 } /* table */
                                                                                                                                                } /* query_block */




45   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAINing non-select queries
 mysql> EXPLAIN UPDATE t1 t11, (SELECT * FROM t2) t12 SET t11.a =10
 WHERE t11.a = 1;
                select                                                                         possible
     id                                        table                            type                                      key                key len   ref     rows   extra
                type                                                                           keys
     1          PRIMARY                        t11                              ref             a,a_2                      a                  5        const   1      NULL

     1          PRIMARY                        <derived2>                       ALL             NULL                       NULL               NULL     NULL    3      Using join buffer (Block
                                                                                                                                                                      Nested Loop)
     2          DERIVED                        t2                               ALL             NULL                       NULL               NULL     NULL    3      NULL




46       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.    Insert Information Protection Policy Classification from Slide 12
EXPLAINing non-select queries
     EXPLAIN
      "query_block": {                                                                                                                   {
        "select_id": 1,                                                                                                                      "table": {
        "nested_loop": [ {                                                                                                                       "table_name": "t12",
             "table": {                                                                                                                          "access_type": "ALL",
                "update": true,                                                                                                                  "rows": 3, "filtered": 100,
                "table_name": "t11",                                                                                                             "using_join_buffer": "Block Nested Loop",
                "access_type": "ref",                                                                                                            "materialized_from_subquery": {
                "possible_keys": [ "a", "a_2“ ],                                                                                                  "using_temporary_table": true,
                "key": "a", "key_length": "5",                                                                                                    "query_block": {
                "ref": ["const"] /* ref */,                                                                                                        "select_id": 2,
                "rows": 1, "filtered": 100                                                                                                         "table": { … } /* table */
             } /* table */ },                                                                                                                     } /* query_block */
          … here goes                  2nd    table…=>                                                                                           } /* materialized_from_subquery */
        ] /* nested_loop */ } /* query_block */                                                                                              } /* table */ }


47    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAINing non-select queries
 mysql> EXPLAIN UPDATE t1 SET a = 10 WHERE a IN
 (SELECT a FROM t2);
     id                  select type                         table              type              possible keys                         key         key len          ref   rows   extra
     1                   PRIMARY                              t1                ALL                a,a_2                                 NULL       NULL      NULL         3      NULL

     1                   DEPENDENT                            t2                ALL                NULL                                  NULL       NULL      NULL         3      Using where
                         SUBQUERY




48       Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
EXPLAINing non-select queries
     EXPLAIN
     "query_block": {                                                                                                              [{
        "select_id": 1,                                                                                                                          "dependent": true, "cacheable": false,
        "table": {                                                                                                                               "query_block": {
          "update": true,                                                                                                                         "select_id": 2,
          "table_name": "t1",                                                                                                                     "table": {
          "possible_keys": [ "a", "a_2“ ],                                                                                                         "table_name": "t2",
          "attached_condition":                                                                                                                    "access_type": "ALL",
             "<in_optimizer>(`test`.`t1`.`a`,                                                                                                      "rows": 3, "filtered": 100,
              <exists>(select 1 from `test`.`t2` where                                                                                             "attached_condition":
                                (<cache>(`test`.`t1`.`a`) =                                                                                          "(<cache>(`test`.`t1`.`a`) = `test`.`t2`.`b`)"
                                  `test`.`t1`.`a`)))",                                                                                            } /* table */
            "attached_subqueries": [ … => …]                                                                                                     } /* query_block */
         } /* table */                                                                                                                  }]
      } /* query_block */


49    Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12
50   Copyright © 2012, Oracle and/or its affiliates. All rights reserved.   Insert Information Protection Policy Classification from Slide 12

Powerful Explain in MySQL 5.6

  • 1.
    1 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 2.
    Powerful EXPLAIN in MySQL5.6 Evgeny Potemkin MySQL Optimizer team 2 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 3.
    Program Agenda  Introduction  Optimizer trace  EXPLAIN in JSON format  Explaining INSERT, UPDATE, DELETE 3 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 4.
    Graphic Section Divider 4 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 5.
    Optimizer Trace 5 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 6.
    Optimizer trace: quickstart mysql> SET optimizer_trace= “enabled=on“, end_markers_in_json=on; mysql> SELECT * FROM orders WHERE customer_id <> 6; mysql> SELECT trace FROM information_schema.OPTIMIZER_TRACE; mysql> SET optimizer_trace="enabled=off"; QUERY SELECT * FROM orders WHERE customer_id <> 6 TRACE “steps”: [ { "join_preparation": { "select#": 1,… } … } …] MISSING_BYTES_BEYOND_MAX_MEM_SIZE 0 INSUFFICIENT_PRIVILEGES 0 6 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 7.
    Optimizer trace: what’sinside  Steps – Join preparation  Modifications to subquery  Expanded query – Join optimization  Range optimizer info  Plan generation process  Condition optimization  Processing of ORDER/GROUP BY – Join execution  Tmp tables related info 7 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 8.
    Optimizer trace: example mysql>SET optimizer_trace="enabled=on"; mysql> SELECT * FROM t1,t2 WHERE f1=1 AND f1=f2 AND f2>0; mysql> SELECT trace FROM information_schema.optimizer_trace; mysql> SET optimizer_trace="enabled=off"; 8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 9.
    Optimizer trace: example join_optimization / steps / condition_processing "condition": "WHERE", "original_condition": "((`t1`.`f1` = 1) and (`t1`.`f1` = `t2`.`f2`) and (`t2`.`f2` > 0))", "steps": [ { "transformation": "equality_propagation", "resulting_condition": "((`t2`.`f2` > 0) and multiple equal(1, `t1`.`f1`, `t2`.`f2`))" }, { "transformation": "constant_propagation", "resulting_condition": "((1 > 0) and multiple equal(1, `t1`.`f1`, `t2`.`f2`))" }, { "transformation": "trivial_condition_removal", "resulting_condition": "multiple equal(1, `t1`.`f1`, `t2`.`f2`)" } ] /* steps */ 9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 10.
    Optimizer trace: example join_optimization / row_estimation / table : t1, range_analysis "table_scan": { "analyzing_range_alternatives": { "rows": 5, "range_scan_alternatives": [ { "cost": 5.1085 "index": "f1", } /* table_scan */, "ranges": [ "1 <= f1 <= 1“ ] /* ranges */, "potential_range_indices": [ { "index_dives_for_eq_ranges": true, "index": "f1", "usable": true, "rows": 2, "cost": 3.41, "key_parts": [ "f1“ ] /* key_parts */ } "chosen": false, ] /* potential_range_indices */, "cause": "cost" "best_covering_index_scan": { } "index": "f1", "cost": 2.093, "chosen": true ] /* range_scan_alternatives */, } /* best_covering_index_scan */, "analyzing_roworder_intersect": { "group_index_range": { "usable": false, "cause": "too_few_roworder_scans" "chosen": false, "cause": "not_single_table" } /* analyzing_roworder_intersect */ } /* group_index_range */, } /* analyzing_range_alternatives */ 10 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 11.
    Optimizer trace: example join_optimization / considered_execution_plan "plan_prefix": [ ] /* plan_prefix */, "plan_prefix": [ "`t2`“ ] /* plan_prefix */, "table": "`t2`", "table": "`t1`", "best_access_path": { "best_access_path": { "considered_access_paths": [ { "considered_access_paths": [ { "access_type": "scan", "rows": 3, "access_type": "ref", "index": "f1", "cost": 2.6051, "chosen": true "rows": 2, "cost": 3.4698, "chosen": true } ] /* considered_access_paths */ }, { } /* best_access_path */, "access_type": "scan", "using_join_cache": true, "cost_for_plan": 2.6051, "rows": 2, "cost": 3.8087, "rows_for_plan": 3, "chosen": true "rest_of_plan": [ { } ] } /* best_access_path */, … next iteration … => "cost_for_plan": 6.4138, } ] /* rest_of_plan */ "rows_for_plan": 6, "chosen": true 11 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 12.
    Optimizer trace: example join_optimization / plan refinement { { "attaching_conditions_to_tables": { "refine_plan": [ "original_condition": { "((`t1`.`f1` = 1) and (`t2`.`f2` = 1))", "table": "`t1`" "attached_conditions_summary": [ { }, "table": "`t1`", "attached": null { }, { "table": "`t2`", "table": "`t2`", "attached": "(`t2`.`f2` = 1)" "access_type": "table_scan" } } ] /* attached_conditions_summary */ ] /* refine_plan */ } /* attaching_conditions_to_tables */ } }, 12 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 13.
    Optimizer trace: controls  optimizer-trace  optimizer-trace-features – greedy_search – range_optimizer – dynamic_range – repeated_subselect  optimizer-trace-limit  optimizer-trace-offset  optimizer-trace-max-mem-size  end-markers-in-json 13 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 14.
    Optimizer trace: exploring  mysql> pager less  SELECT TRACE INTO DUMPFILE <filename> FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;  JSONView for Firefox  Pretty JSON for Chrome  A registry patch for IE 14 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 15.
    EXPLAIN in JSONformat 15 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 16.
    Traditional EXPLAIN id selecttype table type possible key key len ref rows extra keys 1 PRIMARY cust index cust_id, cust_name 40 const 10 Using where; Start cust_name materialize; Scan 1 PRIMARY orders ref order_id order_id 8 ordid 32 Using where; End materialize; Using join buffer (Block Nested Loop) 1 PRIMARY <derived2> ref <auto_key0> <auto_key0> 23 vc1 100 Using index 2 DERIVED flights ALL NULL NULL NULL NULL 10000 Using where; Start materialize; Scan 2 DERIVED storage eq_ref PRIMARY PRIMARY 8 fl_store 1 Using where; End materialize; Using join buffer (Block Nested Loop) 4 SUBQUERY buzz range buzzez buzzez 11 NULL 42 Using index; Using where 6 SUBQUERY shortage index shrt_idx shrt_idx 100 NULL 243 Using index; Using where 16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 17.
    Traditional EXPLAIN: Cons  Hard to read in complex cases  Unclear/non-obvious query structure in complex cases – Could take quite some time to see what’s going on  Limited information about query plan – How conditions are split? When a subquery is evaluated?  Hard to impossible to extend – Indication of a feature being used usually is limited to “Using <something>” 17 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 18.
    EXPLAIN in JSON:Basics mysql> EXPLAIN SELECT * FROM t1; id select type table type possible keys key key len ref rows extra 1 SIMPLE t1 index NULL i 5 NULL 3 NULL 18 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 19.
    EXPLAIN in JSON:Basics EXPLAIN "query_block": { "select_id": 1, "table": { "table_name": "t1", mysql> EXPLAIN FORMAT=JSON "access_type": "index", SELECT * FROM t1; "key": "i", "key_length": "5", "rows": 3, "filtered": 100, "using_index": true } /* table */ } /* query_block */ 19 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 20.
    EXPLAIN in JSON:Basics mysql> set end_markers_in_json = on; end_markers_in_json = off end_markers_in_json = on } } /* table */ } } /* query_block */ } } ] ] /* query_specifications */ } } /* union_result */ } } /* query_block */ } } /* materialized_from_subquery */ } } /* table */ } } /* grouping_operation */ } } /* ordering_operation */ } } /* query_block */ 20 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 21.
    EXPLAIN in JSON:Basics mysql> EXPLAIN SELECT * FROM t1 WHERE str IS NULL AND id > 0; id select type table type possible keys key key len ref rows extra 1 SIMPLE t1 ref str str 11 const 1 Using index condition; Using where 21 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 22.
    EXPLAIN in JSON:Basics EXPLAIN "query_block": { "select_id": 1, "table": { "table_name": "t1", mysql> EXPLAIN FORMAT=JSON "access_type": "ref", SELECT * FROM t1 WHERE str IS "possible_keys": [ "str“ ], "key": "str", "key_length": "11", NULL AND id > 0; "ref": [ "const"], "rows": 1, "filtered": 100, "index_condition": "isnull(`test`.`t1`.`str`)“ "attached_condition": "(`test`.`t1`.`id` > 0)" } /* table */ } /* query_block */ 22 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 23.
    EXPLAIN in JSON:Basics mysql> EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1001-01-01'; possible key id select type partitions table type key ref rows extra keys len 1 SIMPLE pNULL, t1 index a a 4 NULL 6 Using where; p0001-01-01, Using index p1001-01-01 23 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 24.
    EXPLAIN in JSON:Basics EXPLAIN "query_block": { "select_id": 1, "table": { "table_name": "t1", mysql> EXPLAIN FORMAT=JSON "partitions": [ "pNULL", "p0001-01-01", SELECT * FROM t1 WHERE a "p1001-01-01"], "access_type": "index", BETWEEN '0000-00-00' AND '1001- "possible_keys": [ "a" ], 01-01'; "key": "a", "key_length": "4", "rows": 6, "filtered": 33.333, "using_index": true, "attached_condition": "(`test`.`t1`.`a` between '0000-00-00' and '1001-01-01')" }} 24 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 25.
    EXPLAIN in JSON:Basics mysql> EXPLAIN SELECT * FROM t1 JOIN t2 USING(i); possible key id select type table type key ref rows extra keys len 1 SIMPLE t2 ALL NULL NULL NULL NULL 3 NULL 1 SIMPLE t1 index i i 5 NULL 3 Using where; Using index; Using join buffer (Block Nested Loop) 25 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 26.
    EXPLAIN in JSON:Basics EXPLAIN { { "query_block": { "table": { "select_id": 1, "table_name": "t1", "nested_loop": [ "access_type": "index", { "possible_keys": [ "i" ], "table": { "key": "i", "key_length": "5", "table_name": "t2", "rows": 3, "filtered": 100, "access_type": "ALL", "using_index": true, "rows": 3, "using_join_buffer": "Block Nested Loop", "filtered": 100 "attached_condition": } /* table */ "(`test`.`t1`.`i` = `test`.`t2`.`i`)" }, } /* table */ … next table … => } ] } /* nested_loop */ } /* query_block */ 26 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 27.
    EXPLAIN in JSON:Basics MySQL Workbench 5.2 SELECT * FROM t1 JOIN t2 USING(i); 27 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 28.
    EXPLAIN in JSON:more complex queries mysql> EXPLAIN FORMAT=JSON SELECT t1.a, MIN(t2.b) FROM t1 LEFT JOIN t2 ON t1.a=t2.a GROUP BY t1.a ORDER BY t1.b; id select type table type possible keys key key len ref rows extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using temporary; Using filesort 1 SIMPLE t2 ref PRIMARY PRIMARY 4 test.t1.a 1 Using index 28 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 29.
    EXPLAIN in JSON:more complex queries EXPLAIN "query_block": { mysql> EXPLAIN FORMAT=JSON "select_id": 1, SELECT t1.a, MIN(t2.b) "ordering_operation": { "using_temporary_table": true, FROM t1 LEFT JOIN t2 "using_filesort": true, "grouping_operation": { ON t1.a=t2.a "using_filesort": true, GROUP BY t1.a ORDER BY t1.b; "nested_loop": [ { “table” : {…} }, { “table” : {…} } ] /* nested_loop */ } /* grouping_operation */ } /* ordering_operation */ } /* query_block */ 29 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 30.
    EXPLAIN in JSON:more complex queries MySQL Workbench 5.2 SELECT t1.a, MIN(t2.b) FROM t1 LEFT JOIN t2 ON t1.a=t2.a GROUP BY t1.a ORDER BY t1.b; 30 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 31.
    EXPLAIN in JSON:more complex queries mysql> EXPLAIN SELECT a, b FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 1 AS a, 2 AS b) t1 GROUP BY a ORDER BY b DESC; possible key id select type table type key ref rows extra keys len 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 Using temporary; Using filesort 2 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used 3 UNION NULL NULL NULL NULL NULL NULL NULL No tables used NULL UNION <union2,3> ALL NULL NULL NULL NULL NULL Using temporary RESULT 31 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 32.
    EXPLAIN in JSON:more complex queries EXPLAIN "query_block": { "union_result": { "ordering_operation": { "using_temporary_table": true, "using_filesort": true, "table_name": "<union2,3>", "grouping_operation": { "query_specifications": [ { "using_temporary_table": true, "query_block": { "using_filesort": false, "table": { "table": { "message": "No tables used" "table_name": "t1", } } }, { "materialized_from_subquery": { "query_block": { "using_temporary_table": true, "table": { "query_block": { "message": "No tables used" … here goes union_result… => } } } ] } /* union_result */ }}}}}} 32 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 33.
    EXPLAIN in JSON:more complex queries MySQL Workbench 5.2 SELECT a, b FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 1 AS a, 2 AS b) t1 GROUP BY a ORDER BY b DESC; 33 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 34.
    EXPLAIN in JSON:subqueries mysql> EXPLAIN FORMAT=JSON SELECT derived.vc FROM (SELECT * FROM t1) AS derived WHERE derived.vc IN (SELECT t2.vc1 FROM t2 JOIN t3 ON t2.vc2=t3.vc); select possible key id table type key ref rows extra type keys len 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Start materialize; Scan 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where; End materialize; Using join buffer (Block Nested Loop) 1 PRIMARY <derived2> ref <auto_key0> <auto_key0> 23 t2.vc1 2 NULL 2 DERIVED t1 ALL NULL NULL NULL NULL 2 NULL 34 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 35.
    EXPLAIN in JSON:subqueries EXPLAIN "query_block": { "table": { "select_id": 1, "table_name": "derived", "nested_loop": [ "materialized_from_subquery": { { "table": { "using_temporary_table": true, "table_name": "<subquery3>", "query_block": { "materialized_from_subquery": { "select_id": 2, "query_block": { "table": { "nested_loop": [ "table_name": "t1“, { "table": { "table_name": "t2“ } }, … { "table": { "table_name": "t3“ } } } ] } /* query_block */ } } /* table */ } /* query_block */ }, { … next table goes here … => } } /* materialized_from_subquery */ ] /* nested_loop */ } /* query_block */ } /* table */ 35 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 36.
    EXPLAIN in JSON:subqueries MySQL Workbench 5.2 SELECT derived.vc FROM (SELECT * FROM t1) AS derived WHERE derived.vc IN (SELECT t2.vc1 FROM t2 JOIN t3 ON t2.vc2=t3.vc); 36 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 37.
    EXPLAIN in JSON:subqueries mysql> EXPLAIN SELECT * FROM t1, t2 WHERE t2.a IN (SELECT a FROM t1 WHERE t1.b <> 30) AND t1.b=t2.a GROUP BY t1.a; select possible id table type key key len ref rows extra type keys 1 SIMPLE t2 index a a 5 NULL 4 Using where; Using index; Using temporary; Using filesort 1 SIMPLE t1 ref a a 5 test.t2.a 101 Using where; Using index; Start temporary; End temporary 1 SIMPLE t1 index NULL a 10 NULL 10004 Using where; Using index; Using join buffer (Block Nested Loop) 37 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 38.
    EXPLAIN in JSON:subqueries EXPLAIN "query_block": { { "select_id": 1, "duplicates_removal": { "grouping_operation": { "using_temporary_table": true, "using_temporary_table": true, "table": { "nested_loop": [ "table_name": "t1", { "attached_condition": "(`test`.`t1`.`b` <> 30)" "table": { } /* table */ "table_name": "t2", } /* duplicates_removal */ "attached_condition": "(`test`.`t2`.`a` is not null)" }, { } /* table */ "table": { }, "table_name": "t1", … next 2 tables => … "attached_condition": "(`test`.`t1`.`b` = `test`.`t2`.`a`)" ] /* nested_loop */ } /* grouping_operation */ } /* table */ } /* query_block */ } 38 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 39.
    EXPLAIN in JSON:subqueries MySQL Workbench 5.2 SELECT * FROM t1, t2 WHERE t2.a IN (SELECT a FROM t1 WHERE t1.b <> 30) AND t1.b=t2.a GROUP BY t1.a; 39 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 40.
    EXPLAIN in JSON:subqueries EXPLAIN "query_block": { "query_block": { "select_id": 1, "select_id": 1, "nested_loop": [ "nested_loop": [ { { "table": { "table": { "table_name": "t2", "table_name": "t1", } /* table */ "access_type": "range", "loosescan": true, }, { } /* table */ "table": { }, { "table_name": "t1", "table": { "first_match": "t2", "table_name": "t2", "attached_condition": "(`test`.`t1`.`b` <> 30)" } /* table */ } /* table */ } } ] /* nested_loop */ } /* query_block */ ] /* nested_loop */ } /* query_block */ 40 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 41.
    EXPLAIN for non-select queries 41 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 42.
    EXPLAINing non-select queries  INSERT/REPLACE and INSERT/REPLACE SELECT  Single- and multi-table UPDATE  Single- and multi-table DELETE 42 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 43.
    EXPLAINing non-select queries mysql> EXPLAIN UPDATE t1 SET a = 10 WHERE a < 10; id select type table type possible keys key key len ref rows extra 1 SIMPLE t1 range a,a_2 a 5 NULL 2 Using where; Using temporary 43 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 44.
    EXPLAINing non-select queries EXPLAIN "query_block": { "select_id": 1, "table": { "update": true, mysql> EXPLAIN FORMAT=JSON "table_name": "t1", "access_type": "range", UPDATE t1 SET a = 10 WHERE "possible_keys": [ "a", "a_2" ], a < 10; "key": "a", "key_length": "5", "rows": 2, "filtered": 100, "using_temporary_table": "for update", "attached_condition": "(`test`.`t1`.`a` < 10)" } /* table */ } /* query_block */ 44 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 45.
    EXPLAINing non-select queries EXPLAIN "query_block": { "select_id": 1, "table": { "delete": true, mysql> EXPLAIN FORMAT=JSON "table_name": "t1", "access_type": "range", DELETE FROM t1 WHERE a < 10; "possible_keys": [ "a", "a_2“ ], "key": "a", "key_length": "5", "rows": 2, "filtered": 100, "attached_condition": "(`test`.`t1`.`a` < 10)" } /* table */ } /* query_block */ 45 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 46.
    EXPLAINing non-select queries mysql> EXPLAIN UPDATE t1 t11, (SELECT * FROM t2) t12 SET t11.a =10 WHERE t11.a = 1; select possible id table type key key len ref rows extra type keys 1 PRIMARY t11 ref a,a_2 a 5 const 1 NULL 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 Using join buffer (Block Nested Loop) 2 DERIVED t2 ALL NULL NULL NULL NULL 3 NULL 46 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 47.
    EXPLAINing non-select queries EXPLAIN "query_block": { { "select_id": 1, "table": { "nested_loop": [ { "table_name": "t12", "table": { "access_type": "ALL", "update": true, "rows": 3, "filtered": 100, "table_name": "t11", "using_join_buffer": "Block Nested Loop", "access_type": "ref", "materialized_from_subquery": { "possible_keys": [ "a", "a_2“ ], "using_temporary_table": true, "key": "a", "key_length": "5", "query_block": { "ref": ["const"] /* ref */, "select_id": 2, "rows": 1, "filtered": 100 "table": { … } /* table */ } /* table */ }, } /* query_block */ … here goes 2nd table…=> } /* materialized_from_subquery */ ] /* nested_loop */ } /* query_block */ } /* table */ } 47 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 48.
    EXPLAINing non-select queries mysql> EXPLAIN UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2); id select type table type possible keys key key len ref rows extra 1 PRIMARY t1 ALL a,a_2 NULL NULL NULL 3 NULL 1 DEPENDENT t2 ALL NULL NULL NULL NULL 3 Using where SUBQUERY 48 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 49.
    EXPLAINing non-select queries EXPLAIN "query_block": { [{ "select_id": 1, "dependent": true, "cacheable": false, "table": { "query_block": { "update": true, "select_id": 2, "table_name": "t1", "table": { "possible_keys": [ "a", "a_2“ ], "table_name": "t2", "attached_condition": "access_type": "ALL", "<in_optimizer>(`test`.`t1`.`a`, "rows": 3, "filtered": 100, <exists>(select 1 from `test`.`t2` where "attached_condition": (<cache>(`test`.`t1`.`a`) = "(<cache>(`test`.`t1`.`a`) = `test`.`t2`.`b`)" `test`.`t1`.`a`)))", } /* table */ "attached_subqueries": [ … => …] } /* query_block */ } /* table */ }] } /* query_block */ 49 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12
  • 50.
    50 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12