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)
 Max(MyDateCol) index Asc or Desc?

Author  Topic 

Kristen
Test

22859 Posts

Posted - 2006-12-05 : 09:52:32
If I want to do

SELECT MAX(MyDateCol)

and I create an index on MyDateCol to help the process, should the index be Ascending or Descending - or doesn't it matter?

Kristen

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-05 : 09:57:46
Depending on the amount of records, DESCENDING might be a better choice because the MAX(MyDateCol) will be found on first page in first extent.
I imagine that creating an ASCENDING leads to some reading to get last extent and last page.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-12-05 : 10:24:14
That was my thinking too ...

There are 5.5M rows in the table, I'll give it a go. Not sure how I can time it though?

SET STATISTICS IO ON; SET STATISTICS TIME ON

shows

Table 'MyTable'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0.

same values if I use a Descending index

Kristen
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-12-05 : 13:36:26
OK, supplemental question: what's the benefit of using ASC / DESC in an index? (Given that presumably SQL Server can "walk" the index in either direction)

Kristen
Go to Top of Page
   

- Advertisement -