关于mysql的索引相关问题

-- no1
explain
select * from table1 t1 inner join table2 t2 on t1.ID=t2.A
where 
if(t1.ID is not null,t1.ID = 137,null)
 and  t1.B="nsnsnd" ;

-- no2

explain
select * from table1 t1 inner join table2 t2 on t1.ID=t2.A
where 
if(t1.ID is not null,t1.ID = 137,null)
 and  t1.B="B00012" 

and t1.C="C00123";
-- n03

explain
select * from table1 t1 inner join table2 t2 on t1.ID=t2.A
where 
t1.ID = 137;

t1.ID 为table1的主键,t1.C建有索引;

t2.A 建有索引

执行效果如下:




例1,由于在t1的主键上使用了函数,所以主键索引不起作用,全表扫描t2,然后通过t2使用t1主键索引,t2优先t1;

例2,使用t1的C索引,t2的A索引,同样t1主键索引不起作用,t1优先t2;

例3,主键上不加函数,走索引,t1优先t2;

总结:

按照表的关联顺序,查询表,索引上加函数,索引失效,但不影响该表其他索引的使用,如果不使用任何索引条件限制,但关联的其他表使用本表索引,则优先查关联表.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值