我有4张桌子: 域: :有很多=>渠道 :has_many =>销售 :has_many =>访问
我正在尝试获取此查询中每个域的所有域名,渠道数,销售次数和访问次数:
SELECT
kd.id, kd.domain,
COUNT(distinct kc.id) AS channels_count,
COUNT(distinct kv.id) AS visits_count,
COUNT(distinct kv.ip_address) AS visitors_count,
COUNT(distinct ks.id) AS sales_count
FROM
domains AS kd
LEFT JOIN
channels AS kc
ON(kc.domain_id=kd.id)
LEFT JOIN
sales AS ks
ON(ks.channel_id=kc.id)
LEFT JOIN
visits AS kv
ON(kv.channel_id=kc.id)
GROUP BY kd.id
我在销售和访问表中有几千条记录,没有什么大的,但我的查询需要永远。它永远不会回来。知道为什么吗?
db:postgresql
答案 0 :(得分:2)
COUNT(DISTINCT)可能是资源匮乏。此外,您的表可能在“kd.id”列上有许多重复项,导致生成数以万计的行。
更有效的方法是首先进行聚合,然后将结果连接在一起。我认为以下查询可以执行您想要的操作(我假设每个表中的id列都是唯一的):
SELECT kd.id, kd.domain, channels_count, visits_count,
visitors_count, sales_count
FROM domains kd LEFT JOIN
(select kc.domain_id, count(*) as channels_count
from channels kc
group by kc.domain_id
) kc
ON(kc.domain_id=kd.id) LEFT JOIN
(select ks.channel_id, count(*) as sales_count
from sales ks
group by ks.channel_id
) ks
ON(ks.channel_id=kc.id) LEFT JOIN
(select kv.channel_id, count(*) as visits_count,
count(distinct ipaddress) as visitors_count
from visits kv
on kv.channel_id
)
ON(kv.channel_id=kc.id)
GROUP BY kd.id