我正在尝试为每个客户计算子记录(地址).我有2个查询,我想知道它们是否相同:
SELECT a.AddressId, c.CustomerID, COUNT(*) AS NumDuplicates FROM Customers C INNER JOIN Addresses a ON c.AddressID = a.AddressID GROUP BY c.CustomerID, a.AddressId ORDER BY NumDuplicates DESC SELECT c.CustomerID, (SELECT COUNT(*) FROM Addresses a WHERE a.AddressID = c.AddressID) AS AddressCount FROM Customers c ORDER BY AddressCount desc
如果他们不是,那有什么区别?如果它们更有效率?
这两个查询是不同的,因为第一个查询只返回在地址表中至少有一个匹配的客户.第二个返回所有客户,即使那些没有匹配且具有AddressId
NULL的客户.
等效的第一个查询是:
SELECT c.CustomerID, COUNT(a.AddressId) AS NumDuplicates FROM Customers C LEFT JOIN Addresses a ON c.AddressID = a.AddressID GROUP BY c.CustomerID ORDER BY NumDuplicates DESC;
至于性能,你应该尝试一下.有两个原因可能更快.第二个避免了必须进行聚合,但确实有一个相关的子查询.但是,SQL Server有一些加速连接和聚合的技巧.我猜想相关的子查询版本更快,但我的数据和服务器可能是错的.