SlideShare une entreprise Scribd logo
1  sur  14
Explaining the
                                                                              MySQL EXPLAIN

                                                                                         Ronald Bradford
                                                                                    http://ronaldbradford.com
                                                                                             RMOUG QEW
                                                                                        Denver, Colorado - 2012.05


                                                                         EffectiveMySQL.com - Performance, Scalability & Business Continuity




                                     Agenda                                                                PURPOSE

EXPLAIN syntax options                                                 EXPLAIN is used for
How to read QEP                                                         Determine Query Execution Plan (QEP)
QEP examples                                                            Understand MySQL optimizer
MySQL optimizer limitations                                             Identify information needed for tuning
                                                                        Not providing tuning recommendations
                     Slides at http://j.mp/EM-Explain

 EffectiveMySQL.com - Performance, Scalability & Business Continuity     EffectiveMySQL.com - Performance, Scalability & Business Continuity
ABOUT THE AUTHOR
                           Ronald Bradford

 2011 - All time top blog contributor to Planet MySQL
 2010 - Published Author of Expert PHP & MySQL
 2010 - Oracle ACE Director (first in MySQL)
 2009 - MySQL community member of the year
 22 years of RDBMS experience,12 years with MySQL
     MySQL Inc (2006-2008)
     Oracle Corporation (1996-1999)
 Provide independent consulting - Available NOW



  EffectiveMySQL.com - Performance, Scalability & Business Continuity    EffectiveMySQL.com - Performance, Scalability & Business Continuity




                          MySQL QEP                                                                          SYNTAX

 Cost Based Optimizer                                                   EXPLAIN SELECT ...
 No pinning                                                               EXPLAIN PARTITIONS SELECT ...

 Few hints                                                                EXPLAIN EXTENDED SELECT ...

 Calculated every SQL execution
   Except Query Cache                                                   Does not support UPDATE,DELETE
                                                                                                            Now supported in MySQL 5.6




  EffectiveMySQL.com - Performance, Scalability & Business Continuity    EffectiveMySQL.com - Performance, Scalability & Business Continuity
Support Commands                                                                                                EXPLAIN USAGE

     SHOW CREATE TABLE                                                                                  Does not execute query
     SHOW INDEXES
     SHOW TABLE STATUS
                                                                                                        BUT
     INFORMATION_SCHEMA
     SHOW VARIABLES                                                                                                                                      WARNING !!!

     SHOW STATUS                                                                                        May execute portions
     Optimizer Trace - Since 5.6.3                                                                        e.g. derived tables

        EffectiveMySQL.com - Performance, Scalability & Business Continuity                              EffectiveMySQL.com - Performance, Scalability & Business Continuity




                                                            EXPLAIN                                                          Attributes
                                                                               Example
    mysql> EXPLAIN SELECT * FROM proposal WHERE post_id=16102176;
                                                                                                                                   mysql> EXPLAIN SELECT * FROM inventory
+----+-------------+----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table    | type | possible_keys | key | key_len | ref | rows     | Extra       |   id                             -> WHERE item_id = 16102176G
+----+-------------+----------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE       | proposal | ALL | NULL           | NULL | NULL    | NULL | 787126 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+--------+-------------+
                                                                                                        select_type                ************************** 1. row ********
                                                                                                                                              id: 1
                                                                                                        table                        select_type: SIMPLE
                                                                                                                                           table: inventory
mysql> EXPLAIN SELECT * FROM inventory
    -> WHERE item_id = 16102176G                                                                       type                                type: ref
*************************** 1. row **********************
           id: 1                                                                                        possible_keys              possible_keys: item_id
  select_type: SIMPLE                                                                                                                        key: item_id
        table: inventory                                                                                key                              key_len: 4
         type: ref
                                                                                                                                             ref: const
possible_keys: item_id
          key: item_id
                                                                                                        key_len                             rows: 1
      key_len: 4
          ref: const                                                                                    ref                                Extra:
         rows: 1
        Extra:                               MySQL Client                 SQL Statement Terminator      rows
                                                                               ;
                                                                             G                         Extra

        EffectiveMySQL.com - Performance, Scalability & Business Continuity                              EffectiveMySQL.com - Performance, Scalability & Business Continuity
Essential                                                                     Essential
                                                Example                                                                       Example
                           mysql> EXPLAIN SELECT * FROM inventory                                        mysql> EXPLAIN SELECT * FROM inventory
id                             -> WHERE item_id = 16102176G                  id                             -> WHERE item_id = 16102176G
select_type                                                                   select_type
                           ************************* 1. row **********************                       ************************* 1. row *********
                                                                                                         *************************** 1. row *******
                                      id: 1                                                                         id: 1
table                        select_type: SIMPLE                              table                        select_type: SIMPLE
                                   table: inventory                                                              table: inventory
type                                type: ref                                 type                                type: ref
                                                                                                                        ALL
possible_keys
                                                             !                possible_keys
                                                                                                                                            "
                           possible_keys: item_id                                                        possible_keys: item_id
                                                                                                                        NULL
                                     key: item_id                                                                  key: item_id
                                                                                                                        NULL
key                              key_len: 4                                   key                              key_len: 4
                                                                                                                        NULL
                                     ref: const                                                                    ref: const
                                                                                                                        NULL
key_len                             rows: 1                                   key_len                             rows: 1
                                                                                                                        787338
ref                                Extra:                                     ref                                Extra: Using where

rows                                                                         rows
Extra                                                                        Extra

 EffectiveMySQL.com - Performance, Scalability & Business Continuity           EffectiveMySQL.com - Performance, Scalability & Business Continuity




                           Essential                                                                                                    key
                                                Example
                           mysql> EXPLAIN SELECT * FROM inventory
id                             -> WHERE item_id = 16102176G                 Identify index to be used
select_type                ************************* 1. row **********************
                                      id: 1
table                        select_type: SIMPLE                             Generally only one per table (*)
                                   table: inventory
type                                type: ref
possible_keys
                                                             !
                           possible_keys: item_id
                                     key: item_id
key                              key_len: 4                                  Associated attributes                                    id: 1
                                     ref: const
key_len                             rows: 1                                                                                 select_type: SIMPLE
ref                                Extra:                                       possible_keys                                      table: inventory
                                                                                                                                    type: ALL
rows                                                                                                                      possible_keys: NULL

Extra                                                                           key_len                                              key: NULL
                                                                                                                                key_len: NULL
                                                                                                                                     ref: NULL
                                                                                                                                    rows: 787338
                                                                                                                                   Extra: Using where
 EffectiveMySQL.com - Performance, Scalability & Business Continuity           EffectiveMySQL.com - Performance, Scalability & Business Continuity
key                                                                                                           key
                                                    Example                                                     MERGE INDEX Example
mysql> EXPLAIN SELECT * FROM inventory                                        mysql> EXPLAIN SELECT id FROM users
    -> WHERE item_id = 16102176G                                                 -> WHERE first = 'west' OR last='west'G
