SQL Server 2005 indexes the same columns in a different order
I have a report that is taking a long time. I ran the Database Engine Tuning Advisor and one of the recommendations was to create 2 indexes. However, I noticed that the indexes were the same columns, but in different orders.
Here is a table:
---Locations---
| *LocationID |
| Code |
| ...more... |
| DivisionID |
| RegionID |
---------------
and the recommendation was to add these 2 indices
CREATE NONCLUSTERED INDEX [IX_Locations_Region_Loc_Div] ON [dbo].[Locations]
(
[RegionID] ASC,
[LocationID] ASC,
[DivisionID] ASC
)
INCLUDE ( [Code]) WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_Locations_Loc_Reg_Div] ON [dbo].[Locations]
(
[LocationID] ASC,
[RegionID] ASC,
[DivisionID] ASC
)
INCLUDE ( [Code]) WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
GO
Why does SQL Server have to have these 3 index columns in different orders when used for inner join only?
SELECT UserID, LocationID FROM [User] u INNER JOIN [Locations] l ON u.LocationID = l.LocationID
a source to share
I'm not sure about 2005, but I know that (at least some) previous versions of SQL Server could only get an index to use if the columns in the where clause were in the same order as the columns in the index column. I don't like it, but I proved it.
I just noticed that you are talking about joins - then I basically wrote my joins in a where clause instead of using ANSI syntax, so I guess it still applies.
a source to share