MySQL优化-03

来源:互联网 发布:关于套路的网络用语 编辑:程序博客网 时间:2024/06/05 19:59

1:explain select goods_id,goods_name from ecs_goods \G   goods_id为索引

      id:1  编号

      select-type:SIMPLE 查询方式 

       table:ecs_goods 表格

      type  : ALL 

      possible-keys:NULL 可能用到的索引为NULL

      key:NULL 实际上用到的索引为NULL

      key-len:NULL 索引长度

      ref:NULL 两表联查的时候的关联性

      rows:22 估计要查询的行

      Extra:

2 : explain select * from ecs_goods where goods_id in (select good_id from ecs_goods where cat_id=3) \G

      包括两个查询主句

      id:1  编号

      select-type:PRIMARY //主查询  unique_subquery //非类型查询

      table:ecs_goods 表格

      possible-keys:NULL 可能用到的索引为NULL

      key:NULL 实际上用到的索引为NULL

      key-len:NULL 索引长度

      ref:NULL 两表联查的时候的关联性

      rows:22 估计要查询的行

      Extra:

3:explain 3 union select 4 \G

       id:1  编号

       select-type:PRIMARY

       table:ecs_goods 表格

       type  : NULL //当出现type的值为 const system 的时候 表示在查询速度上已经很快了,接近常数级别。

       possible-keys:NULL 可能用到的索引为NULL

       key:NULL 最终使用到的索引

       key-len:NULL 索引长度

       ref:NULL 两表联查的时候的关联性

       rows:22 估计要查询的行

       Extra:No table used //没有表格使用

4:explain select * from ecs_goods where goods_id >30 \G //goods_id 为主键索引

       id:1  编号

       select-type:SIMPLE //表示当前语句知识简单的查询

       table:ecs_goods 表格

       type  : range //利用主键实现范围的查询,因为tree索引对于范围查询时使用的。

       possible-keys:primary //可能用到的主键索引

       key:primary //实际用到的是主键索引

       key-len:3  //索引长度为3

       ref:NULL 无相互关联

       rows:50 估计要查询的行

       Extra:using where 使用到where 语句

5:explain select * from ecs_goods where goods_id+1 >30 \G //goods_id 为主键索引

      要注意的是在where子句之后使用表达式 是不能用到任何索引的。where 子句之后用到的必须是完全孤立的列。

      但是下面的情况用到了索引

      explain select email from email where rigit(email,length(email)-position('@ in eamil')) ='@qq.com' \G 

       \g 表示用横行显示

      \G 表示用竖行显示

      如果 eamil 是 索引的话 , 这里 用到了 索引覆盖 用到的 Extra:using index 表示用到索引,避免回行的出现。

      如果语句修改为 

      explain select  email,uname from email ......................................同上

      那么就不能用到索引覆盖,需要没走一个索引进行一次回行

6: explain select * from ecs_goods where parent_id=10 \G;

       id:1  编号

       select-type:SIMPLE //表示当前语句知识简单的查询

       table:ecs_goods 表格

       type  : ALL //表示的是从表中将所有数据全部查询一次,不去走索引 实际上是data_all

       possible-keys:NULL

       key:NULL

       key-len:3  //索引长度为3

       ref:NULL 无相互关联

       rows:50 估计要查询的行

       Extra:using where 使用到where 语句

7:explain select email from email \G 其中email是主键

     type  : index   比ALL快点儿,实际上是遍历Btree上的每一个节点,然后回行  索引说实际上 同样是查询了全部数据 但是提前利用到了索引

8:explain select * from ecs_goods order by goods_id \G //goods_id 为主键

        id:1  编号

       select-type:SIMPLE //表示当前语句知识简单的查询

       table:ecs_goods 表格

       type  : ALL // 虽然说排序用到了 主键索引 但是 type 类型还是 ALL

      原因是:由于查询的是全部数据,所以真正执行的操作是 先将所有的数据查询出来然后,然后进行    文件排序

       possible-keys:NULL

       key:NULL

       key-len:3  //索引长度为3

       ref:NULL 无相互关联

       rows:50 估计要查询的行

       Extra:using filesort //一般文件排序是在内存中执行的,但是当排序的字段为text型或者更长的时候,就会去磁盘进行排序,这样的速度会更慢

9:explain select * from ecs_goods where goods_id>25   //goods_id 为主键

      type:range //范围查询 Btree 索引对于range 查询是有益的

      explain select * from ecs_goods where goods_id=20

      type:const //type 为const null system 速度上相差不大 都是非常快的

      但是

      explain select * from ecs_goods where cat_id =3  //cat_id 为外键 

      type的类型为 ref 

                             eq_ref  

      表示通过一个小范围的查询就可以查到最后的结果

10:explain select cat_id ,count(*) from ecs_category group by cat_id   //cat_id 为主键索引

       Extra:using index 用到了索引的原因是什么?  统计只需要每次先从索引上查询一次,然后在进行+1操作,不需要回行再去找数据,所以利用到了索引

       explain select cat_id ,avg(shop_price) from ecs_category group by cat_id

       Extra:using temporary using filesort   什么也没有用到的原因是什么? 没有用到索引的原因是由于需求是就算整个表的平均值,索引Mysql要做是将整个表的数据一次查找出来,没有必要再去索引查询,这样会更慢。

         

       

 



 



 

原创粉丝点击