對時間字段 trans_date 添加普通索引。
使用where trans_date = ‘’ 是肯定會使用索引的。
但是使用< , > 比較符時就不一定了。
select count(1) from A; // 40000 EXPLAIN select * from A where trans_date = '20190428';
![]()
2. 使用> , 查詢語句沒有使用索引
select count(1) from t_trans_log_info where trans_date > '20190428'; //11200 EXPLAIN select * from t_trans_log_info where trans_date > '20190410';
![]()
3.
select count(1) from t_trans_log_info where trans_date > '20190528'; //1120 EXPLAIN select * from t_trans_log_info where trans_date > '20190528';
![]()
這時又使用了索引,說明時間字段 使用大於 時是否使用索引 是和結果的數量有關的,當數量較少(網上查到是有一個比例)時時使用索引的。
二。
復合索引: 索引並不強制出現在條件第一位,因為mysql會優化查詢。但是復合索引的首列必須出現在查詢條件中才可能使用該索引。
可以理解索引就是個字符串,從中間去找,效率極低。需要注意部分操作可能是索引失效,如不等於,函數計算,類型轉換,空,非空判斷等。
同時重復性較高的列不適合建立索引。
