简体   繁体   中英

To this table , there is a mostly used query , How to create good index to improve speed

this is my table 's construct

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]

and two mostly used query is this form : one

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

two

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

this table increase about 300000 records every day , so now above query become more and more slower ,

i want to create index to improve speed , but i do not know the most effective way , can anybody tell me which columns will be the best index column

Depending on your data, you might find that FromTime > 20140128020000 and especially ToTime < 20140128030000 might NOT be selective enough to warrant an index on either of these columns alone.

So this leaves you with Customer and Area - again you would need to understand the distribution of data in your table for these columns.

Assuming high selectivity on Customer , and assuming that Customer IS highly correlated to Area (ie Customer is typically only from one area), then try keep your index as narrow as possible, eg:

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(Customer)

If Customer is NOT correlated to area, or if Customer by itself is not selective:

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(Customer, Area)

Or if Area has better selectivity than customer:

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(Area, Customer)

Or if it turns out that the report is run for recent data only and there is a large amount of legacy data, then FromTime could be selective:

CREATE INDEX IX_MyIndex ON InterfaceMeraAdReport_CAV(FromTime, Customer)

etc. ie You will need to know your data in order to make decisions on indexes.

Since you fetch only 6 of the columns in the select, it may also make sense to include a covering index.

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

Alternatively, if these 2 queries are the only significant queries against this table, then you should also consider choosing the CLUSTERED index based on these queries, eg optimize for fewest page reads. Customer , Area and possibly FromTime again will probably be most suited.

(Obviously you won't need a covering index if this if you make this the clustered index).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM