mysql中索引与FROM_UNIXTIME的问题详解


本文摘自PHP中文网,作者小云云,侵删。

本文主要介绍mysql中索引与FROM_UNIXTIME的问题的相关资料,需要的朋友可以参考下,希望能帮助到大家。

零、背景

简单收集一些信息后,发现这个慢查询问题隐藏的很深,问了好多人包括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不能利用上索引是右值与左值的类型不一致导致的。 。

相关推荐:

MySQL中两表关联的连接表如何创建索引图文详解

MySQL分区字段列有必要再单独建索引吗?

MySQL实现查看与创建以及删除索引的方法介绍

以上就是mysql中索引与FROM_UNIXTIME的问题详解的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

怎样手动打开mysql服务

mysql建表与索引使用规范详解

mysql怎么查询有哪些用户

mysql 聚合函数用法是什么

mysql数据库是如何实现xa规范的

mysql str如何转换 date

mysql 教程之列类型中的字符串型

mysql怎么建库?

mysql不严谨模式怎么开启?

mysql执行过程以及查询缓存的详细介绍

更多相关阅读请进入《UNIXTIME》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...