我有 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
我在销售和访问表中有几千条记录,没什么大不了的,但我的查询需要很长时间。它永远不会回来。知道为什么吗?
数据库:postgresql