下面是我的查询示例。我最多只有大约10个这些连接/子查询都具有基本相同的格式,但具有不同的连接和where子句。
SELECT DISTINCT mytable.label, tableA.counter, tableB.counter
FROM mytable
LEFT JOIN
(SELECT COUNT(id) as counter, label
FROM mytable
...joins...
...where...
GROUP BY label) tableA
ON tableA.label=mytable.label
LEFT JOIN
(SELECT COUNT(id) as counter, label
FROM mytable
...joins...
...where...
GROUP BY label) tableB
ON tableB.label=mytable.label
...
这需要大约2-4秒,这是一个高流量的页面,所以这种速度不够好。任何人都可以推荐一种提高性能的方法吗?
答案 0 :(得分:2)
此处无需GROUP
,因为您只返回1个值。尝试这样的子查询方法:
SELECT DISTINCT T.label,
(SELECT COUNT(id) as counter FROM tableA A WHERE A.blah = T.blah) as AValue,
(SELECT COUNT(id) as counter FROM tableB B WHERE B.blah = T.blah) as BValue
FROM mytable T
答案 1 :(得分:0)