如何避免额外的内部联接来优化此查询?

时间:2013-03-25 08:04:31

标签: sql sql-server sql-server-2008 sql-server-2008-r2 sql-execution-plan

有什么方法可以避免内部联接从客户表中获取客户名字和姓氏,我如何优化执行计划?

SELECT c1.firstname, c1.lastname, t.*
FROM customer c1
INNER JOIN
    (select c.Customerid ,count(si.CustomerID) as 'No Of Orders'
    from Customer c
    LEFT OUTER join SalesInvoice si ON si.CustomerID = c.CustomerID
    GROUP by c.CustomerID) as t
ON c1.CustomerID = t.Customerid

enter image description here

3 个答案:

答案 0 :(得分:2)

有两种方法可以做到这一点。 一种方法是将所有非聚合列包括在列表中,如下所示:

SELECT
    c.firstname,
    c.lastname,
    c.CustomerId,
    count(si.CustomerID) as 'No Of Orders'
FROM
    customer c
LEFT OUTER JOIN
    SalesInvoice si ON si.CustomerID = c.CustomerID
GROUP BY
    c.CustomerID,
    c.firstname,
    c.lastname

另一种方法是在选择列表中使用聚合函数(min或max应该这样做):

SELECT
    MIN(c.firstname) as firstname,
    MIN(c.lastname) as lastname,
    c.CustomerId,
    count(si.CustomerID) as 'No Of Orders'
FROM
    customer c
LEFT OUTER JOIN
    SalesInvoice si ON si.CustomerID = c.CustomerID
GROUP BY
    c.CustomerID

我认为第一个查询的性能可能更好。要提高整体性能,您需要创建一个包含或覆盖索引。

您可以查看以下链接,了解所包含的索引:

  1. Why use the INCLUDE clause when creating an index?
  2. http://msdn.microsoft.com/en-IN/library/ms190806.aspx

答案 1 :(得分:1)

使用OVER()子句

尝试此选项
SELECT c1.Customerid, c1.firstname, c1.lastname, 
       COUNT(si.CustomerID) OVER(PARTITION BY c1.CustomerID) AS 'No Of Orders'
FROM customer c1 LEFT JOIN SalesInvoice si ON si.CustomerID = c1.CustomerID

为了提高性能,您需要此索引

CREATE INDEX ix_Customerid_Customer ON Customer(Customerid) INCLUDE(firstname, lastname)

答案 2 :(得分:0)

尝试一下:

SELECT
    c.firstname,
    c.lastname,
    c.CustomerId,
    count(si.CustomerID) as 'No Of Orders'
FROM
    customer c
LEFT OUTER JOIN
    SalesInvoice si ON si.CustomerID = c.CustomerID
GROUP BY
    c.CustomerID,
    c.firstname,
    c.lastname