mysql查询指定索引

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013219624/article/details/89024895

最近再读《高性能MySQL》确实学到很多东西,今天在做索引优化的时候,有一条SQL没有走预期的索引,所以记录下
比如有如下表结构

CREATE TABLE `test` (
  `id` bigint(20) unsigned NOT NULL,
	`content` varchar(32) NOT NULL DEFAULT '',
  `last_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	PRIMARY key(id),
  KEY `idx_last_update_time` (`last_update_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

假如有如下SQL(其实我们想让它走idx_last_update_time索引,但是它走了主键索引)

explain select id,content,last_update_time from test where last_update_time>'2019-03-02' and id>2

这时我们可以通过force index强制它走idx_last_update_time索引

explain select id,content,last_update_time from test force index(idx_last_update_time) where last_update_time>'2019-03-02' and id>2

猜你喜欢

转载自blog.csdn.net/u013219624/article/details/89024895
今日推荐