2014-01-28 41 views
0

這是我的表的構造這個表,有一個最常用的查詢,如何建立良好的索引來提高速度

CREATE TABLE [dbo].[InterfaceMeraAdReport_CAV](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [reportID] [nvarchar](100) NOT NULL, 
    [FromTime] [nvarchar](100) NOT NULL, 
    [ToTime] [nvarchar](100) NOT NULL, 
    [Customer] [nvarchar](100) NOT NULL, 
    [Area] [nvarchar](100) NOT NULL, 
    [Vendor] [nvarchar](100) NOT NULL, 
    [SuccessCalls] [nvarchar](100) NOT NULL, 
    [TotalCalls] [nvarchar](100) NOT NULL, 
    [TotalMins] [nvarchar](100) NOT NULL, 
    [ASR] [nvarchar](100) NOT NULL, 
    [ACD] [nvarchar](100) NOT NULL, 
    [Fee] [nvarchar](100) NOT NULL, 
    [Cost] [nvarchar](100) NOT NULL, 
    [Profix] [nvarchar](100) NOT NULL, 
CONSTRAINT [PK_MeraAdvReport_CA] PRIMARY KEY CLUSTERED 
(
    [ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

和兩個主要用於查詢這種形式: 一個

select Customer,vendor,SUM(cast(SuccessCalls as int)) as successCalls, 
SUM(cast(TotalCalls as int)) as TotalCalls, 
SUM(cast(TotalMins as decimal(18,2))) as TotalMins, 
case when SUM(cast(TotalCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(SuccessCalls as decimal(18,2)))/SUM(cast(TotalCalls as decimal(18,2)))) end as ASR, 
case when SUM(cast(SuccessCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(TotalMins as decimal(18,2)))/SUM(cast(SuccessCalls as decimal(18,2)))) end as ACD, 
SUM(cast(Profix as decimal(18,2))) as profix 
from InterfaceMeraAdReport_CAV 
where FromTime >= '20140128020000' and ToTime <= '20140128030000' 
and Customer= '01.2136' and Area in ('62817','62818','62819','62859','62877','62878','62879') 
group by Customer,Vendor 
select Customer,SUM(cast(SuccessCalls as int)) as successCalls, 
SUM(cast(TotalCalls as int)) as TotalCalls, 
SUM(cast(TotalMins as decimal(18,2))) as TotalMins, 
case when SUM(cast(TotalCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(SuccessCalls as decimal(18,2)))/SUM(cast(TotalCalls as decimal(18,2)))) end as ASR, 
case when SUM(cast(SuccessCalls as decimal(18,2))) = 0 then 0.0 else (SUM(cast(TotalMins as decimal(18,2)))/SUM(cast(SuccessCalls as decimal(18,2)))) end as ACD 
from InterfaceMeraAdReport_CAV 
where FromTime >= @timeFrom and ToTime <= @timeTo 
and Customer= @customer and Area in ('62817','62818','62819','62859','62877','62878','62879') 
group by Customer 

此表增加了30多萬記錄的每一天,所以現在上面的查詢是來越來越慢,

我想創建索引來提高速度,但我不知道最有效的方式,有誰能夠告訴我哪些列將是最好的索引列

回答

0

根據您的數據,您可能會發現FromTime > 20140128020000,尤其是ToTime < 20140128030000可能沒有足夠的選擇性來保證僅在這些列中的任何一個上有索引。

因此,這使您與CustomerArea - 再次您需要了解您的表中的數據分佈這些列。

Customer假設高選擇性,並假設客戶是高度相關的區域(即客戶通常只從一個區域),然後嘗試讓你的指標儘可能窄,例如:

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(Customer) 

如果客戶是不相關的區域,或者如果客戶本身不是選擇性的:

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(Customer, Area) 

或者,如果面積比客戶更好的選擇性:

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(Area, Customer) 

或者,如果事實證明,該報告是運行只有最近的數據,還有大量遺留數據,然後FromTime可能是選擇:

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(FromTime, Customer) 

等即你需要知道你的數據,以便對索引做出決定。

由於您僅提取select中的6列,因此包含覆蓋索引也是有意義的。

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(Customer, Area) 
INCLUDE (Vendor, SuccessCalls, TotalCalls, TotalMins, Profix); 

另外,如果這兩個查詢是只有顯著查詢根據此表,那麼你也應該考慮根據這些查詢,例如選擇CLUSTERED指數優化最少的頁面讀取。 Customer,Area和可能的FromTime也可能是最適合的。

(很顯然,如果您將此設爲聚簇索引,則不需要覆蓋索引)。

相關問題