0

在 SQL 操作方面,我并不太先进。我有来自横幅系统的以下 4 个表格来宣传我的博客:

banners 
-id
-other irrelevant data

clicks
-id
-bid (banner id) 
-other irrelevant data

exits (people who clicked on the banner and never used the blog or registered an account)
-id
-bid

postregistered (used the banner and register to the blog)
-id
-bid

现在我想生成一个简单的报告来向我展示每个横幅的total number of clicks,exitsreg's

我试过这个:

SELECT COUNT(c.id) as clicks, COUNT(e.id) as exits, COUNT(r.id) as reg
FROM banners b
LEFT JOIN clicks c
ON c.bid = b.id
LEFT JOIN exits e
ON e.bid = b.id
LEFT JOIN registered r
ON r.bid = b.id
GROUP BY b.name

但它只是让 mysql 杀死我的处理器并且永远不会完成,没有足够的数据让它成为一个繁重的查询。

请你能帮助我,对不起,如果这看起来很基本。

编辑:

我可以运行每个 Left join 自己的并获得正确的结果,但我宁愿将其作为单个查询运行

4

1 回答 1

3

您面临的问题是每个横幅的小笛卡尔积。简单来说,banner id 不足以链接表格。

假设您有 10 个横幅。每个横幅都有 100 次点击。在这些点击中,50 次是退出,50 次是注册。您可能认为您将从中得到大约 1000 行。

不,你得到了所有这些的乘积:10 * 100 * 50 * 50 = 2,500,000。这是很多处理。

您需要某种用户 ID 来跟踪用户从点击到退出和注册。或者,您可以预先汇总您的查询:

select b.bid, b.name, numclicks, numexits, numreg
from banner b left outer join
     (select bid, count(*) as numclicks
      from clicks
      group by bid
     ) c left outer join
     on c.bid = b.bid join
     (select bid, count(*) as numexits
      from exits
      group by bid
     ) e
     on e.bid = b.bid left outer join
     (select bid, count(*) as numreg
      from postregistered
      group by bid
     ) r
     on r.bid = b.bid
于 2013-05-29T13:12:10.370 回答