資料庫時間型別查詢未使用索引

2021-10-25 02:39:14 字數 529 閱讀 9246

由於業務需求,目前需要在資料庫中根據時間查詢資料,資料庫內欄位型別為datetime;

在sql中採用的是 date_sub函式獲取時間 —> date_sub(now(),interval 30 day) ;date_sub(now(),interval 30 day) 獲取的時間是varchar型別,無法使用到索引,由於資料量較大,故而影響效率較為嚴重.

原因尋找,型別不同而無法使用到索引,想辦法在sql內將varchar轉datetime;

使用過time函式,但是後來發現該函式獲取的值為時分秒,不可用; 後來使用 str_to_date 函式解決

str_to_date(date_sub(now(),interval 30 day),』%y-%m-%d %h:%i:%s』) 即可得到datetime型別

注: 此處也可使用convert函式解決,但該函式較為影響sql執行效率,不推薦.

資料庫索引型別

邏輯上 single column 單行索引 concatenated 多行索引 unique 唯一索引 nonunique 非唯一索引 function based函式索引 domain 域索引 物理上 partitioned 分割槽索引 nonpartitioned 非分割槽索引 b tree ...

資料庫索引型別

邏輯上 single column 單行索引 concatenated 多行索引 unique 唯一索引 nonunique 非唯一索引 function based函式索引 domain 域索引 物理上 partitioned 分割槽索引 nonpartitioned 非分割槽索引 b tree ...

使用索引優化資料庫查詢效率

使用索引優化資料庫查詢效率 1.不宜建立索引的情形 1 經常插入,修改和刪除的表 2 資料量比較小的表,因為查詢優化器在搜尋索引時所花費的時間可能會大於遍歷全表的資料所需要的時間 2.適合建立索引的情形 1 為where子句 現的列建立索引 2 建立組合索引 3 為group by子句 現的列建立索...