Reputation: 3257
I have a Material
table with two columns that are indexed.
CREATE NONCLUSTERED INDEX [MaterialName_PartType_idx] ON [dbo].[Material]
(
[MaterialName] ASC,
[PartType] ASC
)
CREATE NONCLUSTERED INDEX [PartType_idx] ON [dbo].[Material]
(
[PartType] ASC
)
When I query
SELECT PartType FROM Material
it uses Index Scan, but when I query
SELECT MaterialName FROM Material
it uses Table Scan.
SELECT MaterialName, PartType FROM Material
also uses Table Scan
Both PartType
and MaterialName
are of type VARCHAR(50)
.
Why would it not use Index Scan? I am using SQL Server 2000.
Upvotes: 0
Views: 99
Reputation: 40319
Stock answer: it depends.
How wide are the columns? How "full" are they? How many columns are in the table? How many rows?
If there are just the two columns, and if Material is a varchar(100) that is often populated with 25 or more characters, and if PartType is an integer, and if there are thousands and thousands of rows [note that that is 4 ifs] then:
order by
clause--you're just asking for all the data in whatever order is most convenient for SQL to return it in. If you order by
Material, odds are extremely good SQL will use that index.Upvotes: 2