2022-10-27 455
《MySQL死锁分析的两个工具》中,举了一个强制类型转换导致死锁的例子,有朋友询问是不是类型转换都不能命中索引,花1分钟细说一下。
第一类:“列类型”与“where值类型”不符,不能命中索引,会导致全表扫描(full table scan)。
数据准备:
createtablet1( cellvarchar(3)primarykey )engine=innodbdefaultcharset=utf8; insertintot1(cell)values('111'),('222'),('333');
测试语句:
explainselect*fromt1wherecell=111; explainselect*fromt1wherecell='111';
测试结果:
画外音:关于explain,详见《MySQL死锁分析的两个工具》。
第二类:相join的两个表的字符编码不同,不能命中索引,会导致笛卡尔积的循环计算(nested loop)。
数据准备:
createtablet2( cellvarchar(3)primarykey )engine=innodbdefaultcharset=latin1; insertintot2(cell)values('111'),('222'),('333'),('444'),('555'),('666'); createtablet3( cellvarchar(3)primarykey )engine=innodbdefaultcharset=utf8; insertintot3(cell)values('111'),('222'),('333'),('444'),('555'),('666');
测试语句:
explainselect*fromt1,t2wheret1.cell=t2.cell; explainselect*fromt1,t3wheret1.cell=t3.cell;
测试结果:
画外音:图片请放大。
总结
两类隐蔽的不能利用索引的case:
画外音:本文测试于MySQL5.6。
原文链接:https://77isp.com/post/10648.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态