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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Clustered Index on Composite PK

Author  Topic 

maximus_vj
Yak Posting Veteran

88 Posts

Posted - 2005-05-25 : 18:12:35
Hello,

I have a table with two columns as a primary key. So by default a composite clustered index is created.

My Table schema is tbl_test(ID1 int (PK),ID2 int (PK), name varchar(50))
Index is created on (ID1,ID2).

Index is used for the following query:
select * from tbl_test where ID1 = 10
Index is NOT USED for the following query:
select * from tbl_test where ID2 = 10

If I change the order of the index, (ID2,ID1) then

Index is used for the following query:
select * from tbl_test where ID2 = 10
Index is NOT USED for the following query:
select * from tbl_test where ID1 = 10

What should I do so that index is used in both these queries?

Thanks
maximus



AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-05-25 : 19:27:37
You can't use the same index for both queries because indexes are only useful based on the field order they are specified in the CREATE INDEX statement. However, you could create a second (non-clustered) index on the second field so that an index is still used. It just won't be the clustered one.

---------------------------
EmeraldCityDomains.com
Go to Top of Page
   

- Advertisement -