一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Mysql - mysql中索引與FROM_UNIXTIME的問題

mysql中索引與FROM_UNIXTIME的問題

2020-07-29 17:26tiankonguse Mysql

這篇文章主要介紹了mysql中索引與FROM_UNIXTIME的問題的相關資料,需要的朋友可以參考下

零、背景

這周四收到很多告警,找DBA看了看,發現有個慢查詢。

簡單收集一些信息后,發現這個慢查詢問題隱藏的很深,問了好多人包括DBA都不知道原因。

一、問題

有一個DB, 有一個字段, 定義如下.

?
1
2
3
4
5
6
7
8
9
10
MySQL [d_union_stat]> desc t_local_cache_log_meta;
+----------------+--------------+------+-----+---------------------+
| Field     | Type     | Null | Key | Default       |
+----------------+--------------+------+-----+---------------------+
| c_id      | int(11)   | NO  | PRI | NULL        |
| c_key     | varchar(128) | NO  | MUL |           |
| c_time     | int(11)   | NO  | MUL | 0          |
| c_mtime    | varchar(45) | NO  | MUL | 0000-00-00 00:00:00 |
+----------------+--------------+------+-----+---------------------+
17 rows in set (0.01 sec)

索引如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
MySQL [d_union_stat]> show index from t_local_cache_log_meta \G    
*************************** 1. row ***************************
    Table: t_local_cache_log_meta
  Non_unique: 0
   Key_name: PRIMARY
 Column_name: c_id
  Collation: A
 Cardinality: 6517096
  Index_type: BTREE
*************************** 2. row ***************************
.
.
.
*************************** 6. row ***************************
    Table: t_local_cache_log_meta
  Non_unique: 1
   Key_name: index_mtime
 Column_name: c_mtime
  Collation: A
 Cardinality: 592463
  Index_type: BTREE
6 rows in set (0.02 sec)

然后我寫了一個SQL如下:

?
1
2
3
4
5
6
SELECT
  count(*)
FROM
  d_union_stat.t_local_cache_log_meta
where
  `c_mtime` < FROM_UNIXTIME(1494485402);

終于有一天DBA過來了, 扔給我一個流水,說這個SQL是慢SQL。

?
1
2
3
4
# Time: 170518 11:31:14
# Query_time: 12.312329 Lock_time: 0.000061 Rows_sent: 0 Rows_examined: 5809647
SET timestamp=1495078274;
DELETE FROM `t_local_cache_log_meta` WHERE `c_mtime`< FROM_UNIXTIME(1494473461) limit 1000;

我頓時無語了,我的DB都是加了索引,SQL都是精心優化了的,怎么是慢SQL呢?

問為什么是慢SQL,DBA答不上來, 問了周圍的同事也都答不上來。

我心里暗想遇到一個隱藏很深的知識點了。

令人懷疑的地方有兩個:1.有6個索引。 2. 右值是 FROM_UNIXTIME 函數。

于是查詢MYSQL官方文檔,發現6個不是問題。

All storage engines support at least 16 indexes per table and a total index length of at least 256 bytes.  
Most storage engines have higher limits.

于是懷疑問題是 FROM_UNIXTIME 函數了。

然后看看MYSQL的INDEX小節,找到一點蛛絲馬跡。

1.To find the rows matching a WHERE clause quickly.
2. To eliminate rows from consideration.
 If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows.
3.If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows.
4. MySQL can use indexes on columns more efficiently if they are declared as the same type and size.
 Comparison of dissimilar columns (comparing a string column to a temporal or numeric column, for example) may prevent use of indexes if values cannot be compared directly without conversion.

看到第4條的時候,提到不同類型可能導致不走索引,難道 FROM_UNIXTIME 的返回值不能轉化為字符串類型?

于是查詢 FROM_UNIXTIME 函數的返回值。

MySQL FROM_UNIXTIME() returns a date /datetime from a version of unix_timestamp.

返回的是一個時間類型,那強制轉化為字符串類型呢?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MySQL [d_union_stat]> explain SELECT
  ->   *
  -> FROM
  ->   t_local_cache_log_meta
  -> where
  ->   `c_mtime` = CONCAT(FROM_UNIXTIME(1494485402)) \G
*************************** 1. row ***************************
      id: 1
 select_type: SIMPLE
    table: t_local_cache_log_meta
     type: ref
possible_keys: index_mtime
     key: index_mtime
   key_len: 137
     ref: const
     rows: 1
    Extra: Using where
1 row in set (0.01 sec)

這次可以看到, 使用了索引,只掃描了一個數據。

二、結論

這次對 FROM_UNIXTIME 的返回值強制轉化一下就可以利用上索引了。

所以這個SQL不能利用上索引是右值與左值的類型不一致導致的。 。

好了,不多說了, 這篇文章算是一個插曲,后面繼續介紹算法吧。

原文鏈接:http://github.tiankonguse.com/blog/2017/05/19/mysql-index-FROM_UNIXTIME.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产99热| 欧美日韩第二页 | 成年男女免费大片在线观看 | 腿交hd| 亚洲欧洲日产国码天堂 | 无码精品一区二区三区免费视频 | 国产成人福利免费观看 | 午夜片神马影院福利 | 国产成人高清亚洲一区91 | 成人影院免费在线观看 | 美女裆部 | 2022最新a精品视频在线观看 | 99re5精品视频在线观看 | 韩国美女vip内部2020 | 视频一区二区三区在线观看 | 91精品综合 | 97菊爱网 | 娇妻终于接受了3p的调教 | 欧美视频在线播放观看免费福利资源 | 色一情一区二区三区四区 | 小早川怜子亚洲综合中文字幕 | 亚洲经典| 成年人在线免费看 | 日韩视频一区二区三区 | 四虎成人影院 | 天天摸天天碰色综合网 | 我的好妈妈7中字在线观看韩国 | 桥本有菜在线四虎福利网 | 99网站在线观看 | 隔壁老王国产精品福利 | 9色视频在线观看 | 午夜a一级毛片 | 午夜dj影院在线视频观看完整 | 精品人人做人人爽久久久 | 甜蜜惩罚小说 | 色哟哟在线观看 | 四虎影视4hu最新地址在线884 | 亚洲色图欧美视频 | gangbang日本| 5g影院天天5g天天爽大陆 | 久久re视频这里精品一本到99 |