zoukankan      html  css  js  c++  java
  • explain mysql

    Explain的type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是:
    system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

    用explain解释你的select语句,完善语句。

    mysql> explain select artist_id ,type,founded from artist where name='zj'\G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: artist
             type: ALL         //没有走任何索引,
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL  //索引的列没有被引用

             rows: 4            //全部记录数
            Extra: Using where
    1 row in set (0.00 sec)

    mysql> explain select name from artist\G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: artist
             type: index   //查询走了索引
    possible_keys: NULL
              key: name
          key_len: 257
              ref: NULL   //索引的列没有被引用
             rows: 4
            Extra: Using index
    1 row in set (0.00 sec)

    mysql> explain select artist_id ,type,founded from artist where name='zj'\G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: artist
             type: ref              //name字段上创建索引,走了索引
    possible_keys: name
              key: name
          key_len: 257
              ref: const  //索引列固定的值引用
             rows: 2   //走的行数
            Extra: Using where
    1 row in set (0.00 sec)


    mysql>  explain select artist_id ,type,founded from artist where artist_id>3 and artist_id<5\G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: artist
             type: range  //在创建的索引的字段上给出范围查找
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: NULL  //没有索引列被引用
             rows: 1 //检查数据走的行数
            Extra: Using where
    1 row in set (0.00 sec)

    当数据量非常大的时候,可以看出更有效的区别

    mysql> desc stdV;
    +---------+-----------------------+------+-----+---------+----------------+
    | Field   | Type                  | Null | Key | Default | Extra          |
    +---------+-----------------------+------+-----+---------+----------------+
    | id      | int(10) unsigned      | NO   | PRI | NULL    | auto_increment |
    | name    | varchar(32)           | NO   |     |         |                |
    | sex     | enum('male','female') | NO   |     | male    |                |
    | classId | tinyint(3) unsigned   | NO   |     | 0       |                |
    +---------+-----------------------+------+-----+---------+----------------+
    4 rows in set (0.00 sec)

    mysql> desc exam;
    +----------+------------------+------+-----+-------------------+-----------------------------+
    | Field    | Type             | Null | Key | Default           | Extra                       |
    +----------+------------------+------+-----+-------------------+-----------------------------+
    | id       | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
    | name     | char(32)         | NO   |     |                   |                             |
    | category | enum('Q','T')    | NO   |     | NULL              |                             |
    | examDate | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
    +----------+------------------+------+-----+-------------------+-----------------------------+
    4 rows in set (0.00 sec)

    mysql> desc score;
    +--------+------------------+------+-----+---------+-------+
    | Field  | Type             | Null | Key | Default | Extra |
    +--------+------------------+------+-----+---------+-------+
    | id     | int(10) unsigned | NO   | PRI | 0       |       |
    | examId | int(10) unsigned | NO   | PRI | 0       |       |
    | score  | tinyint(4)       | NO   |     | NULL    |       |
    +--------+------------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)

    mysql> explain select stdV.name,score.score,exam.name from score inner join stdV on stdV.id=score.id inner join exam on exam.id=score.examID\G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: score
             type: ALL
    possible_keys: PRIMARY
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 103449
            Extra:
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: exam
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: mysqlOpt.score.examId
             rows: 1
            Extra:
    *************************** 3. row ***************************
               id: 1
      select_type: SIMPLE
            table: stdV
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: mysqlOpt.score.id
             rows: 1
            Extra:
    3 rows in set (0.00 sec)

    mysql> explain select stdV.name,score.score,exam.name from score inner join stdV on stdV.id=score.id inner join exam on exam.id=score.examId where score.id=1000\G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: stdV
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: score
             type: ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    *************************** 3. row ***************************
               id: 1
      select_type: SIMPLE
            table: exam
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: mysqlOpt.score.examId
             rows: 1
            Extra:
    3 rows in set (0.00 sec)

    mysql> explain select stdV.name,score.score,exam.name from score left join stdV on stdV.id=score.id left  join exam on exam.id=score.examId where score.id=1000\G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: score
             type: ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: stdV
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
            Extra:
    *************************** 3. row ***************************
               id: 1
      select_type: SIMPLE
            table: exam
             type: eq_ref
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: mysqlOpt.score.examId
             rows: 1
            Extra:
    3 rows in set (0.00 sec)

  • 相关阅读:
    Selenium IDE的使用
    add jars、add external jars、add library、add class folder的区别
    java中equals和==区别
    java分布式事务
    六边形架构模式
    JAVA分布式事务原理及应用(转)
    HashMap的实现原理和底层数据结构
    HashMap实现原理分析
    深入Java集合学习系列:HashMap的实现原理
    23种设计模式汇总整理
  • 原文地址:https://www.cnblogs.com/zhangjun516/p/2866531.html
Copyright © 2011-2022 走看看