1

我有 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

4

1 回答 1

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)

按 kd.id 分组

于 2012-07-27T01:07:49.937 回答