零、背景

这周四收到很多告警,找DBA看了看,发现有个慢查询。

简单收集一些信息后,发现这个慢查询问题隐藏的很深,问了好多人包括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: BTREE6 rows in set (0.02 sec)
SELECT   count(*)FROM  d_union_stat.t_local_cache_log_metawhere  `c_mtime` < FROM_UNIXTIME(1494485402);
# Time: 170518 11:31:14# Query_time: 12.312329 Lock_time: 0.000061 Rows_sent: 0 Rows_examined: 5809647SET timestamp=1495078274;DELETE FROM `t_local_cache_log_meta` WHERE `c_mtime`< FROM_UNIXTIME(1494473461) limit 1000;

问为什么是慢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.

返回的是一个时间类型,那强制转化为字符串类型呢?

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: refpossible_keys: index_mtime     key: index_mtime   key_len: 137     ref: const     rows: 1    Extra: Using where1 row in set (0.01 sec)

二、结论

这次对 FROM_UNIXTIME 的返回值强制转化一下就可以利用上索引了。

所以这个SQL不能利用上索引是右值与左值的类型不一致导致的。 。

好了,不多说了, 这篇文章算是一个插曲,后面继续介绍算法吧。

更多相关文章

  1. Linux下MYSQL 5.7 找回root密码的问题(亲测可用)
  2. MySQL 什么时候使用INNER JOIN 或 LEFT JOIN
  3. Android(安卓)10 定位问题,获取NMEA(支持5.0~10.0)
  4. mybatisplus的坑 insert标签insert into select无参数问题的解决
  5. 关于Android(安卓)Studio3.2新建项目Android(安卓)resource link
  6. Android软键盘适配问题
  7. SlidingMenu和ActionBarSherlock结合做出出色的App布局,Facebook
  8. android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
  9. Android(安卓)Calendar使用过程中遇到的问题

随机推荐

  1. android应用程序跳转到系统的各个设置页
  2. (转) Android的Window类
  3. Android小项目——简单计算器的实现
  4. Android监听WIFI网络的变化并且获得当前
  5. Android使用XML全攻略(1)
  6. [安卓]Android(安卓)Developer Tools(Ecl
  7. Android 二维码开发功能实现(五)-----对z
  8. Android(安卓)AIDL基本步骤
  9. Socket Android手机客户端与PC服务端局域
  10. sscanf函数引起android 5.0卡死,C++中慎用