mysql concat 索引 Mysql索引總結

2021-10-18 09:03:37 字數 2010 閱讀 5689

日期型別可以直接和string格式的字串比較

select * from *** where event_time>'2018-06-02' 可以使用索引, mysql缺省會把後面的字串轉成date型別。可以使用between and

select * from *** where date(event_time)>'2018-06-02'   不能使用索引

如果時間戳日期和時間都要比較, 最好使用兩個字段儲存這個時間戳, 這樣可以利用索引

select * from *** where event_time>1239237428734; --  使用錯誤, 不能這麼用。可以使用from_unixtime()將數字轉成日期型別

將date和time合併成乙個datetime

select str_to_date(concat(icdate,' ',ictime),'%m/%d/%y %h:%i:%s %s')   from ***x

其他索引總結:

where條件等號兩邊字段型別不同,不走索引

like '***%' 走索引, '%***%'不走索引

對欄位進行函式運算不走索引

組合索引 只使用後面的字段不走索引,使用前後的字段走索引. 第乙個欄位有參於(而且字段型別匹配 沒有函式運算),那麼會走索引, 第乙個字段可以在sql中的任意位置

組合索引遇到第乙個不等值條件 即中斷後面字段使用索引

字段型別不匹配,不走索引

示例:

name varchar, addr varchar, age int。 建立組合索引為name + addr + age

select * from t_user where name like '123%' and age>19;  走索引 type=range

select * from t_user where age>19 and name like '123%' ;  走索引 type=range

select * from t_user where name =123 不走索引 type=all

select * from t_user where name='123' 走索引 type=ref

select * from t_user where addr like '上城%' and age>30; 不走索引 type=all

explain 是否使用了索引

type=all是全表掃瞄

只要不是all, 都能使用索引,只是使用索引的方式不同,效能也有差異==>依次從好到差:system,const,eq_ref,ref,fulltext,ref_or_null,unique_subquery,index_subquery,range,index_merge,index,all

索引不是越多越好, 兩個index進行merge有時還不如只使用乙個index

組合索引建立的依據:select distinct(***)/count(*) 值越大說明區分度越高 組合索引的價值越高

什麼時候建立組合索引?  單列上查詢出來的資料量都很大,但是兩個組合查詢的結果很小 ,此時建立組合索引就比較有意義

(原始的離線度都小,組合的離散度會大???)    0.8*0.8=0.64       0.8*0.5=0.4    如果整體的資料量級別很大,也有點效果

索引的型別

orderby 是否使用索引

orderby用到的索引和where中的索引如果一致, 可以提公升不少效能

字元型別的索引需要指定長度

alter table t_yyy add index `***` (`request_type` asc, `request_param`(15) asc);

MySQL concat函式的使用

mysql concat函式是mysql資料庫中眾多的函式之一,下文將對mysql concat函式的語法和使用進行說明,供您參考和學習。mysql concat函式使用方法 concat str1,str2,返回結果為連線引數產生的字串。如有任何乙個引數為null 則返回值為 null。注意 如果...

mysql concat函式 拼接null的問題

以前專案中也用到了 mysql concat函式 拼接一些商品名稱 一些特殊關鍵字等等 今天用到時,有乙個欄位是null的 結果拼接的後的新字段 為null 驚了 這個平時沒有注意到 特別去檢視了下資料 原來 mysql的concat函式拼接規則是 當多個拼接的字段的字段值中存在null時,返回的一...

mysql concat函式進行模糊查詢

concat 函式,是用來連線字串。精確查詢 select from user where name zhangsan 模糊查詢 select from user where name like zhang 在實際的使用中,條件是作為引數傳遞進來的。所以我們使用 concat 函式 mybatis ...