Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u013219624/article/details/89024895
Recently read "High Performance MySQL" really learned a lot of things to do today in the index optimization, when there is a SQL index is not expected to go, so the next record
, such as the following table structure
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;
If the following SQL (in fact, we want it to go idx_last_update_time index, but it's gone primary key index)
explain select id,content,last_update_time from test where last_update_time>'2019-03-02' and id>2
Then we can force it to go idx_last_update_time index by force index
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