mysql中索引与FROM_UNIXTIME的问题详解
技术百科
小云云
发布时间:2018-01-17
浏览: 次 本文主要介绍mysql中索引与from_unixtime的问题的相关资料,需要的朋友可以参考下,希望能帮助到大家。
零、背景
简单收集一些信息后,发现这个慢查询问题隐藏的很深,问了好多人包括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(*) FROM 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都是精心优化了的,怎么是慢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.
返回的是一个时间类型,那强制转化为字符串类型呢?
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实现查看与创建以及删除索引的方法介绍
# 的是
# 都是
# 有一个
# 转化为
# 都不
# String
# if
# 字符串
# 返回值
# 点了
# for
# mysql
# sql
# 很深
# table
# Length
# date
# 我一
# 字符串类型
# 问了
# column
# number
# dba
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- 如何在Golang中优化文件读写性能_使用缓冲和并
- Win10怎么关闭自动更新错误弹窗_Win10策略
- MAC怎么解压RAR格式文件_MAC第三方解压工具
- VSC里PHP变量未定义报错怎么解决_错误抑制技巧
- Win11怎么更改默认打开方式_Win11关联文件
- Win11怎么开启窗口对齐助手_Windows11
- Win10如何卸载微软拼音输入法 Win10只保留
- 如何在Golang中指定模块版本_使用go.mod
- php下载安装后memory_limit怎么设置_
- Win11怎么更改文件夹图标_自定义Win11文件
- Python代码测试策略_质量保障解析【教程】
- 一文教你快速开通网站LOGO图
- 怎么将XML数据可视化 D3.js加载XML
- Mac如何整理桌面文件_Mac使用堆栈功能一键整理
- 如何使用Golang实现文件追加操作_向已有文件追
- Windows服务持续崩溃怎样修复_系统服务保护机
- Win11鼠标灵敏度怎么调 Win11鼠标指针移动
- Python与Docker容器化部署实战_镜像构建
- 如何使用Golang反射创建map对象_动态生成键
- Python爬虫项目实战教程_Scrapy抓取与存
- 微信里的php文件怎么变mp4_微信接收php转m
- 如何用正则表达式精确匹配“start”到“end”
- 如何用正则与预处理结合精准拦截拼接式垃圾域名
- Win11怎么设置默认PDF阅读器 Win11修改
- 如何在JavaScript中动态拼接PHP的bas
- Golang如何遍历目录文件_Golang fil
- 如何在Golang中使用log包输出不同级别日志_
- 如何使用Golang实现路由分组管理_Golang
- php485返回数据不完整怎么办_php485数据
- Win11怎么关闭自动调节亮度_Windows11
- Windows如何使用注册表查找和删除项?(reg
- Win11怎么设置多显示器任务栏 Win11扩展任
- mac怎么安装adb_MAC配置Android A
- 一文详解网站被黑客入侵挂马解决办法
- Windows10电脑怎么设置虚拟内存_Win10
- VSC怎样在Linux运行PHP_Ubuntu系统
- Win11关机快捷键是什么_Win11快速关机方法
- Win11怎么关闭开机声音_Win11系统启动提示
- Win11搜索栏无法输入_解决Win11开始菜单搜
- 如何在 Go 中可靠地测试含 time.Time
- Windows10如何删除Windows.old_
- Win11怎么关闭用户账户控制UAC_Window
- Python性能剖析高级教程_cProfileLi
- Laravel 查询 JSON 列:高效筛选包含数
- php怎么下载安装后设置错误日志_phpini l
- 如何在 ACF 中正确更新嵌套多层的 Group
- 如何在 Go 同包不同文件中正确引用结构体
- PhpStorm怎么调试PHP代码_PhpStor
- Python路径拼接规范_跨平台处理说明【指导】
- 如何快速验证Golang安装是否成功_运行go v

QQ客服