zoukankan      html  css  js  c++  java
  • Mysql基础(二十):mysql性能优化(五)explain 使用

    先来一张表:

    CREATE TABLE IF NOT EXISTS `article` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `author_id` int(10) unsigned NOT NULL,
    `category_id` int(10) unsigned NOT NULL,
    `views` int(10) unsigned NOT NULL,
    `comments` int(10) unsigned NOT NULL,
    `title` varbinary(255) NOT NULL,
    `content` text NOT NULL,
    PRIMARY KEY (`id`)
    );

    再插几条数据:

    INSERT INTO `article`
    (`author_id`, `category_id`, `views`, `comments`, `title`, `content`) VALUES
    (1, 1, 1, 1, '1', '1'),
    (2, 2, 2, 2, '2', '2'),
    (1, 1, 3, 3, '3', '3');

    需求:
    查询 category_id 为 1 且 comments 大于 1 的情况下,views 最多的 article_id。
    先查查试试看:

    EXPLAIN
    SELECT author_id
    FROM `article`
    WHERE category_id = 1 AND comments > 1
    ORDER BY views DESC
    LIMIT 1G

    看看部分输出结果:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: article
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 3
            Extra: Using where; Using filesort
    1 row in set (0.00 sec)

    很显然,type 是 ALL,即最坏的情况。Extra 里还出现了 Using filesort,也是最坏的情况。优化是必须的。

    嗯,那么最简单的解决方案就是加索引了。好,我们来试一试。查询的条件里即 where 之后共使用了 category_id,comments,views 三个字段。那么来一个联合索引是最简单的了。

    ALTER TABLE `article` ADD INDEX x ( `category_id` , `comments`, `views` );

    结果有了一定好转,但仍然很糟糕:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: article
             type: range
    possible_keys: x
              key: x
          key_len: 8
              ref: NULL
             rows: 1
            Extra: Using where; Using filesort
    1 row in set (0.00 sec)

    type 变成了 range,这是可以忍受的。但是 extra 里使用 Using filesort 仍是无法接受的。但是我们已经建立了索引,为啥没用呢?这是因为按照 BTree 索引的工作原理,先排序 category_id,如果遇到相同的 category_id 则再排序 comments,如果遇到相同的 comments 则再排序 views。当 comments 字段在联合索引里处于中间位置时,因comments > 1 条件是一个范围值(所谓 range),MySQL 无法利用索引再对后面的 views 部分进行检索,即 range 类型查询字段后面的索引无效。
    那么我们需要抛弃 comments,删除旧索引:

     DROP INDEX x ON article;

    然后建立新索引:

    ALTER TABLE `article` ADD INDEX y ( `category_id` , `views` ) ;

    接着再运行查询:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: article
             type: ref
    possible_keys: y
              key: y
          key_len: 4
              ref: const
             rows: 1
            Extra: Using where
    1 row in set (0.00 sec)

    可以看到,type 变为了 ref,Extra 中的 Using filesort 也消失了,结果非常理想。
    再来看一个多表查询的例子。
    首先定义 3个表 class 和 room。

    CREATE TABLE IF NOT EXISTS `class` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `card` int(10) unsigned NOT NULL,
    PRIMARY KEY (`id`)
    );
    CREATE TABLE IF NOT EXISTS `book` (
    `bookid` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `card` int(10) unsigned NOT NULL,
    PRIMARY KEY (`bookid`)
    );
    CREATE TABLE IF NOT EXISTS `phone` (
    `phoneid` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `card` int(10) unsigned NOT NULL,
    PRIMARY KEY (`phoneid`)
    ) engine = innodb;

    然后再分别插入大量数据。插入数据的php脚本:

     
    <?php
    $link = mysql_connect("localhost","root","870516");
    mysql_select_db("test",$link);
    for($i=0;$i<10000;$i++)
    {
        $j   = rand(1,20);
        $sql = " insert into class(card) values({$j})";
        mysql_query($sql);
    }
    for($i=0;$i<10000;$i++)
    {
        $j   = rand(1,20);
        $sql = " insert into book(card) values({$j})";
        mysql_query($sql);
    }
    for($i=0;$i<10000;$i++)
    {
        $j   = rand(1,20);
        $sql = " insert into phone(card) values({$j})";
        mysql_query($sql);
    }
    mysql_query("COMMIT");
    ?>

    然后来看一个左连接查询:

    explain select * from class left join book on class.card = book.cardG

    分析结果是:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra: 
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra: 
    2 rows in set (0.00 sec)

    显然第二个 ALL 是需要我们进行优化的。
    建立个索引试试看:

    ALTER TABLE `book` ADD INDEX y ( `card`);
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra: 
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ref
    possible_keys: y
              key: y
          key_len: 4
              ref: test.class.card
             rows: 1000
            Extra: 
    2 rows in set (0.00 sec)

    可以看到第二行的 type 变为了 ref,rows 也变成了 1741*18,优化比较明显。这是由左连接特性决定的。LEFT JOIN 条件用于确定如何从右表搜索行,左边一定都有,所以右边是我们的关键点,一定需要建立索引。
    删除旧索引:

    DROP INDEX y ON book;

    建立新索引。

    ALTER TABLE `class` ADD INDEX x ( `card`);

    结果

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra: 
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra: 
    2 rows in set (0.00 sec)

    基本无变化。
           然后来看一个右连接查询:

    explain select * from class right join book on class.card = book.card;

    分析结果是:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra: 
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ref
    possible_keys: x
              key: x
          key_len: 4
              ref: test.book.card
             rows: 1000
            Extra: 
    2 rows in set (0.00 sec)

    优化较明显。这是因为 RIGHT JOIN 条件用于确定如何从左表搜索行,右边一定都有,所以左边是我们的关键点,一定需要建立索引。
    删除旧索引:

    DROP INDEX x ON class;

    建立新索引。

    ALTER TABLE `book` ADD INDEX y ( `card`);

    结果

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra: 
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra: 
    2 rows in set (0.00 sec)

    基本无变化。

    最后来看看 inner join 的情况:

    复制代码代码如下:

    explain select * from class inner join book on class.card = book.card;


    结果:

    复制代码代码如下:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra:
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ref
    possible_keys: x
              key: x
          key_len: 4
              ref: test.book.card
             rows: 1000
            Extra:
    2 rows in set (0.00 sec)


    删除旧索引:

    复制代码代码如下:

    DROP INDEX y ON book;


    结果

    复制代码代码如下:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra:
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra:
    2 rows in set (0.00 sec)


    建立新索引。

    复制代码代码如下:

    ALTER TABLE `class` ADD INDEX x ( `card`);


    结果

    复制代码代码如下:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra:
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra:
    2 rows in set (0.00 sec)


    综上所述,inner join 和 left join 差不多,都需要优化右表。而 right join 需要优化左表。

    我们再来看看三表查询的例子

    添加一个新索引:

    复制代码代码如下:

    ALTER TABLE `phone` ADD INDEX z ( `card`);
    ALTER TABLE `book` ADD INDEX y ( `card`);
    复制代码代码如下:

    explain select * from class left join book on class.card=book.card left join phone on book.card = phone.card;
    复制代码代码如下:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: class
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 20000
            Extra:
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: book
             type: ref
    possible_keys: y
              key: y
          key_len: 4
              ref: test.class.card
             rows: 1000
            Extra:
    *************************** 3. row ***************************
               id: 1
      select_type: SIMPLE
            table: phone
             type: ref
    possible_keys: z
              key: z
          key_len: 4
              ref: test.book.card
             rows: 260
            Extra: Using index
    3 rows in set (0.00 sec)


    后 2 行的 type 都是 ref 且总 rows 优化很好,效果不错。

    MySql 中的 explain 语法可以帮助我们改写查询,优化表的结构和索引的设置,从而最大地提高查询效率。当然,在大规模数据量时,索引的建立和维护的代价也是很高的,往往需要较长的时间和较大的空间,如果在不同的列组合上建立索引,空间的开销会更大。因此索引最好设置在需要经常查询的字段中。

     

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/14010203.html

  • 相关阅读:
    前端打包利器:webpack工具
    asp.net 通过ajax方式调用webmethod方法使用自定义类传参及获取返回参数
    C#报错:创建调试信息文件 ……objDebugmodel.pdb: 拒绝访问
    ts 使用Visual Studio2012和TFS网站管理源代码
    Win7(包括32和64位)使用GitHub
    C#程序开发中经常遇到的10条实用的代码
    简单优化实现大数据量的重复判断和导入
    Asp.Net修改上传文件大小限制(修改web.config)
    XlFileFormat
    Excel 2007中的新文件格式
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14010203.html
Copyright © 2011-2022 走看看