这期内容当中小编将会给大家带来有关mysql中怎么实现索引最左原则,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

建表

CREATETABLE`user`(`id`int(10)unsignedNOTNULLAUTO_INCREMENT,`name`varchar(10)DEFAULTNULL,`sex`tinyint(1)DEFAULTNULL,`age`tinyint(2)DEFAULTNULL,PRIMARYKEY(`id`),KEY`Index_user`(`name`,`age`)USINGBTREE)ENGINE=InnoDBAUTO_INCREMENT=4DEFAULTCHARSET=utf8mb4;

测试sql

第一种

mysql>explainSELECT*FROM`user`wherename="tom"\G***************************1.row***************************id:1select_type:SIMPLEtable:userpartitions:NULLtype:refpossible_keys:Index_userkey:Index_userkey_len:43ref:constrows:1filtered:100.00Extra:NULL

第二种

mysql>explainSELECT*FROM`user`whereage=18andname="tom"\G***************************1.row***************************id:1select_type:SIMPLEtable:userpartitions:NULLtype:refpossible_keys:Index_userkey:Index_userkey_len:45ref:const,constrows:1filtered:100.00Extra:NULL

第三种

mysql>explainSELECT*FROM`user`whereage=18\G***************************1.row***************************id:1select_type:SIMPLEtable:userpartitions:NULLtype:ALLpossible_keys:NULLkey:NULLkey_len:NULLref:NULLrows:3filtered:33.33Extra:Usingwhere1rowinset,1warning(0.00sec)

第四种

mysql>explainSELECT*FROM`user`wherename="tom"andage=18\G***************************1.row***************************id:1select_type:SIMPLEtable:userpartitions:NULLtype:refpossible_keys:Index_userkey:Index_userkey_len:45ref:const,constrows:1filtered:100.00Extra:NULL1rowinset,1warning(0.00sec)

上述就是小编为大家分享的mysql中怎么实现索引最左原则了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。