| Author |
Topic |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2011-12-21 : 09:56:25
|
| Hi,The table tblMain has several fields.A number of the fields are added to the primary key as follows:the following keys:CONSTRAINT [PK_tblMain] PRIMARY KEY CLUSTERED ( [MyVDate] ASC, [MyIID] ASC, [MyRSeq] ASC, [MyC] ASCThe following sql needs to be optimized.Select MAX(MyVDate) from tblMain where MyRSeq=0 and MyIID=22 and MyVDate < '2011-12-20'Do you think I should add indexes for MyVDate only?What do you think please? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-21 : 12:13:54
|
| what does current execution plan suggest?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2011-12-21 : 13:57:40
|
| If you are really looking for speed create an indexed view. |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-21 : 15:49:26
|
| The pk is OK for that query, but it's not the best. The key columns are in the wrong order to seek optimally. Try a nonclustered index on (MyRSeq, MyIID, MyVDate). That'll be the fastest possible for that query.p.s. A 4-column clustered index is likely not idea. Why are those 4 the cluster?--Gail ShawSQL Server MVP |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-12-21 : 16:21:13
|
| No, that's not what I mean. It will seek.The leading column is being used as an inequality. That means that only the inequality can be done via an index seek, the other two predicates have to be applied as secondary filters. With the nonclustered index I suggested (the date as the 3rd column), SQL can do both equality predicates and the inequality as a single seek operation.--Gail ShawSQL Server MVP |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|