表:
User
UserId
UserRelation
UserRelationId | ManagerUserId (fk to User.UserId) | UserId
UserBusinessRelation
UserBusinessSaleId | BusinessId | UserId
Business
BusinessId
CampaignGroup
CampaignGroupId | BusinessId
Campaign
CampaignId | CampaignGroupId
Adgroup
AdgroupId | CampaignId
AdgroupKeyword
AdgroupKeywordId | AdgroupId
Click
ClickId | AdgroupKeywordId | Time (Unix Time, Double, Unsigned)
ClickAction
ClickActionId | ClickId | Time (unix time Double, Unsigned)
每个中的第一列是PrimaryKey,Auto-Increment
问题:如何以最有效的方式加入这些内容,充分了解每个企业和数百家企业可能轻松获得数十万次点击?
这是我的疑问:
select count(distinct(ca.ClickActionId)), count(distinct(cl.ClickId)) from ClickAction ca
inner join Click cl on cl.ClickId=ca.ClickId
inner join AdgroupKeyword ak on ak.AdgroupKeywordId=cl.AdgroupKeywordId
inner join Adgroup a on a.AdgroupId=ak.AdgroupId
inner join Campaign c on c.CampaignId=a.CampaignId
inner join CampaignGroup cg on cg.CampaignGroupId=c.CampaignGroupId
inner join Business b on b.BusinessId=cg.BusinessId
inner join UserBusinessSale ubs on ubs.BusinessId=b.BusinessId
inner join UserRelation ur on ur.UserId=ubs.UserId
where ur.ManagerUserId=3
问题在于我回来了:Array ( [0] => stdClass Object ( [count(distinct(ca.ClickActionId))] => 38290 [count(distinct(cl.ClickId))] => 38290 ) )
...我绝对知道这是不正确的,因为点击次数比clickActions多得多(可能至少多10倍)
答案 0 :(得分:0)
首先,关系似乎出现了问题 - 业务真的很必要吗? 看起来您正在通过两个外键加入UserBusinessRelation和CampaignGroup。 隔离计数 - 运行SQL两次,每次运行一次count语句,看看是否有任何不同。
此外,我同意Tony Hopkinson的意见 - 如果你试图计算尚未点击的点击动作,那么你需要那个外部联接。再看一下,你可以向后点击/ clickAction关系.. clickAction取决于是否存在点击。
如果,count(distinct列)独立运行,你可能最好一个接一个地运行两个查询,或者甚至在一个事务中运行,因为mySql已经缓存了计数所需表的索引。 / p>