HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

How does indexes on a date field work?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
fieldindexesdateworkdoeshow

Problem

I have a table with many dates, ten years into the future and ten years history. (combined with customer id and more)

Most of the reads and writes from/to the database is for records with the current date.

How would sql server use an sorted unique index on the date field? Will it start from the top or bottom and scan half the index before it find the current date?

Or is there someway it wil start in the midle of the index and scan up or down based on the frist value in the index?

Solution

SQL Server non-clustered indexes are sorted in ascending order by default, unless you specify that they must be in descending order, when you define them.

(Also worth noting that if a table is a clustered index, that clustered index will be included in all non-clustered indexes as well.)

When it uses that index, it will usually do a seek into the index at the correct spot, using the index's B-tree structure, otherwise it will scan the entire index if the index doesn't satisfy the query.

Context

StackExchange Database Administrators Q#211290, answer score: 2

Revisions (0)

No revisions yet.