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)
 NonClustered Index

Author  Topic 

ramdas
Posting Yak Master

181 Posts

Posted - 2002-08-06 : 13:22:32
Hi folks,
I have a table called GainAndLoss. This table has a column called trade_date. I have a non-clustered index on this column. When I execute the query
SELECT recnum,quantity FROM GainAndLoss
WHERE trade_date >= '06/01/2002'
AND trade_date <= '06/30/2002'

The optimizer does not used the nonclustered index, instead a table scan is performed. Why does this happen? Does the optimizer skip the index because the data is not selectibe enough?

Bye
Ramdas

Ramdas Narayanan
SQL Server DBA

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-08-06 : 13:35:37
The optimizer has determined that the cost of the bookmark lookup (to get the recnum and quantity) outway the benefit of the non-clustered index seek. If you are having a performance problem with this query, you could consider a covering index.

When I do this...

create table gainandloss (
recnum int,
quantity int,
trade_date datetime )

create index idx_blah on gainandloss(trade_date)
go

select recnum,quantity
from gainandloss where trade_date >='06/01/2002' and trade_date <='06/30/2002'
go

 
...it does use the index and bookmark lookup. Granted, I have no data, so the statistics are worthless.


create index idx_cover on gainandloss(trade_date,recnum,quantity)
go
select recnum,quantity
from gainandloss where trade_date >='06/01/2002' and trade_date <='06/30/2002'
go

 
...That should cover the query though...

Jay White
{0}
Go to Top of Page

ramdas
Posting Yak Master

181 Posts

Posted - 2002-08-06 : 14:10:55
Thank you

Ramdas Narayanan
SQL Server DBA
Go to Top of Page

jasper_smith
SQL Server MVP &amp; SQLTeam MVY

846 Posts

Posted - 2002-08-06 : 15:18:30
How many rows in this range ?
How many rows in the table ?
How mant pages in the table ? (You can use DBCC SHOWCONTIG to get this)



HTH
Jasper Smith
Go to Top of Page
   

- Advertisement -