zoukankan      html  css  js  c++  java
  • mysql中能够使用索引的典型场景

    mysql 演示数据库:http://downloads.mysql.com/docs/sakila-db.zip

    匹配全值


    explain select * from rental where rental_date='2005-05-25 17:22:10' and inventory_id=373 and customer_id=343

    匹配值的范围查询


    explain select * from rental where customer_id >= 373 and customer_id<400

    匹配最左前缀


    仅仅使用索引中的最左列进行查询,比如:在col1+col2+col3字段上的联合索引中,能够使用的索引情况可以有:col1 (col2+col3) ,col1+col2+col3。不能被使用的索引:col2 (col2+col3) 等情况。以payment表为例

    alter table payment add index idx_payment_date (payment_date,amount,last_update);

    explain select * from payment where payment_date='2006-02-14 15:16:03' and last_update='2006-02-15 22:12:32'G;

     explain select * from payment where amount=3.98 and last_update='2006-02-15 22:12:32'G;

    explain select * from payment where amount=3.98 and payment_date='2006-02-14 15:16:03'G;

    explain select * from payment where last_update='2006-02-15 22:12:32' and payment_date='2006-02-14 15:16:03'G;

     仅仅对索引进行查询


     当查询的的列都在索引的字段中时,查询的效率更高

    explain select last_update from payment where amount=3.98G;

    explain select last_update from payment where payment_date='2006-02-14 15:16:03'G;

     

    匹配列前缀


    仅仅使用索引中的第一列,并且只包含索引第一列的开头一部分进行查找

     

    explain select title from film_text where title like 'AFRICAN%'G;

     explain select description from film_text where description like 'AFRICAN%'G;

     如果列名是索引,那么使用column_name is null 就会使用索引


    explain select * from payment where rental_id is nullG;

    实现索引匹配部分精确而其他部分进行范围匹配


     

    explain select inventory_id from rental where rental_date='2006-02-14 15:16:03' and customer_id>=300 and customer_id<=400G;

    Index Condition PushDown(ICP)


    mysql 5.6引入了Index Condition PushDown(ICP) 的特性,进一步优化了查询,某些情况下的条件过滤操作下放到存储引擎

    explain select * from rental where rental_date='2006-02-14 15:16:03' and customer_id>=300 and customer_id<=400G;

    5.5版本的复合索引

     

    5.6版本的ICP

  • 相关阅读:
    27 Spring Cloud Feign整合Hystrix实现容错处理
    26 Spring Cloud使用Hystrix实现容错处理
    25 Spring Cloud Hystrix缓存与合并请求
    24 Spring Cloud Hystrix资源隔离策略(线程、信号量)
    23 Spring Cloud Hystrix(熔断器)介绍及使用
    22 Spring Cloud Feign的自定义配置及使用
    21 Spring Cloud使用Feign调用服务接口
    20 Spring Cloud Ribbon配置详解
    19 Spring Cloud Ribbon自定义负载均衡策略
    18 Spring Cloud Ribbon负载均衡策略介绍
  • 原文地址:https://www.cnblogs.com/dsitn/p/7088921.html
Copyright © 2011-2022 走看看