Clustered Primary key index on INT column or on a date column

  • Hi All,

    I currently have a dilemma, its to do with a table and also its current schema. At the moment, it has a nonclustered unique index on the ClientMatchID column, as this is more like the identifier column on the table, this column is often used within joins with other table.

    CREATE TABLE [dbo].[ClientMatchPort](

    [ClientMatchID] [int] NOT NULL,

    [CodeVendorID] [int] NOT NULL,

    [StockID] [int] NOT NULL,

    [ClientMatchValue] [varchar](50) NOT NULL,

    [LastRevised] [datetime] NULL,

    [ActivationDate] [datetime] NULL,

    [TrunkID] [bigint] NULL

    )

    On the other hand, the LastRevised column is often used as a where clause after joining the ClientMatchID column with other columns from other tables and inline views, there is no specific order to the joins.

    My proposal is to make the ClientMatchID the Primary key and also clustered and create a nonclustered index on the LastRevised column. On speaking to a mate, his argument is that there are a lot of cases where there exists a WHERE clause looking at the LastRevised column.

    My instinct tells me that best practices dictate that clustered indexes are not best placed on a date column.

    Any thoughts ?

  • Dean Jones (6/10/2009)


    Hi All,

    I currently have a dilemma, its to do with a table and also its current schema. At the moment, it has a nonclustered unique index on the ClientMatchID column, as this is more like the identifier column on the table, this column is often used within joins with other table.

    CREATE TABLE [dbo].[ClientMatchPort](

    [ClientMatchID] [int] NOT NULL,

    [CodeVendorID] [int] NOT NULL,

    [StockID] [int] NOT NULL,

    [ClientMatchValue] [varchar](50) NOT NULL,

    [LastRevised] [datetime] NULL,

    [ActivationDate] [datetime] NULL,

    [TrunkID] [bigint] NULL

    )

    On the other hand, the LastRevised column is often used as a where clause after joining the ClientMatchID column with other columns from other tables and inline views, there is no specific order to the joins.

    My proposal is to make the ClientMatchID the Primary key and also clustered and create a nonclustered index on the LastRevised column. On speaking to a mate, his argument is that there are a lot of cases where there exists a WHERE clause looking at the LastRevised column.

    My instinct tells me that best practices dictate that clustered indexes are not best placed on a date column.

    Any thoughts ?

    If your date queries are range based, then the clustered index on the date column may be worthwhile. Remember, the clustered index is the order in which the data is physically stored on disk.

  • The column which is used the most for RANGE SELECTION (between, >, <) should be chosen for clustered index.

    Then all records from the selection will be located consequently.

    _____________
    Code for TallyGenerator

  • Lynn Pettis (6/10/2009)


    If your date queries are range based, then the clustered index on the date column may be worthwhile. Remember, the clustered index is the order in which the data is physically stored on disk.

    Kind of. Data within each page is in order. Pages may not be in order, especially after page splits, but are treated logically as if they were.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • An important thing you need to consider here is the usage of "LastRevised" column. As the name indicates, I assume, it stores the last modified date of the record. In that case, if you have a clustered index on "LastRevised" column, the row has to physically move whenever you update the column. That will affect your update performance. So it all depends what performance (insert/update/select) is important to you.

  • Thanks for the reply, but doesnt thesame rule apply if the Clustered index was on an integer column ?

  • Sorry for the late reply.

    Yes. That's why it's better to avoid clustered index on a column which values would change constantly.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply