2026/5/14 17:56:14
网站建设
项目流程
顺德中小企业网站建设,北京网站设计公司兴田德润怎么样,aws配置wordpress,北京免费做网站验证索引效率在未建立索引之前#xff0c;执行如下SQL语句#xff0c;查询SQL的耗时#xff1a;select * from tb_sku where snSN0003450001针对字段创建索引create index idx_sku_sn on tb_sku(sn);创建完索引之后#xff0c;再来看这条查询sql的耗时。查看sql的执行计划最…验证索引效率在未建立索引之前执行如下SQL语句查询SQL的耗时select * from tb_sku where snSN0003450001针对字段创建索引create index idx_sku_sn on tb_sku(sn);创建完索引之后再来看这条查询sql的耗时。查看sql的执行计划最左前缀法则如果索引了多列联合索引要遵守最左前缀法则。最左前缀法则指的是查询从索引的最左列开始并且不跳过索引中的列。如果跳跃某一列索引将部分失效后面的字段索引失效。-- 1. 查询profession、age、status三个条件 explain select * from tb_user where profession 软件工程 and age 31 and status 0; -- 2. 查询profession、age两个条件 explain select * from tb_user where profession 软件工程 and age 31; -- 3. 仅查询profession条件 explain select * from tb_user where profession 软件工程; -- 4. 查询age、status两个条件缺少profession explain select * from tb_user where age 31 and status 0; -- 5. 仅查询status条件 explain select * from tb_user where profession 软件工程 and status 0;tb_user表中的索引如下最左前缀法则的验证a)执行select * from tb_user where profession软件工程 and age31 and status0;可能使用的索引是 idx_pro_age_sta; 实际使用的索引是 idx_pro_age_sta;b执行select * from tb_user where profession 软件工程 and age 31;可能使用的索引是idx_pro_age_sta, 实际使用的索引是idx_pro_age_sta;idx_pro_age_sta;c) 执行select * from tb_user where profession 软件工程;可能使用的索引是idx_pro_age_sta, 实际使用的索引是idx_pro_age_staidx_pro_age_stad执行select * from tb_user where age 31 and status 0;没有匹配的索引可以使用。e执行select * from tb_user where profession 软件工程 and status 0;索引的长度是43说明只有profession字段使用到了索引 status字段的索引失效。f)执行select * from tb_user where age 31 and status 0 and profession 软件工程;索引长度是55说明三个字段的索引都用上了。这说明字段在where条件中的位置无关。范围查询联合索引中出现范围查询(,explain select * from tb_user where profession 软件工程 and age 30 and status 0; explain select * from tb_user where profession 软件工程 and age 30 and status 0;age 使用了范围查询age右边的列status会失效因此索引的长度是48.age 使用的范围查询时age右边的列不会索引失效。