SQL Server: Clutering by timestamp; pros/cons
Posted
by Ian Boyd
on Stack Overflow
See other posts from Stack Overflow
or by Ian Boyd
Published on 2010-04-21T17:20:33Z
Indexed on
2010/04/21
17:23 UTC
Read the original article
Hit count: 323
i have a table in SQL Server, where i want inserts to be added to the end of the table (as opposed to a clustering key that would cause them to be inserted in the middle). This means i want the table clustered by some column that will constantly increase.
This could be achieved by clustering on a datetime
column:
CREATE TABLE Things (
...
CreatedDate datetime DEFAULT getdate(),
[timestamp] timestamp,
CONSTRAINT [IX_Things] UNIQUE CLUSTERED (CreatedDate)
)
But i can't guaranteed that two Things
won't have the same time. So my requirements can't really be achieved by a datetime column.
i could add a dummy identity int
column, and cluster on that:
CREATE TABLE Things (
...
RowID int IDENTITY(1,1),
[timestamp] timestamp,
CONSTRAINT [IX_Things] UNIQUE CLUSTERED (RowID)
)
But you'll notice that my table already constains a timestamp
column; a column which is guaranteed to be a monotonically increasing. This is exactly the characteristic i want for a candidate cluster key.
So i cluster the table on the rowversion
(aka timestamp
) column:
CREATE TABLE Things (
...
[timestamp] timestamp,
CONSTRAINT [IX_Things] UNIQUE CLUSTERED (timestamp)
)
Rather than adding a dummy identity int
column (RowID) to ensure an order, i use what i already have.
What i'm looking for are thoughts of why this is a bad idea; and what other ideas are better.
Note: Community wiki, since the answers are subjective.
© Stack Overflow or respective owner