相关疑难解决方法(0)

哪个更有效,where 子句或与百万多行表的连接?

我们运行的网站在一个表中具有 250 MM 的行,而在另一个表中,我们将其连接到大多数查询中的行不到 15 MM。

示例结构:

MasterTable (Id, UserId, Created, Updated...) -- 15MM Rows
DetailsTable (Id, MasterId, SomeColumn...) -- 250MM Rows
UserTable (Id, Role, Created, UserName...) -- 12K Rows
Run Code Online (Sandbox Code Playgroud)

我们必须定期对所有这些表进行一些查询。一种是抓取免费用户(~10k 免费用户)的统计数据。

Select Count(1) from DetailsTable dt 
join MasterTable mt on mt.Id = dt.MasterId 
join UserTable ut on ut.Id = mt.UserId 
where ut.Role is null and mt.created between @date1 and @date2
Run Code Online (Sandbox Code Playgroud)

问题是这个查询有时会运行很长时间,因为连接发生在 where 之前很久。

在这种情况下,使用 wheres 而不是 joins 或可能更明智where column in(...)吗?

join best-practices

22
推荐指数
2
解决办法
11万
查看次数

标签 统计

best-practices ×1

join ×1