mysql - Mysql query takes too much time with Index
问题描述
My query takes too much time with index. How to create a good index for my Mysql query?
Query
SELECT `id`, `time`, `volume`, `candle` FROM `forex_history`
WHERE
period='30m' AND
id IN ('40','1817')
ORDER BY `time` DESC
LIMIT 600;
95 million rows in the table
Slow query log:
# Query_time: 3.801843 Lock_time: 0.000076 Rows_sent: 600 Rows_examined: 10966
# Rows_affected: 0 Bytes_sent: 49296
10% queries are slow. Query takes 0.2 Sec to 30 Sec
Table schema:
Execution plan:
解决方案
我可以建议以下索引:
CREATE INDEX idx ON forex_history (period, time, volume, candle);
-- add id if not using InnoDB
该索引将涵盖整个WHERE
子句以及SELECT
.
您可以尝试其他两个版本:
(period, id, time, volume, candle)
(period, id, time)
推荐阅读
- git - Visual Studio 2019 社区中的 Git,忽略缓存文件
- php - 提交表单后,AMP 表单不会重定向到thankyou
- sql - 预计 NUMBER 得到 DATE 的数据类型不一致
- reactjs - 表单提交后设置初始值formik react js
- python - sqlalchemy 登录 SQL Server 失败:错误 233 - 管道另一端没有进程
- python - 触发 Jupyter 笔记本 (x) 从另一个 (y) 运行,但将输出保存到 Python3 中的 x 笔记本中
- python - 用python嗅探收到的HTTP post数据包
- javascript - 如何检查公会中是否存在消息?(discord.js)
- mysql - Mysql获取每日数据的每周平均值
- python - 如何在 Locust 中执行负载测试之前实现用户注册批处理?