mysql - MySQL 上的 SQL 查询优化
问题描述
我有一个执行时间过长的 SQL 查询。我该如何优化它,以便它不会花费太多时间,大约需要 620 秒,这意味着 10 分钟。
| 190543 | root | localhost | ischolar | Query | 620 | Copying to tmp table
SELECT a.article_id, count(a.article_id) AS views
FROM timed_views_log a
INNER JOIN published_articles pa
ON (a.article_id = pa.article_id)
WHERE
a.date BETWEEN date_format(curdate() - interval 1 month,'%Y-%m-01 00:00:00') AND
date_format(last_day(curdate()-interval 1 month),'%Y-%m-%d 23:59:59')
GROUP BY a.article_id
ORDER BY
views desc
LIMIT 6, 5;
解决方案
如果您使用的是 SQL Server,则可以使用它建议的 sql server 查询执行计划和优化。参考文章 - https://www.sqlshack.com/using-the-sql-execution-plan-for-query-performance-tuning/
您的查询是与 where 子句的连接,因此大多数表中的数据本身很大,请尝试添加索引。
推荐阅读
- ruby-on-rails - 神秘类型错误:没有为类 OpenSSL::Digest 定义 _dump_data
- php - 从选择/输入框中获取输入。在 javascipt 中,然后在 php 中运行选择查询
- node.js - 如何将CSV但在字段中具有\r\n和“,”的字符串转换为json?
- reactjs - 页面上的简单路由。如何设置正确的显示?
- php - 如何在 PHP 中将 XML 转换为数组
- android - 如何使单选按钮的图像居中?
- excel - 将变量从用户窗体传递到模块
- git - OpenSSL 和原生 windows 安全通道库有什么区别
- sql-server - 实体框架和 JSON.NET 序列化
- spring - Spring Boot 中用于 Amazon Aurora Serverless 的数据 API