使用内部联接改进SQL查询

时间:2015-11-20 09:00:42

标签: sql sql-server database

我写了这个查询,它将计算按某些值分组的行数 我创建了一个将返回四列的存储过程。有时我收到“超时”错误消息。这是查询:

How tables are related How tables are related

  SELECT Orders_Header.ProjectNumber, Orders_Header.CostCategory,COUNT(Orders_Samples.SampleValue) AS AmountOfSamples, Activities.Area
    FROM  Orders_Header 
   INNER JOIN Orders_Sequence 
      ON Orders_Header.OrderID = Orders_Sequence.OrderID 
   INNER JOIN Orders_Samples 
      ON Orders_Header.OrderID = Orders_Samples.OrderID 
     AND Orders_Samples.ActivityNumber = Orders_Sequence.ActivityNumber 
   INNER JOIN Activities 
      ON Orders_Header.Plant = Activities.Plant 
     AND Orders_Sequence.ActivityName = Activities.ActivityName
   WHERE (Orders_Header.ClosingDate BETWEEN @StartDate AND @FinishDate) 
     AND (Orders_Header.Plant = @Plant) 
     AND (Orders_Sequence.ActivityName = @ActivityName) 
     AND (Area = @Area)
   GROUP BY Orders_Header.ProjectNumber, Orders_Header.CostCategory, Activities.Area

我想尽可能地改进这个查询,所以如果有人会给我一个例子,请解释这些例子。 (我是SQL的初学者)

我搜索了互联网,我有一个建议来寻找执行计划。

Excecution Plan Excecution Plan

在执行计划中,我收到此消息:“缺少索引详细信息”

USE [Orders]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[Orders_Sequence] ([ActivityName],[Rating])
INCLUDE ([OrderID],[ActivityNumber])
GO

0 个答案:

没有答案