mysql查詢指定索引

2021-09-16 18:48:58 字數 834 閱讀 2390

最近再讀《高效能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

mysql負向查詢索引 談談MySQL 索引

1 索引是什麼 索引 index 是幫助mysql高效獲取資料的資料結構。我們可以簡單理解為 索引的目的在於提高查詢效率。2 原理 索引的資料結構是b 樹,原理圖如下 精簡描述 b 樹是為了磁碟或其他直接儲存輔助裝置設計的一種平衡查詢樹。在b 樹中,所有記錄節點都是按鍵值的大小順序存放在葉子節點上,...

mysql指定索性 MySQL入門之索引

索引 1 什麼是索引 索引是一種快速查詢表中內容的機制,使用索引可以提高查詢效率,相當於字典的目錄 可以將查詢過程中經常使用的條件設為索引 2 mysql中索引的型別 1 普通索引 這是最基本的索引型別,而且它沒有唯一性之類的限制。普通索引可以通過以下幾種方式建立 建立索引,例如create ind...

mysql 指定時間 MySQL查詢指定時間的資料

mysql查詢指定時間的資料 user event 使用者事件表 create time 表中儲存時間的字段 獲取當月資料 select from user event where date format create time,y m date format now y m 獲取3月份資料 sel...