几个月前我写了这个查询。它运行良好。但是,这种查询日复一日地执行得越来越慢。
除了相同的表执行之外,此查询还检查多个表中的帐单历史记录。
这是查询 -
SELECT * FROM (
SELECT *,
(SELECT username FROM users WHERE id = u_bills.UserId) AS username,
(SELECT first_name FROM users WHERE id = u_bills.UserId) AS first_name,
(SELECT last_name FROM users WHERE id = u_bills.UserId) AS last_name,
(SELECT phone FROM users WHERE id = u_bills.UserId) AS phone,
(SELECT email FROM users WHERE id = u_bills.UserId) AS email,
(SELECT CPRate FROM cpt WHERE UserId = u_bills.UserId ORDER BY AddedDate DESC LIMIT 0,1) AS cprate,
(SELECT (SELECT PopName FROM pops WHERE PopId = p.PopName) AS PopFullName FROM u_setupinfos AS p
WHERE UserId = u_bills.UserId) AS popname,
(SELECT active FROM users WHERE id = u_bills.UserId) AS active,
(SELECT SUM(PaidAmount) AS PaidAmount FROM u_billhistory
WHERE UserId = u_bills.UserId AND MONTH(AddedDate) = MONTH(PaidDate)) AS PaidAmount,
(SELECT PaidDate FROM u_billhistory
WHERE UserId = u_bills.UserId AND MONTH(AddedDate) = MONTH(PaidDate)
GROUP BY MONTH(PaidDate)) AS PaidDate,
(SELECT PaymentMedia FROM u_billhistory
WHERE UserId = u_bills.UserId AND MONTH(AddedDate) = MONTH(PaidDate)
GROUP BY MONTH(PaidDate)) AS PaymentMedia,
(SELECT TransactionId FROM u_billhistory
WHERE UserId = u_bills.UserId AND MONTH(AddedDate) = MONTH(PaidDate) GROUP BY MONTH(PaidDate)) AS TransactionId,
(SELECT GROUP_CONCAT(CONCAT(`BillHisId`,';',`PaidAmount`,';',`PaidDate`) separator '|') AS vals
FROM u_billhistory
WHERE UserId = u_bills.UserId
AND YEAR(PaidDate) = YEAR(CURDATE())) AS TotalPaids
FROM u_bills) AS m
WHERE m.username = 'abc'
请查看图片上的EXPLAIN
报告 -
我想组织这个查询。我需要建议使这个查询更快,如3到5秒,而不是1分半钟或更长。我在这个问题上犯了什么错误?
重要提示 -
答案 0 :(得分:5)
这很慢,因为你让它为结果中每条记录的每个字段单独查找一个附加表...有时不止一个。您应该使用JOIN而不是嵌套的SELECT。对于某些分组结果,您可以加入SELECT
语句,该语句执行自己的GROUP BY。
此外,各列上的单独索引请勿帮助!!
将索引想象成电话簿。一个简单的电话簿在姓氏和名字上使用聚集索引。然后它还列出了每个项目的地址和电话号码。这就是聚集索引:只是数据的存储顺序。
其他索引就像补充一样。 “电话号码”上的非聚集索引就像本书末尾的补充,按顺序列出电话号码,然后显示姓氏和名字(但不是地址)。 “地址”的索引也会这样做:一个单独的补充,按顺序列出地址,然后给你起名字。
使用这些索引补充,如果您有一个电话号码并想知道地址,您必须首先查找补充中的电话号码以查找名称,然后在主书中查找名称以获取地址。在此示例中,电话补充确实有帮助,因为它比电话簿的完整扫描更好。
但是,重要的是要注意地址补充_does 不帮助此搜索,即使地址是您想要找到的确切字段。 可能对其他搜索有用,但在这种情况下无效。此外,请记住,对您图书的每次更新都要求您不仅要更新图书,还要更新所有补充内容。它不仅仅是存储空间,还包括更新每个索引所花费的时间。你需要考虑它是否值得。如果你有一堆未使用的索引,没有它们你会更好。
回到你的具体问题,关于唯一真正对你有帮助的索引是u_bills.username
,user.UserId
(希望它的表是集群)和{{}的单个索引1}}和u_billhistory.UserId
。
以下是重构此查询的初步尝试:
u_billhistory.PaidDate
请注意,它并不完整,因为我们没有关于您的架构和数据的所有信息,并且因为似乎缺少SELECT ub.*, u.username, u.first_name, u.last_name, u.phone, u.email, u.active,
m.PaidAmount, m.PaymentMedia, m.TransactionId,
y.TotalPaids, p.PopName As PopFullName, cpt2.CPRate
FROM u_bills ub
INNER JOIN users u on u.id = ub.UserId
INNER JOIN u_setupinfos usi on usi.UserId = ub.UserID
INNER JOIN pops p ON p.PopId = usi.PopName
INNER JOIN (
SELECT UserId, MIN(AddedDate) As cptDate
FROM cpt
GROUP BY UserID
) cpt1 ON cp1.UserID = ub.UserID
INNER JOIN (
SELECT UserID, AddedDate, MIN(CPRate) AS CPRate
FROM cpt
GROUP BY UserID, AddedDate
) cpt2 ON cpt2.UserID = ub.UserID AND cpt2.AddedDate = cpt1.ctpDate
INNER JOIN (
SELECT UserID, MONTH(PaidDate) PaidMonth, SUM(PaidAmount) AS PaidAmount,
PaymentMedia, TransactionId --these two fields need an aggregate function of some type!
FROM u_billhistory
GROUP BY UserId, MONTH(PaidDate)
) m ON m.UserID = ub.UserId AND m.PaidMonth = MONTH(ub.AddedDate)
INNER JOIN (
SELECT UserID, YEAR(PaidDate) As PaidYear,
GROUP_CONCAT(CONCAT(`BillHisId`,';',`PaidAmount`,';',`PaidDate`) separator '|') AS TotalPaids
FROM u_billhistory
WHERE YEAR(PaidDate) = YEAR(CurDate())
GROUP BY UserId, YEAR(PaidDate)
) y ON Y.UserId = ub.UserId
WHERE username='abc'
和两个缺少的聚合函数。 MySql这种方式很糟糕......一个更好的数据库不会允许你的查询运行,直到你修复了这些东西。