我有一个包含下表的数据库:
医院(~28K 行), 检查(~116K 行), 问题(~290K 行)
医院有检查,每次检查都有零个或多个问题。我有以下疑问:
SELECT count(*) as count, BName, BCity, BAddress, BState, BZip, Ins_date, BCountry, Ins_Type
FROM ( SELECT b.id as ID, b.hospital_name as BName, b.city as BCity, b.address as BAddress, b.country as BCountry, b.state as BState, b.zip as
BPostal, i.date as Ins_date, v.type as Ins_Type
FROM hospital_table b, inspection_table i, issue_table v
WHERE b.id = i.business_id
AND i.id = v.inspection_id
ORDER BY b.hospital_name, i.date DESC ) AS sumissues
GROUP BY ID
ORDER BY count DESC;
我期望并得到的输出是:
112 | Burnaby Memorial | Burnaby | 3935 Kincaid St | BC | 2017-07-19 | Canada | Cleanliness
问题是运行大约需要 40 秒。我有一个外键索引和inspection_table.date。关于如何优化它有什么想法吗?
最佳答案
查看您的代码
子选择中的 order by 不需要,也不需要子选择
SELECT
count(*) as count
, b.hospital_name as BName
, b.city as BCity
, b.address as BAddress
, b.country as BCountry
, b.state as BState
, b.zip as BPostal
, i.date as Ins_date
, v.type as Ins_Type
FROM hospital_table b
INNER JOIN inspection_table i ON b.id = i.business_id
INNER JOIN issue_table v ON i.id = v.inspection_id
GROUP BY b.id
ORDER BY count DESC, b.hospital_name, i.date
但要小心 @User_by_Already 的评论
对于这个和避免更多行组的其他列,如果你不关心这些列的结果,你可以使用(假)聚合函数
SELECT
count(*) as count
, b.hospital_name as BName
, b.city as BCity
, b.address as BAddress
, b.country as BCountry
, b.state as BState
, b.zip as BPostal
, min(i.date) as Ins_date
, min(v.type) as Ins_Type
FROM hospital_table b
INNER JOIN inspection_table i ON b.id = i.business_id
INNER JOIN issue_table v ON i.id = v.inspection_id
GROUP BY b.id
ORDER BY count DESC, b.hospital_name, i.date
关于MySQL 的 group by 和 count 查询非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46418379/