Sql-server – Need advice on Primary and Clustering Keys of a table

best practicesclustered-indexprimary-keysql-server-2008-r2

I need advise on a table which is part of a development project I'm involved.
A brief summary on the use of the table:

Data from multiple databases will be consolidated in this table for the purpose of full text searching. Full text index will be on a single column.

I'm trying to decide on the best candidate for the primary (and the clustering) key. There will be two important fields: ID and Type. ID is an identity field (which is also primary+clustering keys) on the source tables. Using just this field will result in a conflict because several source tables have the same ID (identity) field.
Type is a look-up table.
Id and Type together is unique.

I came up with the following scenarios:

  1. Id and Type as primary+clustering key.
  2. Create an identity field on the target table (primary+clustering) as well as a ID (synced with source's identity field) and Type.
  3. Create a separate Id field on the target table (clustering). Primary key will be source's ID and type.

What would be the best choice here?

Best Answer

First I'm assuming that both ID and Type are INT columns. Second that this is going to be a somewhat active table with a fair number of inserts (and possibly deletes). And last but not least that you aren't going to be pulling a "range" of IDs. For example you don't need to look at ID 10000-15000.

If so I would create a third column that is an INT identity column. Make the Primary/Clustered key on it. Then create a unique key on ID + Type. By making the new column the clustered key you will avoid any fragmentation problems caused by inserting into the middle of pages and speed up your inserts. The unique key ID+Type will be used for your queries and will be small enough (8 bytes) that you shouldn't see any issues there.

Last but not least since you are trying to put a full text index on the table you will need a single column, non-null, unique key preferably an integer. The new column will fit that bill admirably. (And of course your ID + Type combo wouldn't work.)