久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

mysql中索引與FROM

134次閱讀
沒有評論

共計 3168 個字符,預(yù)計需要花費 8 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

丸趣 TV 小編給大家分享一下 mysql 中索引與 FROM_UNIXTIME 的問題示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

零、背景

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

一、問題

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

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)

索引如下:

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 如下:

SELECT 
 count(*)
 d_union_stat.t_local_cache_log_meta
where
 `c_mtime`   FROM_UNIXTIME(1494485402);

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

# 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 都是精心優(yōu)化了的,怎么是慢 SQL 呢?

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

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

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

于是查詢 MYSQL 官方文檔,發(fā)現(xiàn) 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 函數(shù)了。

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

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 的返回值不能轉(zhuǎn)化為字符串類型?

于是查詢 FROM_UNIXTIME 函數(shù)的返回值。

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

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

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)

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

二、結(jié)論

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

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

以上是“mysql 中索引與 FROM_UNIXTIME 的問題示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道!

向 AI 問一下細節(jié)

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-12-04發(fā)表,共計3168字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 苍溪县| 漾濞| 台山市| 仁布县| 嘉鱼县| 奈曼旗| 偃师市| 宜兴市| 太白县| 扶余县| 鹰潭市| 昆明市| 渝北区| 丹阳市| 石家庄市| 吉木萨尔县| 崇信县| 昆明市| 濮阳市| 乳源| 色达县| 郧西县| 嘉荫县| 灵台县| 上思县| 桐城市| 阿拉尔市| 福清市| 奎屯市| 丰城市| 洛川县| 宣恩县| 万全县| 苍溪县| 香格里拉县| 海宁市| 连云港市| 潍坊市| 黄龙县| 青铜峡市| 松溪县|