*************************** 1. row **********************                     ************************ 1. row **********************
           id: 1                                                                         id: 1                     CREATE TABLE `users` (
                                                                                                                     `id` int(10) unsigned NOT NULL AUTO_INCRE
  select_type: SIMPLE                                                           select_type: SIMPLE                  `first` varchar(20) NOT NULL,
        table: inventory                                                              table: users                   `last` varchar(20) NOT NULL,
         type: ref                                                                     type: index_merge             `username` varchar(20) NOT NULL,
                                                                                                                     `last_login` timestamp NOT NULL DEFAULT C
possible_keys: item_id                                                        possible_keys: first,last              PRIMARY KEY (`id`),
          key: item_id                                                                  key: first,last              KEY `first` (`first`),
      key_len: 4                                                                    key_len: 22,22                   KEY `last` (`last`),
                                                                                                                     KEY `username` (`username`) ...
          ref: const                                                                    ref: NULL
         rows: 1                                                                       rows: 2
        Extra:                                                                        Extra: Using union(first,last); Using where




     EffectiveMySQL.com - Performance, Scalability & Business Continuity              EffectiveMySQL.com - Performance, Scalability & Business Continuity




                            possible_keys                                                                                 possible_keys
                                                                                                                                                             Example

   Indexes the optimizer considered
                                                                              | 1 | SIMPLE
                                                                              | 1 | SIMPLE
                                                                                                 | c
                                                                                                 | i
                                                                                                         | const | PRIMARY
                                                                                                         | ALL   | customer_id
                                                                                                                                 | PRIMARY | 4
                                                                                                                                 | NULL    | NULL
                                                                                                                                                             | rows | Extra

                                                                                                                                                     | const |
                                                                                                                                                     | NULL |
                                                                                                                                                                                  "
                                                                              +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------+
                                                                              | id | select_type | table | type | possible_keys | key      | key_len | ref                           |
                                                                              +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------+
                                                                                                                                                                  1 | Using filesort |
                                                                                                                                                                  7 | Using where    |

      Why was no index used?                                                  +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------+




      Too many is not good
                                                            id: 1             | 1 | SIMPLE       | i     | ref   | customer_id
                                                                                                                                               | key_len | ref


                                                                                                                                 | customer_id | 4
                                                                                                                                                                 | rows | Extra

                                                                                                                                                         | const |
                                                                                                                                                         | const |
                                                                                                                                                                      1 |       !
                                                                              +----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+
                                                                              | id | select_type | table | type | possible_keys | key                                                 |
                                                                              +----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+
                                                                              | 1 | SIMPLE       | c     | const | PRIMARY       | PRIMARY     | 4                                    |
                                                                                                                                                                      5 | Using where |
                                                                              +----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+
                                                  select_type: SIMPLE
   Associated attributes                                 table: inventory
                                                          type: ALL
                                                possible_keys: NULL
      key                                                  key: NULL
                                                      key_len: NULL
                                                           ref: NULL
                                                          rows: 787338
                                                         Extra: Using where
     EffectiveMySQL.com - Performance, Scalability & Business Continuity              EffectiveMySQL.com - Performance, Scalability & Business Continuity
possible_keys                                                                                                               rows
                                                        Example
************************ 1. row **********************
           id: 1                                                                                     Estimated number of table rows (*)
  select_type: SIMPLE
        table: FES
         type: ref
possible_keys: FK_ASI,EmpID1,Idx_SID,SuleUnitsEmpSuled,Idx_SUnitID

                                                                                                     Associated attributes
          key: SuleUnitsEmpSuled
      key_len: 8
          ref: FSU.SUnitId,FS.SID
         rows: 26
        Extra:         ..                                                                               key                                                id: 1
                                                                                                                                                 select_type: SIMPLE
                          PRIMARY KEY (`ID`),
                          KEY `FK_ASI` (`ASID`),                                                                                                        table: inventory
                          KEY `EmpID1` (`EmpID`),                                                                                                        type: ALL
                          KEY `Idx_Composite` (`ID`,`EmpID`,`SUnitID`,`SID`,`SourceSID`,`ASID`),
                          KEY `Idx_SID` (`SID`),                                                                                               possible_keys: NULL
                          KEY `SuleUnitsEmpSuled` (`SUnitID`,`SID`),                                                                                      key: NULL
                          KEY `Idx_SUnitID` (`SUnitID`),
                                                                                                                                                     key_len: NULL
                                                                                                                                                          ref: NULL
                                                                                                                                                         rows: 787338
                                                                                                                                                        Extra: Using where
     EffectiveMySQL.com - Performance, Scalability & Business Continuity                            EffectiveMySQL.com - Performance, Scalability & Business Continuity




                                                    key_len                                                                                    key_len
                                                                                                                                       Calculations

      Amount of index used (*)                                                                     TINYINT - 1 byte                    CHAR(n) - n bytes
                                                                                                   SMALLINT - 2 bytes                  VARCHAR(n) - n bytes
         Multi column efficiency                                                                    INT - 4 bytes                       NULL + 1 byte
                                                                                                   BIGINT - 8 bytes                    VARCHAR + 2 bytes
                                                                                                   DATE - 3 bytes
      Associated attributes                                 id: 1
                                                  select_type: SIMPLE                              TIMESTAMP - 4 bytes                 Character set x [1-3] bytes
                                                         table: inventory
         Extra = Using Index                              type: ALL
                                                possible_keys: inv_id
                                                                                                   DATETIME - 8 bytes
                                                           key: inv_id
                                                      key_len: 4
                                                           ref: NULL
                                                          rows: 1
                                                         Extra: Using where
     EffectiveMySQL.com - Performance, Scalability & Business Continuity                            EffectiveMySQL.com - Performance, Scalability & Business Continuity
key_len                                   key_len with varchar
                                                    Example                                                                                                      Example
mysql> EXPLAIN SELECT user_id,balance,created                                     mysql> EXPLAIN SELECT *
    -> FROM accounts                                                                  -> FROM categories
    -> WHERE id = 42G                                                                -> WHERE name LIKE 'NCAA%';
*************************** 1. row **********************                         *************************** 1. row *********************
           id: 1                                                                             id: 1
  select_type: SIMPLE                                                               select_type: SIMPLE
        table: accounts                                                                   table: categories
         type: const                                                                       type: range
possible_keys: PRIMARY                   8 Bytes                                  possible_keys: name                                  30 Bytes + 2 Bytes
          key: PRIMARY      CREATE TABLE `accounts` (                                       key: name         CREATE TABLE categories (
      key_len: 8              `id` BIGINT NOT NULL AUTO_INCREMENT,                      key_len: 32             id int NOT NULL AUTO_INCREMENT,
          ref: const        ...                                                             ref: NULL           name VARCHAR(30) NOT NULL,
         rows: 1            PRIMARY KEY (id)                                               rows: 6            ...
        Extra:              ...                                                           Extra: Using where    INDEX (name)
                                                                                                              ...




     EffectiveMySQL.com - Performance, Scalability & Business Continuity                  EffectiveMySQL.com - Performance, Scalability & Business Continuity




               key_len with utf8                                                                                                                           key_len
                                                    Example                                                                                                      Example
                                                                                                                                      (1)                  (3)
                                                                           mysql> EXPLAIN SELECT ID, post_title FROM wp_posts WHERE post_type='post' and post_date > '2010-06-01';
mysql> EXPLAIN SELECT *                                                    +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+
    -> FROM categories                                                     | id | select_type | table    | type | possible_keys    | key              | key_len | ref   | rows | Extra       |
                                                                           +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+
    -> WHERE name LIKE 'NCAA%';                                            | 1 | SIMPLE       | wp_posts | ref | type_status_date | type_status_date | 62       | const | 1132 | Using where |
                                                                           +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+

***************************    1. row *********************
           id: 1
  select_type: SIMPLE
        table: categories
         type: range           (30 * 3) Bytes + 2 Bytes                                  CREATE TABLE `wp_posts` (
possible_keys: name            CREATE TABLE `categories` (                               ...
          key: name              `id` int(4) NOT NULL AUTO_INCREMENT,                      `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
      key_len: 92                `name` VARCHAR(30) NOT NULL,                              `post_status` varchar(20) NOT NULL DEFAULT 'publish',
          ref: NULL            ...                                                         `post_type` varchar(20) NOT NULL DEFAULT 'post',
         rows: 6                                                                         ...
        Extra: Using where
                                 INDEX (name)
                               ) ... DEFAULT CHARSET=utf8
                                                                                           PRIMARY KEY (`ID`),                     (1)               (2)                 (3)         (4)
                                                                                           KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
                                                                                         ) DEFAULT CHARSET=utf8

                                                                                                                                            62 + 62 + 8 + 8 Bytes
     EffectiveMySQL.com - Performance, Scalability & Business Continuity                  EffectiveMySQL.com - Performance, Scalability & Business Continuity
key_len                                                                                             select_type
                                                                                      Example
                                                           (1)                  (3)
mysql> EXPLAIN SELECT ID, post_title FROM wp_posts WHERE post_type='post' and post_date > '2010-06-01';
+----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+
| id | select_type | table    | type | possible_keys    | key              | key_len | ref   | rows | Extra       |
+----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+
                                                                                                                                    SIMPLE                                    DEPENDENT UNION
| 1 | SIMPLE       | wp_posts | ref | type_status_date | type_status_date | 62       | const | 1132 | Using where |
+----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+
                                                             (1)                 (2)                      (3)
mysql> EXPLAIN SELECT ID, post_title FROM wp_posts WHERE post_type='post' AND post_status='publish' AND post_date >
                                                                                                                                    PRIMARY                                   UNION RESULT
'2010-06-01';

| id | select_type | table    | type | possible_keys     | key             62+62+8 Bytes
+----+-------------+----------+-------+------------------+------------------+---------+------+------+-------------+
                                                                            | key_len | ref | rows | Extra        |
+----+-------------+----------+-------+------------------+------------------+---------+------+------+-------------+                 SUBQUERY                                  UNCACHEABLE QUERY
| 1 | SIMPLE       | wp_posts | range | type_status_date | type_status_date | 132     | NULL |    1 | Using where |
+----+-------------+----------+-------+------------------+------------------+---------+------+------+-------------+
              CREATE TABLE `wp_posts` (
              ...
                `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
                                                                                                                                    DERIVED                                   UNCACHEABLE UNION
                `post_status` varchar(20) NOT NULL DEFAULT 'publish',                                                                                                                         id: 1
                `post_type` varchar(20) NOT NULL DEFAULT 'post',
              ...                                                                                                                   UNION                                           select_type: SIMPLE
                                                                                                                                                                                           table: inventory
                PRIMARY KEY (`ID`),                     (1)               (2)                 (3)         (4)                                                                               type: ALL
                KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
              ) DEFAULT CHARSET=utf8
                                                                                                                                                                                  possible_keys: NULL
                                                                                                                                                                                             key: NULL
                                                                 62 + 62 + 8 + 8 bytes                                                                                                  key_len: NULL
                                                                                                                                                                                             ref: NULL
                                                                                                                                                                                            rows: 787338
               EffectiveMySQL.com - Performance, Scalability & Business Continuity                                                     EffectiveMySQL.com - Performance, Scalability & Business Continuity
                                                                                                                                                                                           Extra: Using where




                                                            select_type                                                                                                             select_type
                                                                                      Example                                                                                                                 Example
                                                                                                                                                            3 ways to get same query results
      mysql> EXPLAIN SELECT MAX(id) FROM (SELECT id FROM users WHERE first = 'west') c;                               mysql> EXPLAIN SELECT p.* FROM parent p WHERE p.id NOT IN (SELECT c.parent_id FROM child c);
      +----+-------------+------------+------+---------------+-------+---------+------+------+-------------+          +----+--------------------+-------+----------------+---------------+-----------+---------+------+------+-------------+
      | id | select_type | table      | type | possible_keys | key   | key_len | ref | rows | Extra        |          | id | select_type        | table | type           | possible_keys | key       | key_len | ref | rows | Extra        |
      +----+-------------+------------+------+---------------+-------+---------+------+------+-------------+          +----+--------------------+-------+----------------+---------------+-----------+---------+------+------+-------------+
      | 1 | PRIMARY      | <derived2> | ALL | NULL           | NULL | NULL     | NULL |    2 |             |          | 1 | PRIMARY             | p     | ALL            | NULL          | NULL      | NULL    | NULL |    7 | Using where |
      | 2 | DERIVED      | users      | ref | first          | first | 22      |      |    1 | Using where |          | 2 | DEPENDENT SUBQUERY | c      | index_subquery | parent_id     | parent_id | 4       | func |    2 | Using index |
      +----+-------------+------------+------+---------------+-------+---------+------+------+-------------+          +----+--------------------+-------+----------------+---------------+-----------+---------+------+------+-------------+

      mysql> EXPLAIN SELECT p.* FROM parent p WHERE p.val LIKE 'a%'                                                   mysql> EXPLAIN SELECT p.* FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE c.child_id IS NULL;
          -> UNION                                                         2 represents id                            +----+-------------+-------+------+---------------+-----------+---------+------+------+-------------------------+
          -> SELECT p.* FROM parent p WHERE p.id > 5;                                                                 | id | select_type | table | type | possible_keys | key       | key_len | ref | rows | Extra                    |
      +----+--------------+------------+-------+---------------+------+---------+------+------+-------------+         +----+-------------+-------+------+---------------+-----------+---------+------+------+-------------------------+
      | id | select_type | table       | type | possible_keys | key | key_len | ref | rows | Extra          |         | 1 | SIMPLE       | p     | ALL | NULL           | NULL      | NULL    | NULL |    7 |                         |
      +----+--------------+------------+-------+---------------+------+---------+------+------+-------------+         | 1 | SIMPLE       | c     | ref | parent_id      | parent_id | 4       | p.id |    2 | Using where; Not exists |
      | 1 | PRIMARY       | p          | range | val           | val | 12       | NULL |    1 | Using where |         +----+-------------+-------+------+---------------+-----------+---------+------+------+-------------------------+
      | 2 | UNION         | p          | ALL   | PRIMARY       | NULL | NULL    | NULL |    8 | Using where |
      | NULL | UNION RESULT | <union1,2> | ALL   | NULL        | NULL | NULL    | NULL | NULL |             |         mysql> EXPLAIN SELECT p.* FROM parent p WHERE NOT EXISTS (SELECT parent_id FROM child c WHERE c.parent_id = p.id);
      +----+--------------+------------+-------+---------------+------+---------+------+------+-------------+         +----+--------------------+-------+------+---------------+-----------+---------+------+------+-------------+
                                                                                                                      | id | select_type        | table | type | possible_keys | key       | key_len | ref | rows | Extra        |
                                                                                                                      +----+--------------------+-------+------+---------------+-----------+---------+------+------+-------------+
                                                                                                                      | 1 | PRIMARY             | p     | ALL | NULL           | NULL      | NULL    | NULL |    7 | Using where |
                                                                                                                      | 2 | DEPENDENT SUBQUERY | c      | ref | parent_id      | parent_id | 4       | p.id |    2 | Using index |
                                                                          1,2 represents id                           +----+--------------------+-------+------+---------------+-----------+---------+------+------+-------------+




                                                                                                                                                                                       Which query is best?


               EffectiveMySQL.com - Performance, Scalability & Business Continuity                                                     EffectiveMySQL.com - Performance, Scalability & Business Continuity
Extra                   Extra - Using temporary

Most Common                                                                         Internal Table (MEMORY based)
 Using where                                                                        Can have multiple per query
 Using temporary                                                                    Save to disk impact
 Using filesort                                           id: 1
                                               select_type: SIMPLE
                                                                                        TEXT/BLOB
                                                      table: inventory
 Using index           **GOOD**                        type: ALL
                                             possible_keys: NULL
                                                                                        Size > min(max_heap_table_size,                          tmp_table_size)
                                                        key: NULL
 Using join buffer                                 key_len: NULL           http://forge.mysql.com/wiki/Overview_of_query_execution_and_use_of_temp_tables
                                                        ref: NULL
                                                       rows: 787338
                                                      Extra: Using where
  EffectiveMySQL.com - Performance, Scalability & Business Continuity                 EffectiveMySQL.com - Performance, Scalability & Business Continuity




Extra - Using filesort                                                         Extra - Using filesort
                                                                                                                                                     Example
                                                                                EXPLAIN
 ORDER BY                                                                       SELECT
                                                                                FROM
                                                                                           i.invoice_date, i.customer_id, i.invoice_total, c.company, c.state
                                                                                           invoice i INNER JOIN customer c USING (customer_id)
                                                                                WHERE      i.customer_id = 42
                                                                                ORDER BY   i.invoice_date;
   Can be CPU intensive                                                         +-------+-------+---------------+-------------+---------+-------+------+----------------+
                                                                                | table | type | possible_keys | key          | key_len | ref   | rows | Extra          |
                                                                                +-------+-------+---------------+-------------+---------+-------+------+----------------+
                                                                                | c     | const | PRIMARY       | PRIMARY     | 4       | const |    1 | Using filesort |
                                                                                | i     | ref   | customer_id   | customer_id | 4       | const |    5 | Using where    |
                                                                                +-------+-------+---------------+-------------+---------+-------+------+----------------+



 Is order via DB necessary?                                                     CREATE TABLE invoice(
                                                                                  invoice_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
                                                                                  invoice_date DATE NOT NULL,

 Can you leverage an index?
                                                                                  customer_id INT UNSIGNED NOT NULL,
                                                                                  invoice_total DECIMAL(10,2) NOT NULL,
                                                                                  PRIMARY KEY(invoice_id),
                                                                                  KEY (customer_id)
                                                                                ) ENGINE=InnoDB;




  EffectiveMySQL.com - Performance, Scalability & Business Continuity                 EffectiveMySQL.com - Performance, Scalability & Business Continuity
Extra - Using filesort                                                                                                   Extra - Using index
                                                                                 Example
            EXPLAIN
            SELECT
            FROM
                       i.invoice_date, i.customer_id, i.invoice_total, c.company, c.state
                       invoice i INNER JOIN customer c USING (customer_id)                                               Does not mean using index
            WHERE      i.customer_id = 42
            ORDER BY   i.invoice_date;
            +-------+-------+---------------+-------------+---------+-------+------+----------------+
            | table | type | possible_keys | key          | key_len | ref   | rows | Extra          |
                                                                                                                         Means using ONLY THE INDEX
            +-------+-------+---------------+-------------+---------+-------+------+----------------+
            | c     | const | PRIMARY       | PRIMARY     | 4       | const |    1 | Using filesort |
            | i     | ref   | customer_id   | customer_id | 4       | const |    5 | Using where    |
            +-------+-------+---------------+-------------+---------+-------+------+----------------+


            CREATE TABLE invoice(
              invoice_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
                                                                                                                         Additional Presentation
              invoice_date DATE NOT NULL,

                                                                                                                               Improving Performance with
              customer_id INT UNSIGNED NOT NULL,
              invoice_total DECIMAL(10,2) NOT NULL,
              PRIMARY KEY(invoice_id),
              KEY (customer_id, invoice_date)
            ) ENGINE=InnoDB;                                                                                                         Better Indexes
                                                           Modify index, remove per query sorting



                  EffectiveMySQL.com - Performance, Scalability & Business Continuity                                       EffectiveMySQL.com - Performance, Scalability & Business Continuity




                        Extra - Using index                                                             Extra - Using join buffer
                                                                                 Example                                                                                                                       Example
         Executed 25-30 thousand (30,000) queries per second
+----+---------+----+--------+------+------+-------------+
                                    +----+---------+----+--------+------+--------------------------+    | id | select_type | table | type   | possible_keys          | key     | key_len | ref             | rows | Extra
                                                                                                        +----+-------------+-------+--------+------------------------+---------+---------+-----------------+------+-------------------------------------------
| id | select_t| tab| key    | key_ || id | select_t| tab| |
                                        rows | Extra        key  | key_ | Extra                    |    | 1 | SIMPLE       | fs    | ref    | PRIMARY,sts            | sts     | 768     | const           |   21 | Using where; Using temporary; Using filesor
+----+---------+----+--------+------+------+-------------+
                                    +----+---------+----+--------+------+--------------------------+    | 1 | SIMPLE       | fsu   | ref    | PRIMARY,d,sid          | sid     | 4       | fs.sid          |   21 | Using where
                                                                                                        | 1 | SIMPLE       | fes   | ref    | sasi,eid,sid,sues,suid | sues    | 8       | fsi.suid,fs.sid |   26 |
| 1 | PRIMARY | c | statu | 1       || 1 74 PRIMARY |where |
                                          | | Using c | adver | 4       | Using where              |    | 1 | SIMPLE       | ma    | ALL    | masi                   | NULL    | NULL    | NULL            | 200 | Using where; Using join buffer
| 1 | PRIMARY | e | PRIMA | 4       || 1 |1 PRIMARY |where |
                                              | Using e | statu | 1     | Using where; Using index |    | 1 | SIMPLE       | fas   | eq_ref | PRIMARY                | PRIMARY | 4       | ma.faid         |    1 | Using where; Using index
                                                                                                        | 1 | SIMPLE       | las   | eq_ref | PRIMARY,asai           | PRIMARY | 4       | ma.laid         |    1 |
| 1 | PRIMARY | g | campa | 4       || 1 |1 PRIMARY |where |
                                              | Using g | campa | 4     | Using where              |    | 1 | SIMPLE       | la    | eq_ref | PRIMARY                | PRIMARY | 4       | las.aid         |    1 |
| 10 | DEPENDEN| crb| id_ca | 4     || 10253 DEPENDEN|where |
                                          | | Using crb| id_ca | 66     | Using where              |    | 1 | SIMPLE       | fp    | eq_ref | PRIMARY                | PRIMARY | 4       | ses.eid         |    1 |
| 9 | DEPENDEN| csb| pub_s | 98 || 9 |1 DEPENDEN|where |
                                              | Using csb| pub_s | 98   | Using where              |
| 8 | DEPENDEN| arb| id_ad | 4      || 8901 DEPENDEN|where |
                                          | | Using arb| id_ad | 26     | Using where              |
| 7 | DEPENDEN| asb| pub_s | 34 || 7 |1 DEPENDEN|where |
                                              | Using asb| id_ad | 40   | Using where; Using index |
| 6 | DEPENDEN| pm | id_adr | 4     || 6 42 DEPENDEN|where |
                                          | | Using pm | id_adr | 12    | Using index              |
| 5 | DEPENDEN| tgv| searc | 4      || 5 |2 DEPENDEN|where |
                                              | Using tgv| searc | 10   | Using where; Using index |
| 4 | DEPENDEN| st | id_sc | 4      || 4 |7 DEPENDEN|where |
                                              | Using st | id_sc | 4    | Using where; Using index |
                                                                                                                                                   No index can be satisfied for join condition.
| 4 | DEPENDEN| t | PRIMA | 4       || 4 |1 DEPENDEN|where |
                                              | Using t | PRIMA | 4     | Using where              |                                                           i.e. full table scan
| 3 | DEPENDEN| k2 | keywo | 302 || 3 |4 DEPENDEN|where |
                                              | Using k2 | keywo | 302 | Using where; Using index |
| 3 | DEPENDEN| gk2| PRIMA | 100 || 3 |1 DEPENDEN|where |
                                              | Using gk2| PRIMA | 100 | Using where               |
| 2 | DEPENDEN| k1 | keywo | 302 || 2 |3 DEPENDEN|where |
                                              | Using k1 | keywo | 302 | Using where; Using index |
| 2 | DEPENDEN| gk1| PRIMA | 100 || 2 |1 DEPENDEN|where |
                                              | Using gk1| PRIMA | 100 | Using where               |
+----+---------+----+--------+------+------+-------------+
                                    +----+---------+----+--------+------+--------------------------+

                        Before new indexes                                After new indexes
                        executed in 175ms                                 executed in 5ms



                  EffectiveMySQL.com - Performance, Scalability & Business Continuity                                       EffectiveMySQL.com - Performance, Scalability & Business Continuity
Extra cont.                                                                                              Extra
                                                                                                                                      Example
                                                                        mysql> EXPLAIN SELECT COUNT(*) FROM (SELECT id FROM users WHERE first = 'west') c
Less common                                                             *************************** 1. row ***************************
                                                                                   id: 1
                                                                          select_type: PRIMARY
                                                                                table: NULL
 Impossible WHERE ...                                                            type: NULL
                                                                        possible_keys: NULL
                                                                                  key: NULL
                                                                              key_len: NULL
 Distinct                                                                         ref: NULL
                                                                                 rows: NULL
                                                                                Extra: Select tables optimized away
                                                                        *************************** 2. row ***************************
 Not exists                                                                        id: 2
                                                                          select_type: DERIVED
                                                                                table: users

 Select tables optimized away
                                                                                 type: ref
                                                                        possible_keys: first
                                                                                  key: first
                                                                              key_len: 22
                                                                                  ref:
                                                                                 rows: 1




  EffectiveMySQL.com - Performance, Scalability & Business Continuity         EffectiveMySQL.com - Performance, Scalability & Business Continuity




                                Extra cont.                                                                              SYNTAX

Merge Indexes                                                               EXPLAIN SELECT ...
 Using sort_union(...)                                                          EXPLAIN PARTITIONS SELECT ...

 Using union(...)                                                               EXPLAIN EXTENDED SELECT ...

 Using intersect(...)




  EffectiveMySQL.com - Performance, Scalability & Business Continuity         EffectiveMySQL.com - Performance, Scalability & Business Continuity
EXPLAIN PARTITIONS                                                           EXPLAIN EXTENDED
                                                        Example                                                                                        Example
    mysql> EXPLAIN PARTITIONS SELECT * from audit_log WHERE yr in                mysql> EXPLAIN EXTENDED select t1.name from test1 t1 INNER JOIN test2 t2 USING(uid)G
                                                                             *************************** 1. row ***************************
    (2011,2012)G                                                                       id: 1
 *************************** 1. row ***************************                select_type: SIMPLE
            id: 1                                                                    table: t1
                                                                                      type: ALL
   select_type: SIMPLE                                                       possible_keys: NULL
         table: audit_log                                                              key: NULL
    partitions: p2,p3                                                              key_len: NULL
                                                                                       ref: NULL
          type: ALL                                                                   rows: 1
 possible_keys: NULL                                                              filtered: 100.00
                                                                                     Extra:
           key: NULL                                                         *************************** 2. row ***************************
       key_len: NULL                                                                    id: 1
           ref: NULL                                                           select_type: SIMPLE
                                                                                     table: t2
          rows: 2                                                                     type: eq_ref
         Extra: Using where                                                  possible_keys: PRIMARY
                                                                                       key: PRIMARY
                                                                                   key_len: 98
                                                                                       ref: func
                                                                                      rows: 1
                                                                                  filtered: 100.00
                                                                                     Extra: Using where; Using index
                                                                             2 rows in set, 1 warning (0.00 sec)




       EffectiveMySQL.com - Performance, Scalability & Business Continuity           EffectiveMySQL.com - Performance, Scalability & Business Continuity




 EXPLAIN EXTENDED                                                                                             INDEX HINTS
                                                        Example
 mysql> EXPLAIN EXTENDED select t1.name from test1 t1 INNER JOIN
 test2 t2 USING(uid)G                                                            USE INDEX                                                   Can specify multiple indexes
 mysql> SHOW WARNINGSG
 *************************** 1. row ***************************                   IGNORE INDEX
   Level: Note
    Code: 1003
 Message: select `book`.`t1`.`name` AS `name` from `book`.`test1`
 `t1` join `book`.`test2` `t2` where (convert(`book`.`t1`.`uid` using
                                                                                  FORCE INDEX
 utf8) = `book`.`t2`.`uid`)




                                                                                  FOR
                                                                                      JOIN | ORDER BY | GROUP BY

       EffectiveMySQL.com - Performance, Scalability & Business Continuity           EffectiveMySQL.com - Performance, Scalability & Business Continuity
SELECT HINTS                                                       SELECT HINTS

    STRAIGHT_JOIN                                                           SQL_CACHE
        Defines order of tables in QEP                                       SQL_NO_CACHE
                                                                            SQL_CALC_FOUND_ROWS
                                                                            SQL_BIG_RESULT
                                                                            SQL_SMALL_RESULT
                                                                            SQL_BUFFER_RESULT
                                                                            HIGH_PRIORITY
      EffectiveMySQL.com - Performance, Scalability & Business Continuity   EffectiveMySQL.com - Performance, Scalability & Business Continuity




SQL_CALC_FOUND_ROWS
                                                               Example
SELECT id,username FROM users WHERE last LIKE 'w%' LIMIT 10;
..
+--------+----------+
10 rows in set (0.00 sec)


SELECT SQL_CALC_FOUND_ROWS id,username FROM users



                                                                                    Conclusion
WHERE last LIKE 'w%' LIMIT 10;
SELECT FOUND_ROWS();
...
10 rows in set (8.81 sec)

mysql> SELECT FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
|         1671 |
+--------------+
1 row in set (0.02 sec)




      EffectiveMySQL.com - Performance, Scalability & Business Continuity   EffectiveMySQL.com - Performance, Scalability & Business Continuity
CONCLUsiON

Essential tool for SQL analysis
Not the only information you need
Other Related Presentations
 Understanding MySQL Indexes
 Improving Performance with Better
 Indexes
                     Slides at http://j.mp/EM-Explain
                                                                                      Ronald Bradford
 EffectiveMySQL.com - Performance, Scalability & Business Continuity            http://effectiveMySQL.com
                                                                       EffectiveMySQL.com - Performance, Scalability & Business Continuity

Contenu connexe

Tendances

MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorialGiuseppe Maxia
 
Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with ExplainMYXPLAIN
 
0888 learning-mysql
0888 learning-mysql0888 learning-mysql
0888 learning-mysqlsabir18
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101Sveta Smirnova
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningMYXPLAIN
 
Introduction into MySQL Query Tuning for Dev[Op]s
Introduction into MySQL Query Tuning for Dev[Op]sIntroduction into MySQL Query Tuning for Dev[Op]s
Introduction into MySQL Query Tuning for Dev[Op]sSveta Smirnova
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionSveta Smirnova
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query TuningAlexander Rubin
 
Introduction to MySQL Query Tuning for Dev[Op]s
Introduction to MySQL Query Tuning for Dev[Op]sIntroduction to MySQL Query Tuning for Dev[Op]s
Introduction to MySQL Query Tuning for Dev[Op]sSveta Smirnova
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, ReallyMYXPLAIN
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index CookbookMYXPLAIN
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQLEvan Weaver
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersJonathan Levin
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZENorvald Ryeng
 
Preparse Query Rewrite Plugins
Preparse Query Rewrite PluginsPreparse Query Rewrite Plugins
Preparse Query Rewrite PluginsSveta Smirnova
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query TuningSveta Smirnova
 

Tendances (20)

MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
 
Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with Explain
 
0888 learning-mysql
0888 learning-mysql0888 learning-mysql
0888 learning-mysql
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
Explain
ExplainExplain
Explain
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema Tuning
 
Introduction into MySQL Query Tuning for Dev[Op]s
Introduction into MySQL Query Tuning for Dev[Op]sIntroduction into MySQL Query Tuning for Dev[Op]s
Introduction into MySQL Query Tuning for Dev[Op]s
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in action
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
Introduction to MySQL Query Tuning for Dev[Op]s
Introduction to MySQL Query Tuning for Dev[Op]sIntroduction to MySQL Query Tuning for Dev[Op]s
Introduction to MySQL Query Tuning for Dev[Op]s
 
Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, Really
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index Cookbook
 
Sql query patterns, optimized
Sql query patterns, optimizedSql query patterns, optimized
Sql query patterns, optimized
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZE
 
Load Data Fast!
Load Data Fast!Load Data Fast!
Load Data Fast!
 
Preparse Query Rewrite Plugins
Preparse Query Rewrite PluginsPreparse Query Rewrite Plugins
Preparse Query Rewrite Plugins
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
 

En vedette

Inno db internals innodb file formats and source code structure
Inno db internals innodb file formats and source code structureInno db internals innodb file formats and source code structure
Inno db internals innodb file formats and source code structurezhaolinjnu
 
MySQL 5.7: Focus on InnoDB
MySQL 5.7: Focus on InnoDBMySQL 5.7: Focus on InnoDB
MySQL 5.7: Focus on InnoDBMario Beck
 
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!Boris Hristov
 
Mvcc Unmasked (Bruce Momjian)
Mvcc Unmasked (Bruce Momjian)Mvcc Unmasked (Bruce Momjian)
Mvcc Unmasked (Bruce Momjian)Ontico
 
The Power of MySQL Explain
The Power of MySQL ExplainThe Power of MySQL Explain
The Power of MySQL ExplainMYXPLAIN
 
Como migrar una base de datos de mysql a power designer
Como migrar una base de datos de mysql a power designerComo migrar una base de datos de mysql a power designer
Como migrar una base de datos de mysql a power designerAlex Bernal
 
Mv unmasked.w.code.march.2013
Mv unmasked.w.code.march.2013Mv unmasked.w.code.march.2013
Mv unmasked.w.code.march.2013EDB
 
A brief introduction to PostgreSQL
A brief introduction to PostgreSQLA brief introduction to PostgreSQL
A brief introduction to PostgreSQLVu Hung Nguyen
 
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Postgres MVCC - A Developer Centric View of Multi Version Concurrency ControlPostgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Postgres MVCC - A Developer Centric View of Multi Version Concurrency ControlReactive.IO
 
InnoDB Internal
InnoDB InternalInnoDB Internal
InnoDB Internalmysqlops
 
MySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. RyengMySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. Ryeng郁萍 王
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL QueriesAchievers Tech
 
SQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they workSQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they workMarkus Winand
 
Best practices for MySQL High Availability
Best practices for MySQL High AvailabilityBest practices for MySQL High Availability
Best practices for MySQL High AvailabilityColin Charles
 
MySQL High Availability Solutions - Feb 2015 webinar
MySQL High Availability Solutions - Feb 2015 webinarMySQL High Availability Solutions - Feb 2015 webinar
MySQL High Availability Solutions - Feb 2015 webinarAndrew Morgan
 
NLP meetup 2016.10.05 - Szekeres Péter: Neticle
NLP meetup 2016.10.05 - Szekeres Péter: NeticleNLP meetup 2016.10.05 - Szekeres Péter: Neticle
NLP meetup 2016.10.05 - Szekeres Péter: NeticleZoltan Varju
 
Универсальный энергосберегающий режущий аппарат
Универсальный энергосберегающий режущий аппаратУниверсальный энергосберегающий режущий аппарат
Универсальный энергосберегающий режущий аппаратkulibin
 
Communitymanager
CommunitymanagerCommunitymanager
CommunitymanagerMizarvega
 

En vedette (20)

Inno db internals innodb file formats and source code structure
Inno db internals innodb file formats and source code structureInno db internals innodb file formats and source code structure
Inno db internals innodb file formats and source code structure
 
MySQL 5.7: Focus on InnoDB
MySQL 5.7: Focus on InnoDBMySQL 5.7: Focus on InnoDB
MySQL 5.7: Focus on InnoDB
 
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!
 
Mvcc Unmasked (Bruce Momjian)
Mvcc Unmasked (Bruce Momjian)Mvcc Unmasked (Bruce Momjian)
Mvcc Unmasked (Bruce Momjian)
 
The Power of MySQL Explain
The Power of MySQL ExplainThe Power of MySQL Explain
The Power of MySQL Explain
 
Mysql For Developers
Mysql For DevelopersMysql For Developers
Mysql For Developers
 
Como migrar una base de datos de mysql a power designer
Como migrar una base de datos de mysql a power designerComo migrar una base de datos de mysql a power designer
Como migrar una base de datos de mysql a power designer
 
Mv unmasked.w.code.march.2013
Mv unmasked.w.code.march.2013Mv unmasked.w.code.march.2013
Mv unmasked.w.code.march.2013
 
A brief introduction to PostgreSQL
A brief introduction to PostgreSQLA brief introduction to PostgreSQL
A brief introduction to PostgreSQL
 
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Postgres MVCC - A Developer Centric View of Multi Version Concurrency ControlPostgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
 
InnoDB Internal
InnoDB InternalInnoDB Internal
InnoDB Internal
 
MySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. RyengMySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. Ryeng
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
SQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they workSQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they work
 
Best practices for MySQL High Availability
Best practices for MySQL High AvailabilityBest practices for MySQL High Availability
Best practices for MySQL High Availability
 
MySQL High Availability Solutions - Feb 2015 webinar
MySQL High Availability Solutions - Feb 2015 webinarMySQL High Availability Solutions - Feb 2015 webinar
MySQL High Availability Solutions - Feb 2015 webinar
 
NLP meetup 2016.10.05 - Szekeres Péter: Neticle
NLP meetup 2016.10.05 - Szekeres Péter: NeticleNLP meetup 2016.10.05 - Szekeres Péter: Neticle
NLP meetup 2016.10.05 - Szekeres Péter: Neticle
 
Универсальный энергосберегающий режущий аппарат
Универсальный энергосберегающий режущий аппаратУниверсальный энергосберегающий режущий аппарат
Универсальный энергосберегающий режущий аппарат
 
Communitymanager
CommunitymanagerCommunitymanager
Communitymanager
 
Wykładzina vol. 14 Teatr Narodowy Opera Narodowa
Wykładzina vol. 14 Teatr Narodowy Opera NarodowaWykładzina vol. 14 Teatr Narodowy Opera Narodowa
Wykładzina vol. 14 Teatr Narodowy Opera Narodowa
 

Similaire à Explaining the MySQL Explain

Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statasLouis liu
 
Goldilocks and the Three MySQL Queries
Goldilocks and the Three MySQL QueriesGoldilocks and the Three MySQL Queries
Goldilocks and the Three MySQL QueriesDave Stokes
 
Performance tuning
Performance tuningPerformance tuning
Performance tuningami111
 
Improving Performance with Better Indexes
Improving Performance with Better IndexesImproving Performance with Better Indexes
Improving Performance with Better IndexesMYXPLAIN
 
Fix My Functions: TSQL User Defined Functions in SQL Server
Fix My Functions: TSQL User Defined Functions in SQL ServerFix My Functions: TSQL User Defined Functions in SQL Server
Fix My Functions: TSQL User Defined Functions in SQL ServerKendra Little
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsmaxpane
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Alex Zaballa
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsAlex Zaballa
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsAlex Zaballa
 
MySQL Best Practices - OTN LAD Tour
MySQL Best Practices - OTN LAD TourMySQL Best Practices - OTN LAD Tour
MySQL Best Practices - OTN LAD TourRonald Bradford
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ICarlos Oliveira
 
Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)Antony T Curtis
 
10x improvement-mysql-100419105218-phpapp02
10x improvement-mysql-100419105218-phpapp0210x improvement-mysql-100419105218-phpapp02
10x improvement-mysql-100419105218-phpapp02promethius
 
10x Performance Improvements
10x Performance Improvements10x Performance Improvements
10x Performance ImprovementsRonald Bradford
 
Mysql Datadictionary
Mysql DatadictionaryMysql Datadictionary
Mysql Datadictionaryrootuser
 
Copy Of Mysql Datadictionary
Copy Of Mysql DatadictionaryCopy Of Mysql Datadictionary
Copy Of Mysql DatadictionaryGolak Sarangi
 

Similaire à Explaining the MySQL Explain (20)

Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statas
 
Goldilocks and the Three MySQL Queries
Goldilocks and the Three MySQL QueriesGoldilocks and the Three MySQL Queries
Goldilocks and the Three MySQL Queries
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
Improving Performance with Better Indexes
Improving Performance with Better IndexesImproving Performance with Better Indexes
Improving Performance with Better Indexes
 
MariaDB workshop
MariaDB workshopMariaDB workshop
MariaDB workshop
 
Fix My Functions: TSQL User Defined Functions in SQL Server
Fix My Functions: TSQL User Defined Functions in SQL ServerFix My Functions: TSQL User Defined Functions in SQL Server
Fix My Functions: TSQL User Defined Functions in SQL Server
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
Modern sql
Modern sqlModern sql
Modern sql
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
10 sql tips
10 sql tips10 sql tips
10 sql tips
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
MySQL Best Practices - OTN LAD Tour
MySQL Best Practices - OTN LAD TourMySQL Best Practices - OTN LAD Tour
MySQL Best Practices - OTN LAD Tour
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)
 
10x improvement-mysql-100419105218-phpapp02
10x improvement-mysql-100419105218-phpapp0210x improvement-mysql-100419105218-phpapp02
10x improvement-mysql-100419105218-phpapp02
 
10x Performance Improvements
10x Performance Improvements10x Performance Improvements
10x Performance Improvements
 
Mysql Datadictionary
Mysql DatadictionaryMysql Datadictionary
Mysql Datadictionary
 
Copy Of Mysql Datadictionary
Copy Of Mysql DatadictionaryCopy Of Mysql Datadictionary
Copy Of Mysql Datadictionary
 

Plus de MYXPLAIN

Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksMYXPLAIN
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL IndexingMYXPLAIN
 
Advanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisMYXPLAIN
 
Are You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL IndexesAre You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL IndexesMYXPLAIN
 
MySQL 5.6 Performance
MySQL 5.6 PerformanceMySQL 5.6 Performance
MySQL 5.6 PerformanceMYXPLAIN
 
MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MYXPLAIN
 
Tools and Techniques for Index Design
Tools and Techniques for Index DesignTools and Techniques for Index Design
Tools and Techniques for Index DesignMYXPLAIN
 
Covering indexes
Covering indexesCovering indexes
Covering indexesMYXPLAIN
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewMYXPLAIN
 
Advanced query optimization
Advanced query optimizationAdvanced query optimization
Advanced query optimizationMYXPLAIN
 

Plus de MYXPLAIN (10)

Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL Indexing
 
Advanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and Analysis
 
Are You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL IndexesAre You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL Indexes
 
MySQL 5.6 Performance
MySQL 5.6 PerformanceMySQL 5.6 Performance
MySQL 5.6 Performance
 
MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6
 
Tools and Techniques for Index Design
Tools and Techniques for Index DesignTools and Techniques for Index Design
Tools and Techniques for Index Design
 
Covering indexes
Covering indexesCovering indexes
Covering indexes
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
Advanced query optimization
Advanced query optimizationAdvanced query optimization
Advanced query optimization
 

Dernier

Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Dernier (20)

Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

Explaining the MySQL Explain

  • 1. Explaining the MySQL EXPLAIN Ronald Bradford http://ronaldbradford.com RMOUG QEW Denver, Colorado - 2012.05 EffectiveMySQL.com - Performance, Scalability & Business Continuity Agenda PURPOSE EXPLAIN syntax options EXPLAIN is used for How to read QEP Determine Query Execution Plan (QEP) QEP examples Understand MySQL optimizer MySQL optimizer limitations Identify information needed for tuning Not providing tuning recommendations Slides at http://j.mp/EM-Explain EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 2. ABOUT THE AUTHOR Ronald Bradford 2011 - All time top blog contributor to Planet MySQL 2010 - Published Author of Expert PHP & MySQL 2010 - Oracle ACE Director (first in MySQL) 2009 - MySQL community member of the year 22 years of RDBMS experience,12 years with MySQL MySQL Inc (2006-2008) Oracle Corporation (1996-1999) Provide independent consulting - Available NOW EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity MySQL QEP SYNTAX Cost Based Optimizer EXPLAIN SELECT ... No pinning EXPLAIN PARTITIONS SELECT ... Few hints EXPLAIN EXTENDED SELECT ... Calculated every SQL execution Except Query Cache Does not support UPDATE,DELETE Now supported in MySQL 5.6 EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 3. Support Commands EXPLAIN USAGE SHOW CREATE TABLE Does not execute query SHOW INDEXES SHOW TABLE STATUS BUT INFORMATION_SCHEMA SHOW VARIABLES WARNING !!! SHOW STATUS May execute portions Optimizer Trace - Since 5.6.3 e.g. derived tables EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity EXPLAIN Attributes Example mysql> EXPLAIN SELECT * FROM proposal WHERE post_id=16102176; mysql> EXPLAIN SELECT * FROM inventory +----+-------------+----------+------+---------------+------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | id -> WHERE item_id = 16102176G +----+-------------+----------+------+---------------+------+---------+------+--------+-------------+ | 1 | SIMPLE | proposal | ALL | NULL | NULL | NULL | NULL | 787126 | Using where | +----+-------------+----------+------+---------------+------+---------+------+--------+-------------+ select_type ************************** 1. row ******** id: 1 table select_type: SIMPLE table: inventory mysql> EXPLAIN SELECT * FROM inventory -> WHERE item_id = 16102176G type type: ref *************************** 1. row ********************** id: 1 possible_keys possible_keys: item_id select_type: SIMPLE key: item_id table: inventory key key_len: 4 type: ref ref: const possible_keys: item_id key: item_id key_len rows: 1 key_len: 4 ref: const ref Extra: rows: 1 Extra: MySQL Client SQL Statement Terminator rows ; G Extra EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 4. Essential Essential Example Example mysql> EXPLAIN SELECT * FROM inventory mysql> EXPLAIN SELECT * FROM inventory id -> WHERE item_id = 16102176G id -> WHERE item_id = 16102176G select_type select_type ************************* 1. row ********************** ************************* 1. row ********* *************************** 1. row ******* id: 1 id: 1 table select_type: SIMPLE table select_type: SIMPLE table: inventory table: inventory type type: ref type type: ref ALL possible_keys ! possible_keys " possible_keys: item_id possible_keys: item_id NULL key: item_id key: item_id NULL key key_len: 4 key key_len: 4 NULL ref: const ref: const NULL key_len rows: 1 key_len rows: 1 787338 ref Extra: ref Extra: Using where rows rows Extra Extra EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity Essential key Example mysql> EXPLAIN SELECT * FROM inventory id -> WHERE item_id = 16102176G Identify index to be used select_type ************************* 1. row ********************** id: 1 table select_type: SIMPLE Generally only one per table (*) table: inventory type type: ref possible_keys ! possible_keys: item_id key: item_id key key_len: 4 Associated attributes id: 1 ref: const key_len rows: 1 select_type: SIMPLE ref Extra: possible_keys table: inventory type: ALL rows possible_keys: NULL Extra key_len key: NULL key_len: NULL ref: NULL rows: 787338 Extra: Using where EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 5. key key Example MERGE INDEX Example mysql> EXPLAIN SELECT * FROM inventory mysql> EXPLAIN SELECT id FROM users -> WHERE item_id = 16102176G -> WHERE first = 'west' OR last='west'G *************************** 1. row ********************** ************************ 1. row ********************** id: 1 id: 1 CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCRE select_type: SIMPLE select_type: SIMPLE `first` varchar(20) NOT NULL, table: inventory table: users `last` varchar(20) NOT NULL, type: ref type: index_merge `username` varchar(20) NOT NULL, `last_login` timestamp NOT NULL DEFAULT C possible_keys: item_id possible_keys: first,last PRIMARY KEY (`id`), key: item_id key: first,last KEY `first` (`first`), key_len: 4 key_len: 22,22 KEY `last` (`last`), KEY `username` (`username`) ... ref: const ref: NULL rows: 1 rows: 2 Extra: Extra: Using union(first,last); Using where EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity possible_keys possible_keys Example Indexes the optimizer considered | 1 | SIMPLE | 1 | SIMPLE | c | i | const | PRIMARY | ALL | customer_id | PRIMARY | 4 | NULL | NULL | rows | Extra | const | | NULL | " +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------+ 1 | Using filesort | 7 | Using where | Why was no index used? +----+-------------+-------+-------+---------------+---------+---------+-------+------+----------------+ Too many is not good id: 1 | 1 | SIMPLE | i | ref | customer_id | key_len | ref | customer_id | 4 | rows | Extra | const | | const | 1 | ! +----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | +----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+ | 1 | SIMPLE | c | const | PRIMARY | PRIMARY | 4 | 5 | Using where | +----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+ select_type: SIMPLE Associated attributes table: inventory type: ALL possible_keys: NULL key key: NULL key_len: NULL ref: NULL rows: 787338 Extra: Using where EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 6. possible_keys rows Example ************************ 1. row ********************** id: 1 Estimated number of table rows (*) select_type: SIMPLE table: FES type: ref possible_keys: FK_ASI,EmpID1,Idx_SID,SuleUnitsEmpSuled,Idx_SUnitID Associated attributes key: SuleUnitsEmpSuled key_len: 8 ref: FSU.SUnitId,FS.SID rows: 26 Extra: .. key id: 1 select_type: SIMPLE PRIMARY KEY (`ID`), KEY `FK_ASI` (`ASID`), table: inventory KEY `EmpID1` (`EmpID`), type: ALL KEY `Idx_Composite` (`ID`,`EmpID`,`SUnitID`,`SID`,`SourceSID`,`ASID`), KEY `Idx_SID` (`SID`), possible_keys: NULL KEY `SuleUnitsEmpSuled` (`SUnitID`,`SID`), key: NULL KEY `Idx_SUnitID` (`SUnitID`), key_len: NULL ref: NULL rows: 787338 Extra: Using where EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity key_len key_len Calculations Amount of index used (*) TINYINT - 1 byte CHAR(n) - n bytes SMALLINT - 2 bytes VARCHAR(n) - n bytes Multi column efficiency INT - 4 bytes NULL + 1 byte BIGINT - 8 bytes VARCHAR + 2 bytes DATE - 3 bytes Associated attributes id: 1 select_type: SIMPLE TIMESTAMP - 4 bytes Character set x [1-3] bytes table: inventory Extra = Using Index type: ALL possible_keys: inv_id DATETIME - 8 bytes key: inv_id key_len: 4 ref: NULL rows: 1 Extra: Using where EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 7. key_len key_len with varchar Example Example mysql> EXPLAIN SELECT user_id,balance,created mysql> EXPLAIN SELECT * -> FROM accounts -> FROM categories -> WHERE id = 42G -> WHERE name LIKE 'NCAA%'; *************************** 1. row ********************** *************************** 1. row ********************* id: 1 id: 1 select_type: SIMPLE select_type: SIMPLE table: accounts table: categories type: const type: range possible_keys: PRIMARY 8 Bytes possible_keys: name 30 Bytes + 2 Bytes key: PRIMARY CREATE TABLE `accounts` ( key: name CREATE TABLE categories ( key_len: 8 `id` BIGINT NOT NULL AUTO_INCREMENT, key_len: 32 id int NOT NULL AUTO_INCREMENT, ref: const ... ref: NULL name VARCHAR(30) NOT NULL, rows: 1 PRIMARY KEY (id) rows: 6 ... Extra: ... Extra: Using where INDEX (name) ... EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity key_len with utf8 key_len Example Example (1) (3) mysql> EXPLAIN SELECT ID, post_title FROM wp_posts WHERE post_type='post' and post_date > '2010-06-01'; mysql> EXPLAIN SELECT * +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+ -> FROM categories | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+ -> WHERE name LIKE 'NCAA%'; | 1 | SIMPLE | wp_posts | ref | type_status_date | type_status_date | 62 | const | 1132 | Using where | +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+ *************************** 1. row ********************* id: 1 select_type: SIMPLE table: categories type: range (30 * 3) Bytes + 2 Bytes CREATE TABLE `wp_posts` ( possible_keys: name CREATE TABLE `categories` ( ... key: name `id` int(4) NOT NULL AUTO_INCREMENT, `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', key_len: 92 `name` VARCHAR(30) NOT NULL, `post_status` varchar(20) NOT NULL DEFAULT 'publish', ref: NULL ... `post_type` varchar(20) NOT NULL DEFAULT 'post', rows: 6 ... Extra: Using where INDEX (name) ) ... DEFAULT CHARSET=utf8 PRIMARY KEY (`ID`), (1) (2) (3) (4) KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`), ) DEFAULT CHARSET=utf8 62 + 62 + 8 + 8 Bytes EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 8. key_len select_type Example (1) (3) mysql> EXPLAIN SELECT ID, post_title FROM wp_posts WHERE post_type='post' and post_date > '2010-06-01'; +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+ SIMPLE DEPENDENT UNION | 1 | SIMPLE | wp_posts | ref | type_status_date | type_status_date | 62 | const | 1132 | Using where | +----+-------------+----------+------+------------------+------------------+---------+-------+------+-------------+ (1) (2) (3) mysql> EXPLAIN SELECT ID, post_title FROM wp_posts WHERE post_type='post' AND post_status='publish' AND post_date > PRIMARY UNION RESULT '2010-06-01'; | id | select_type | table | type | possible_keys | key 62+62+8 Bytes +----+-------------+----------+-------+------------------+------------------+---------+------+------+-------------+ | key_len | ref | rows | Extra | +----+-------------+----------+-------+------------------+------------------+---------+------+------+-------------+ SUBQUERY UNCACHEABLE QUERY | 1 | SIMPLE | wp_posts | range | type_status_date | type_status_date | 132 | NULL | 1 | Using where | +----+-------------+----------+-------+------------------+------------------+---------+------+------+-------------+ CREATE TABLE `wp_posts` ( ... `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', DERIVED UNCACHEABLE UNION `post_status` varchar(20) NOT NULL DEFAULT 'publish', id: 1 `post_type` varchar(20) NOT NULL DEFAULT 'post', ... UNION select_type: SIMPLE table: inventory PRIMARY KEY (`ID`), (1) (2) (3) (4) type: ALL KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`), ) DEFAULT CHARSET=utf8 possible_keys: NULL key: NULL 62 + 62 + 8 + 8 bytes key_len: NULL ref: NULL rows: 787338 EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity Extra: Using where select_type select_type Example Example 3 ways to get same query results mysql> EXPLAIN SELECT MAX(id) FROM (SELECT id FROM users WHERE first = 'west') c; mysql> EXPLAIN SELECT p.* FROM parent p WHERE p.id NOT IN (SELECT c.parent_id FROM child c); +----+-------------+------------+------+---------------+-------+---------+------+------+-------------+ +----+--------------------+-------+----------------+---------------+-----------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+------+---------------+-------+---------+------+------+-------------+ +----+--------------------+-------+----------------+---------------+-----------+---------+------+------+-------------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2 | | | 1 | PRIMARY | p | ALL | NULL | NULL | NULL | NULL | 7 | Using where | | 2 | DERIVED | users | ref | first | first | 22 | | 1 | Using where | | 2 | DEPENDENT SUBQUERY | c | index_subquery | parent_id | parent_id | 4 | func | 2 | Using index | +----+-------------+------------+------+---------------+-------+---------+------+------+-------------+ +----+--------------------+-------+----------------+---------------+-----------+---------+------+------+-------------+ mysql> EXPLAIN SELECT p.* FROM parent p WHERE p.val LIKE 'a%' mysql> EXPLAIN SELECT p.* FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE c.child_id IS NULL; -> UNION 2 represents id +----+-------------+-------+------+---------------+-----------+---------+------+------+-------------------------+ -> SELECT p.* FROM parent p WHERE p.id > 5; | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------+------------+-------+---------------+------+---------+------+------+-------------+ +----+-------------+-------+------+---------------+-----------+---------+------+------+-------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | | 1 | SIMPLE | p | ALL | NULL | NULL | NULL | NULL | 7 | | +----+--------------+------------+-------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | c | ref | parent_id | parent_id | 4 | p.id | 2 | Using where; Not exists | | 1 | PRIMARY | p | range | val | val | 12 | NULL | 1 | Using where | +----+-------------+-------+------+---------------+-----------+---------+------+------+-------------------------+ | 2 | UNION | p | ALL | PRIMARY | NULL | NULL | NULL | 8 | Using where | | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | | mysql> EXPLAIN SELECT p.* FROM parent p WHERE NOT EXISTS (SELECT parent_id FROM child c WHERE c.parent_id = p.id); +----+--------------+------------+-------+---------------+------+---------+------+------+-------------+ +----+--------------------+-------+------+---------------+-----------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-------+------+---------------+-----------+---------+------+------+-------------+ | 1 | PRIMARY | p | ALL | NULL | NULL | NULL | NULL | 7 | Using where | | 2 | DEPENDENT SUBQUERY | c | ref | parent_id | parent_id | 4 | p.id | 2 | Using index | 1,2 represents id +----+--------------------+-------+------+---------------+-----------+---------+------+------+-------------+ Which query is best? EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 9. Extra Extra - Using temporary Most Common Internal Table (MEMORY based) Using where Can have multiple per query Using temporary Save to disk impact Using filesort id: 1 select_type: SIMPLE TEXT/BLOB table: inventory Using index **GOOD** type: ALL possible_keys: NULL Size > min(max_heap_table_size, tmp_table_size) key: NULL Using join buffer key_len: NULL http://forge.mysql.com/wiki/Overview_of_query_execution_and_use_of_temp_tables ref: NULL rows: 787338 Extra: Using where EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity Extra - Using filesort Extra - Using filesort Example EXPLAIN ORDER BY SELECT FROM i.invoice_date, i.customer_id, i.invoice_total, c.company, c.state invoice i INNER JOIN customer c USING (customer_id) WHERE i.customer_id = 42 ORDER BY i.invoice_date; Can be CPU intensive +-------+-------+---------------+-------------+---------+-------+------+----------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +-------+-------+---------------+-------------+---------+-------+------+----------------+ | c | const | PRIMARY | PRIMARY | 4 | const | 1 | Using filesort | | i | ref | customer_id | customer_id | 4 | const | 5 | Using where | +-------+-------+---------------+-------------+---------+-------+------+----------------+ Is order via DB necessary? CREATE TABLE invoice( invoice_id INT UNSIGNED NOT NULL AUTO_INCREMENT, invoice_date DATE NOT NULL, Can you leverage an index? customer_id INT UNSIGNED NOT NULL, invoice_total DECIMAL(10,2) NOT NULL, PRIMARY KEY(invoice_id), KEY (customer_id) ) ENGINE=InnoDB; EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 10. Extra - Using filesort Extra - Using index Example EXPLAIN SELECT FROM i.invoice_date, i.customer_id, i.invoice_total, c.company, c.state invoice i INNER JOIN customer c USING (customer_id) Does not mean using index WHERE i.customer_id = 42 ORDER BY i.invoice_date; +-------+-------+---------------+-------------+---------+-------+------+----------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | Means using ONLY THE INDEX +-------+-------+---------------+-------------+---------+-------+------+----------------+ | c | const | PRIMARY | PRIMARY | 4 | const | 1 | Using filesort | | i | ref | customer_id | customer_id | 4 | const | 5 | Using where | +-------+-------+---------------+-------------+---------+-------+------+----------------+ CREATE TABLE invoice( invoice_id INT UNSIGNED NOT NULL AUTO_INCREMENT, Additional Presentation invoice_date DATE NOT NULL, Improving Performance with customer_id INT UNSIGNED NOT NULL, invoice_total DECIMAL(10,2) NOT NULL, PRIMARY KEY(invoice_id), KEY (customer_id, invoice_date) ) ENGINE=InnoDB; Better Indexes Modify index, remove per query sorting EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity Extra - Using index Extra - Using join buffer Example Example Executed 25-30 thousand (30,000) queries per second +----+---------+----+--------+------+------+-------------+ +----+---------+----+--------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra +----+-------------+-------+--------+------------------------+---------+---------+-----------------+------+------------------------------------------- | id | select_t| tab| key | key_ || id | select_t| tab| | rows | Extra key | key_ | Extra | | 1 | SIMPLE | fs | ref | PRIMARY,sts | sts | 768 | const | 21 | Using where; Using temporary; Using filesor +----+---------+----+--------+------+------+-------------+ +----+---------+----+--------+------+--------------------------+ | 1 | SIMPLE | fsu | ref | PRIMARY,d,sid | sid | 4 | fs.sid | 21 | Using where | 1 | SIMPLE | fes | ref | sasi,eid,sid,sues,suid | sues | 8 | fsi.suid,fs.sid | 26 | | 1 | PRIMARY | c | statu | 1 || 1 74 PRIMARY |where | | | Using c | adver | 4 | Using where | | 1 | SIMPLE | ma | ALL | masi | NULL | NULL | NULL | 200 | Using where; Using join buffer | 1 | PRIMARY | e | PRIMA | 4 || 1 |1 PRIMARY |where | | Using e | statu | 1 | Using where; Using index | | 1 | SIMPLE | fas | eq_ref | PRIMARY | PRIMARY | 4 | ma.faid | 1 | Using where; Using index | 1 | SIMPLE | las | eq_ref | PRIMARY,asai | PRIMARY | 4 | ma.laid | 1 | | 1 | PRIMARY | g | campa | 4 || 1 |1 PRIMARY |where | | Using g | campa | 4 | Using where | | 1 | SIMPLE | la | eq_ref | PRIMARY | PRIMARY | 4 | las.aid | 1 | | 10 | DEPENDEN| crb| id_ca | 4 || 10253 DEPENDEN|where | | | Using crb| id_ca | 66 | Using where | | 1 | SIMPLE | fp | eq_ref | PRIMARY | PRIMARY | 4 | ses.eid | 1 | | 9 | DEPENDEN| csb| pub_s | 98 || 9 |1 DEPENDEN|where | | Using csb| pub_s | 98 | Using where | | 8 | DEPENDEN| arb| id_ad | 4 || 8901 DEPENDEN|where | | | Using arb| id_ad | 26 | Using where | | 7 | DEPENDEN| asb| pub_s | 34 || 7 |1 DEPENDEN|where | | Using asb| id_ad | 40 | Using where; Using index | | 6 | DEPENDEN| pm | id_adr | 4 || 6 42 DEPENDEN|where | | | Using pm | id_adr | 12 | Using index | | 5 | DEPENDEN| tgv| searc | 4 || 5 |2 DEPENDEN|where | | Using tgv| searc | 10 | Using where; Using index | | 4 | DEPENDEN| st | id_sc | 4 || 4 |7 DEPENDEN|where | | Using st | id_sc | 4 | Using where; Using index | No index can be satisfied for join condition. | 4 | DEPENDEN| t | PRIMA | 4 || 4 |1 DEPENDEN|where | | Using t | PRIMA | 4 | Using where | i.e. full table scan | 3 | DEPENDEN| k2 | keywo | 302 || 3 |4 DEPENDEN|where | | Using k2 | keywo | 302 | Using where; Using index | | 3 | DEPENDEN| gk2| PRIMA | 100 || 3 |1 DEPENDEN|where | | Using gk2| PRIMA | 100 | Using where | | 2 | DEPENDEN| k1 | keywo | 302 || 2 |3 DEPENDEN|where | | Using k1 | keywo | 302 | Using where; Using index | | 2 | DEPENDEN| gk1| PRIMA | 100 || 2 |1 DEPENDEN|where | | Using gk1| PRIMA | 100 | Using where | +----+---------+----+--------+------+------+-------------+ +----+---------+----+--------+------+--------------------------+ Before new indexes After new indexes executed in 175ms executed in 5ms EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 11. Extra cont. Extra Example mysql> EXPLAIN SELECT COUNT(*) FROM (SELECT id FROM users WHERE first = 'west') c Less common *************************** 1. row *************************** id: 1 select_type: PRIMARY table: NULL Impossible WHERE ... type: NULL possible_keys: NULL key: NULL key_len: NULL Distinct ref: NULL rows: NULL Extra: Select tables optimized away *************************** 2. row *************************** Not exists id: 2 select_type: DERIVED table: users Select tables optimized away type: ref possible_keys: first key: first key_len: 22 ref: rows: 1 EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity Extra cont. SYNTAX Merge Indexes EXPLAIN SELECT ... Using sort_union(...) EXPLAIN PARTITIONS SELECT ... Using union(...) EXPLAIN EXTENDED SELECT ... Using intersect(...) EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 12. EXPLAIN PARTITIONS EXPLAIN EXTENDED Example Example mysql> EXPLAIN PARTITIONS SELECT * from audit_log WHERE yr in mysql> EXPLAIN EXTENDED select t1.name from test1 t1 INNER JOIN test2 t2 USING(uid)G *************************** 1. row *************************** (2011,2012)G id: 1 *************************** 1. row *************************** select_type: SIMPLE id: 1 table: t1 type: ALL select_type: SIMPLE possible_keys: NULL table: audit_log key: NULL partitions: p2,p3 key_len: NULL ref: NULL type: ALL rows: 1 possible_keys: NULL filtered: 100.00 Extra: key: NULL *************************** 2. row *************************** key_len: NULL id: 1 ref: NULL select_type: SIMPLE table: t2 rows: 2 type: eq_ref Extra: Using where possible_keys: PRIMARY key: PRIMARY key_len: 98 ref: func rows: 1 filtered: 100.00 Extra: Using where; Using index 2 rows in set, 1 warning (0.00 sec) EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity EXPLAIN EXTENDED INDEX HINTS Example mysql> EXPLAIN EXTENDED select t1.name from test1 t1 INNER JOIN test2 t2 USING(uid)G USE INDEX Can specify multiple indexes mysql> SHOW WARNINGSG *************************** 1. row *************************** IGNORE INDEX Level: Note Code: 1003 Message: select `book`.`t1`.`name` AS `name` from `book`.`test1` `t1` join `book`.`test2` `t2` where (convert(`book`.`t1`.`uid` using FORCE INDEX utf8) = `book`.`t2`.`uid`) FOR JOIN | ORDER BY | GROUP BY EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 13. SELECT HINTS SELECT HINTS STRAIGHT_JOIN SQL_CACHE Defines order of tables in QEP SQL_NO_CACHE SQL_CALC_FOUND_ROWS SQL_BIG_RESULT SQL_SMALL_RESULT SQL_BUFFER_RESULT HIGH_PRIORITY EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity SQL_CALC_FOUND_ROWS Example SELECT id,username FROM users WHERE last LIKE 'w%' LIMIT 10; .. +--------+----------+ 10 rows in set (0.00 sec) SELECT SQL_CALC_FOUND_ROWS id,username FROM users Conclusion WHERE last LIKE 'w%' LIMIT 10; SELECT FOUND_ROWS(); ... 10 rows in set (8.81 sec) mysql> SELECT FOUND_ROWS(); +--------------+ | FOUND_ROWS() | +--------------+ | 1671 | +--------------+ 1 row in set (0.02 sec) EffectiveMySQL.com - Performance, Scalability & Business Continuity EffectiveMySQL.com - Performance, Scalability & Business Continuity
  • 14. CONCLUsiON Essential tool for SQL analysis Not the only information you need Other Related Presentations Understanding MySQL Indexes Improving Performance with Better Indexes Slides at http://j.mp/EM-Explain Ronald Bradford EffectiveMySQL.com - Performance, Scalability & Business Continuity http://effectiveMySQL.com EffectiveMySQL.com - Performance, Scalability & Business Continuity