Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clustered index dilemma - ID or sort?

I have a table with two very important fields:

id INT identity(1,1) PRIMARY KEY
identifiersortcode VARCHAR(900)

My app always sorts and pages search results in the UI based on identifiersortcode, but all table joins (and they are legion) are on the id field. (Aside: yes, the sort code really is that long. There's a strong BL reason.)

Also, due to O/RM use, most SELECT statements are going to pull almost every column.

Currently, the clustered index is on id, but I'm wondering if the TOP / ORDER BY portion of most queries would make identifiersortcode a more attractive option as the clustered key, even considering all of the table joins going on.

Inserts on the table and changes to the identifiersortcode are limited enough that changing my clustered index would be a problem for insert/update operations.

Trying to make the sort code's non-clustered index a covering index (using INCLUDE) is not a good option. There are a number of large columns, and some of them have a lot of update activity.

like image 781
richardtallent Avatar asked Jan 21 '23 18:01

richardtallent


2 Answers

Kimberly L. Tripp's criteria for a clustered index are that it be:

  • Unique
  • Narrow
  • Static
  • Ever Increasing

Based on that, I'd stick with your integer identity id column, which satisfies all of the above. Your identifiersortcode would fail most, if not all, of those requirements.

like image 88
Joe Stefanelli Avatar answered Jan 30 '23 15:01

Joe Stefanelli


To correctly determine which field will benefit most from the clustered index, you need to do some homework. The first thing that you should consider is the selectivity of your joins. If your execution plans filter rows from this table FIRST, then join on the other tables, then you are not really benefiting from having the clustered index on the primary key, and it makes more sense to have it on the sort key.

If however, your joins are selective on other tables (they are filtered, then an index seek is performed to select rows from this table), then you need to compare the performance of the change manually versus the status quo.

like image 41
Chris Shain Avatar answered Jan 30 '23 15:01

Chris Shain