mysql查询指定索引

最近再读《高性能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
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值