CREATE TABLE `db0206`.`article`(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`author_id` INT(11) UNSIGNED NOT NULL,
`category_id` INT(11) UNSIGNED NOT NULL,
`views` INT(11) UNSIGNED NOT NULL,
`comments` INT(11) UNSIGNED NOT NULL,
`title` VARCHAR(255) NOT NULL,
`content` TEXT NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB CHARSET=utf8;
CREATE INDEX idx_article_cv ON article(category_id,views);
explain结果
为什么会用到(category_id,views)这个联合索引呢? 中间的range是如何跳过的?
根据最左前缀匹配原则,联合索category_id,views,可组成如下:category_id,category_id、views两种情况,所以走索引