我有这个查询,它在两个表company
和question
. 这两个表的结构如下:
company | question
id | id
name | company_id
| sentiment
SELECT company.id as company_id, company.name, question.sentiment,
(SELECT count(*) FROM question
WHERE question.sentiment=0 AND question.company_id=company.id) AS count
FROM question
JOIN company ON company.id=question.company_id
WHERE question.sentiment = 0 GROUP BY company_id ORDER BY count DESC LIMIT 5
我一直在试图弄清楚如何重写它以避免子查询似乎会减慢查询执行速度。问题表有超过 1000 条记录。此外,使用索引已将执行时间从 545.037 秒减少到 180.228 秒,我仍然需要改进这一点。
谢谢