mysql复合索引使用场景_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

b85a245dc4974ecdddb1118f81e8ba8e.png

匹配值的范围查询

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

be0b9bfe7e8a0c57f738a150da908bf1.png

匹配最左前缀

仅仅使用索引中的最左列进行查询,比如:在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);

21e05d0808e4740f2f8d2bc4b53f490e.png

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

ef56a0bdaaa6c9a0dd24a6e35dcfed9f.png

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

b7a46bc144acaf7be37594c3bcc4b0f7.png

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

fbb29a4fe0e1ec7d8f5332e3742f12fb.png

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

69d8093ac81058746a0d6259f1044c54.png

仅仅对索引进行查询

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

explain select last_update from payment where amount=3.98\G;

88b842801708bbbc91779b77b40c429f.png

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

1d2d4f221710a4e949cab12c9f4dca74.png

匹配列前缀

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

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

6f14e66afd40e3344f607646c0a44a41.png

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

0564d8fc446c46f9585fc81ededd3b44.png

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

explain select * from payment where rental_id is null\G;

b543ae91b06cc4ce9005c66683eedd59.png

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

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

77b60eb04f077bb4b77734c444005208.png

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<=400\G;

c04fc6d20c4f3aa3f2b693d31f11d90f.png

5.5版本的复合索引

af2c94bafc62d7fbf65c818e848fa193.png

5.6版本的ICP

d783d9f33a878d4727a9d3450a5235f0.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值