【发布时间】:2011-04-18 04:10:24
【问题描述】:
这个查询在我的慢查询日志中弹出:
SELECT
COUNT(*) AS ordersCount,
SUM(ItemsPrice + COALESCE(extrasPrice, 0.0)) AS totalValue,
SUM(ItemsPrice) AS totalValue,
SUM(std_delivery_charge) AS totalStdDeliveryCharge,
SUM(extra_delivery_charge) AS totalExtraDeliveryCharge,
this_.type AS y5_,
this_.transmissionMethod AS y6_,
this_.extra_delivery AS y7_
FROM orders this_
WHERE this_.deliveryDate BETWEEN '2010-01-01 00:00:00' AND '2010-09-01 00:00:00'
AND this_.status IN(1, 3, 2, 10, 4, 5, 11)
AND this_.senderShop_id = 10017
GROUP BY this_.type, this_.transmissionMethod, this_.extra_delivery
ORDER BY this_.deliveryDate DESC;
该表是 InnoDB,大约有 880k 行,执行时间在 9-12 秒之间。我尝试添加以下索引ALTER TABLE orders ADD INDEX _deliverydate_senderShopId_status ( deliveryDate , senderShop_id , status, type, transmissionMethod, extra_delivery);,但没有任何实际收获。欢迎任何帮助和/或建议
这是现在的查询执行计划:
id select_type 表类型 possible_keys key key_len ref rows 已过滤 Extra 1 SIMPLE this_ref FKC3DF62E57562BA6F 8 const 139894 100.00 使用where;使用临时的;使用文件排序我从文本中取出了 possible_keys 值,因为我认为它列出了表中的所有索引。使用的密钥(FKC3DF62E57562BA6F)看起来像
键名类型唯一压缩字段基数排序空注释 FKC3DF62E57562BA6F BTREE 否 否 senderShop_id 4671 A【问题讨论】:
-
deliveryDate的列类型是什么?
-
索引应该与查询计划的分析一起完成,以查看它们是如何被真正使用的。基于数据库完成的优化,数据库将如何使用索引并不总是很明显。使用此索引发布您的查询计划,然后我们将能够更好地提供帮助
-
@Thilo - deliveryDate 是日期时间
-
是否正在使用索引?应该是,但是您可以在 where 子句中尝试明确的 to_date 吗?另外,所有列都不是 NULL 吗?
-
@In Sane - 我编辑了问题并添加了查询执行计划
标签: mysql indexing performance