SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Index requirement for logical operations


Index requirement for logical operations

Author
Message
chandan_jha18
chandan_jha18
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4643 Visits: 2134
Please help me build the index for the query.The table has more than 3 million rows and whatever index combination I choose,always there is a index scan for the query.I am not able to convert it into a seek operation.this runs throughout the day and keeps my CPU spike up every 5 minutes

SELECT TOP 20 Primary, Secondary, [Year], DocType, [PageCount], ReturnId, IMAGEPATH
FROM [table] WHERE [Year] = 2009 AND
(Primary IN (123, 234) OR Secondary IN (241, 354))

Please suggest me an index for this.I beleive the 'OR' operator is causing the scan but is there a way to avoid it.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)

Group: General Forum Members
Points: 415072 Visits: 47140
You need two indexes because of the or

One index on Primary, one on Secondary. You may need to make both covering, depends on the rows involved. test and see

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Philip Turtle
Philip Turtle
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 22
If you paste the SQL into a query window in SSMS and press CTL-L you will see the estimated execution plan. SSMS may display a Missing Index suggestion and right-clicking the suggestion and choosing Missing Index details will show the SQL needed to create the index. Alternatively you could use the Index tuning tool available from the query menu.
chandan_jha18
chandan_jha18
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4643 Visits: 2134
Philip Turtle (3/3/2011)
If you paste the SQL into a query window in SSMS and press CTL-L you will see the estimated execution plan. SSMS may display a Missing Index suggestion and right-clicking the suggestion and choosing Missing Index details will show the SQL needed to create the index. Alternatively you could use the Index tuning tool available from the query menu.



Gail,
I created the two indexes.
One on primary and year including all the columns in select query
Second on secondary or year including all the columns in select query.

But there is still a table scan.I have good amount of memory so all the database stays in cache but the scan makes the cpu go up to 90 percent every time this query runs.Any more suggestions please.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)

Group: General Forum Members
Points: 415072 Visits: 47140
GilaMonster (3/3/2011)
You need two indexes because of the or

One index on Primary, one on Secondary. You may need to make both covering, depends on the rows involved. test and see


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


chandan_jha18
chandan_jha18
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4643 Visits: 2134
GilaMonster (3/4/2011)
GilaMonster (3/3/2011)
You need two indexes because of the or

One index on Primary, one on Secondary. You may need to make both covering, depends on the rows involved. test and see


CREATE NONCLUSTERED INDEX [primary] ON [dbo].[Finals]
(
[Primary] ASC
)
INCLUDE ( [ReturnId],
[Secondary],
[Year],
[DocType],
[PAGECOUNT],
[IMAGEPATH])


CREATE NONCLUSTERED INDEX [secondary] ON [dbo].[Finals]
(
[secondary] ASC
)
INCLUDE ( [ReturnId],
[primary],
[Year],
[DocType],
[PAGECOUNT],
[IMAGEPATH])

I created these two indexes but the query does a table scan with the first index to give me the output.I even updated the statistics for the table.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)

Group: General Forum Members
Points: 415072 Visits: 47140
Execution plan please

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


chandan_jha18
chandan_jha18
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4643 Visits: 2134
I have attached the execution plan for the query.I am posting my query and indexes which i created as per your specification.

My query:


SELECT TOP 20 Primary, Secondary, [Year], DocType, [PageCount], ReturnId, IMAGEPATH
FROM finals WHERE [Year] = 2009 AND
(Primary IN (123, 234) OR Secondary IN (241, 354))


Index-1

CREATE NONCLUSTERED INDEX [prim] ON [dbo].[Finals]
(
[Primary ] ASC
)
INCLUDE ( [ReturnId],
[Secondary ],
[Year],
[DocType],
[PAGECOUNT],
[IMAGEPATH])

Index-2

CREATE NONCLUSTERED INDEX [sec] ON [dbo].[Finals]
(
[Secondary ] ASC
)
INCLUDE ( [ReturnId],
[Primary ],
[Year],
[DocType],
[PAGECOUNT],
[IMAGEPATH])
GilaMonster
GilaMonster
SSC Guru
SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)SSC Guru (415K reputation)

Group: General Forum Members
Points: 415072 Visits: 47140
Move year from the include to the key. I didn't notice the filter on that earlier. That's for both indexes

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


chandan_jha18
chandan_jha18
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4643 Visits: 2134
GilaMonster (3/4/2011)
Move year from the include to the key. I didn't notice the filter on that earlier


I moved 'year' to the key column in both the indexes but still the same execution plan with non-clustered index scan
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search