gpt4 book ai didi

mysql - 使用日期函数优化 Group by 的 Mysql 查询

转载 作者:行者123 更新时间:2023-11-29 01:46:46 25 4
gpt4 key购买 nike

我有一个从汇总表中提取信息的报告,理想情况下会同时从两个时期提取信息,即当前时期和上一时期。我的表结构如下:

report_table
item_id INT(11)
amount Decimal(8,2)
day DATE

主键是item_id,天。该表当前包含 37k 条记录,其中包含 92 个不同的项目和 1200 个不同的日期。我正在使用 Mysql 5.1。

这是我的选择语句:

SELECT r.day, sum(r.amount)/(count(distinct r.item_id)*count(r.day)) AS `current_avg_day`, 
sum(r2.amount)/(count(distinct r2.item_id)*count(r2.day)) AS `previous_avg_day`
FROM `client_location_item` AS `cla`
INNER JOIN `client_location` AS `cl`
INNER JOIN `report_item_day` AS `r`
INNER JOIN `report_item_day` AS `r2`
WHERE (r.item_id = cla.item_id)
AND (cla.location_id = cl.location_id)
AND (r.day between from_unixtime(1293840000) and from_unixtime(1296518399))
AND (r2.day between from_unixtime(1291161600) and from_unixtime(1293839999))
AND (cl.location_code = 'LOCATION')
group by month(r.day);

目前这个查询在我的环境中需要 2.2 秒。解释计划是:

'1', 'SIMPLE', 'cl', 'ALL', 'PRIMARY', NULL, NULL, NULL, '33', 'Using where; Using temporary; Using filesort'
'1', 'SIMPLE', 'cla', 'ref', 'PRIMARY,location_id,location_id_idxfk', 'location_id', '4', 'cl.location_id', '1', 'Using index'
'1', 'SIMPLE', 'r', 'ref', 'PRIMARY', 'PRIMARY', '4', cla.asset_id', '211', 'Using where'
'1', 'SIMPLE', 'r2', 'ALL', NULL, NULL, NULL, NULL, '37602', 'Using where; Using join buffer'

如果我将索引添加到“day”列,我的查询不会运行得更快,而是在 2.4 秒内运行。当时查询的解释计划是:

'1', 'SIMPLE', 'r2', 'range', 'report_day_day_idx', 'report_day_day_idx', '3', NULL, '1092', 'Using where; Using temporary; Using filesort'
'1', 'SIMPLE', 'r', 'range', 'PRIMARY,report_day_day_idx', 'report_day_day_idx', '3', NULL, '1180', 'Using where; Using join buffer'
'1', 'SIMPLE', 'cla', 'eq_ref', 'PRIMARY,location_id,location_id_idxfk', 'PRIMARY', '4', 'r.asset_id', '1', 'Using where'
'1', 'SIMPLE', 'cl', 'eq_ref', 'PRIMARY', 'PRIMARY', '4', cla.location_id', '1', 'Using where'

根据 MySQL 文档,最有效的分组执行是在有索引检索分组列时执行的。但它也指出,唯一可以真正利用索引的函数是 min() 和 max()。有谁知道我可以做些什么来进一步优化我的查询?或者,尽管总行数少于非索引版本,但为什么我的“索引”版本运行速度更慢?

创建表:

CREATE TABLE `report_item_day` (
`item_id` int(11) NOT NULL,
`amount` decimal(8,2) DEFAULT NULL,
`day` date NOT NULL,
PRIMARY KEY (`item_id`,`day`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

当然,我的另一个选择是进行 2 次数据库调用,每个时间段调用一次。如果我这样做,每个查询的查询立即下降到 0.031 秒。我仍然觉得应该有一种方法来优化此查询以获得可比较的结果。

最佳答案

三件事:

1) 我在 WHERE 子句中没有看到 r2.item_id 的内容。没有它,r2 将通过笛卡尔积计算在内,并将对其他 item_id 求和。

将您的原始查询更改为如下所示:

SELECT r.day
,sum(r.amount)/(count(distinct r.item_id)*count(r.day)) AS `current_avg_day`
,sum(r2.amount)/(count(distinct r2.item_id)*count(r2.day)) AS `previous_avg_day`
FROM `client_location_item` AS `cla`
INNER JOIN `client_location` AS `cl`
INNER JOIN `report_item_day` AS `r`
INNER JOIN `report_item_day` AS `r2`
WHERE (r.item_id = cla.item_id) AND (r2.item_id = cla.item_id) AND (cla.location_id = cl.location_id)
AND (r.day between from_unixtime(1293840000) and from_unixtime(1296518399))
AND (r2.day between from_unixtime(1291161600) and from_unixtime(1293839999))
AND (cl.location_code = 'LOCATION')
group by month(r.day);

查看 EXPLAIN PLAN 在此之后是否发生变化。

2) 这样做:ALTER TABLE report_itme_day ADD INDEX (date,item_id);

这将索引扫描日期而不是项目 ID。

查看 EXPLAIN PLAN 在此之后是否发生变化。

3) 最后的手段:重构查询

SELECT r.day, sum(r.amount)/(count(distinct r.item_id)*count(r.day)) AS `current_avg_day`, sum(r2.amount)/(count(distinct r2.item_id)*count(r2.day)) AS `previous_avg_day` FROM
(SELECT CLA.item_id FROM client_location CL,client_location_item CLA WHERE CLA.location_code = 'LOCATION' AND CLA.location_id=CL.location_id) A,
report_item_day r,
report_item_day r2,
WHERE (r.item_id = A.item_id)
AND (r2.item_id = A.item_id)
AND (r.day between from_unixtime(1293840000) and from_unixtime(1296518399))
AND (r2.day between from_unixtime(1291161600) and from_unixtime(1293839999))
group by month(r.day);

这绝对可以进一步重构。我只是稍微重构了一下。

试一试!!!

关于mysql - 使用日期函数优化 Group by 的 Mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4973368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com