Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
bill_
Starting Member
38 Posts |
Posted - 2010-05-28 : 09:20:54
|
| A table has a a column for clinic and another for visitdate.There is an index for visitdate.visdate clinic----------------12/31/2000 4101/01/2001 0201/01/2001 4401/01/2001 0301/01/2001 0201/02/2001 08goes on and on like this for over a million records.I need to query for visdates in a date range for one clinic each time.My questions are:1) If an additional index was madewith both visdate and clinic as index key columns,would queries speed up ?2) If an additional index was madewith only clinic as a index key column,would queries speed up ?3)If the answers to 1) and 2) are "yes", which is faster ? |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-05-28 : 09:58:53
|
| If your query is something likeselect visdate,clinic from myTablewhere visdate >= aDate and visdate <= laterDateand clinic = '03'an index on those two columns (called a covering index) could really help. But if you're not really limiting your results by clinic or visdate, SQL might just say "You know, it'd be faster to just scan the table than use the index". The only way to know for sure is by looking at the execution plan and testing.JimEveryday I learn something that somebody else already knew |
 |
|
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-05-30 : 01:23:39
|
| 1) yes2) yes3) 1 is fasterThat is for specific query as jimf provides. With other queries, it may be different.Also, when you do covering index (or composite index), make the column that has less repeated data first. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-05-30 : 02:05:44
|
I doubt (2) will be any better. Clinic is probably not "selective" - i.e. you have only a relatively few distinct values for clinic, repeated many times, and as such SQL would not use the index.Presumably also there are NOT many rows for each given date? Thus an index by VisDate, Clinic would be most selective, and cover the queryBut the only way to know, for sure, with your data is to test it by by checking the query plan and Logical I/O before/after creating the index. |
 |
|
|
|
|
|
|
|