最左原则,这种情况为什么会用到索引?
建表语句:
CREATE TABLE `user` (`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` char(10) NOT NULL,
`nick_name` varchar(20) NOT NULL DEFAULT '' COMMENT '昵称',
`job` varchar(20) NOT NULL DEFAULT '' COMMENT '职业',
PRIMARY KEY (`id`),
KEY `index_name` (`name`,`nick_name`,`job`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
插入的数据:
查询的语句:
explain select * from user where nick_name = 'ligoudan' and job = 'dog';
分析结果:
[{
"id": 1,
"select_type": "SIMPLE",
"table": "user",
"partitions": null,
"type": "index",
"possible_keys": "index_name",
"key": "index_name",
"key_len": "204",
"ref": null,
"rows": 3,
"filtered": 33.33,
"Extra": "Using where; Using index"
}
]
查询的语句:
explain select * from user where job = 'ligoudan';
分析结果:
[{
"id": 1,
"select_type": "SIMPLE",
"table": "user",
"partitions": null,
"type": "index",
"possible_keys": "index_name",
"key": "index_name",
"key_len": "204",
"ref": null,
"rows": 3,
"filtered": 33.33,
"Extra": "Using where; Using index"
}
]
我想不明白为什么会使用到索引,看了一些文章,最左原则,如果建了(a、b、c)的索引,说只有a 、ab 、abc会使用索引,可是我实际测试,b、c、bc这三种组合也使用到了索引,是我哪里理解不对么?
回答
Mysql新版的查询优化器,会自动优化查询顺序
以上是 最左原则,这种情况为什么会用到索引? 的全部内容, 来源链接: utcz.com/a/23497.html