Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Including additional columns in a unique clustered index


Including additional columns in a unique clustered index

Author
Message
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3956 Visits: 6693
Uniqueness should not be the primary consideration for a clus key. Uniqueness should be a factor, but it's not the most important factor. The proper clus key depends still depends more on how the table is used.

For example, for a typical log table, there will be a datetime logged. It is not unique, since several INSERTs can easily occur within the 3 ms between datetime intervals.

Lookup is 100% by datetime for typical log uses: what happened with <x> activity between 11:30AM and 12:30PM yesterday.

Also, say you've chosen to add an identity column to the table; by definition, that column will be unique (whether constrained as such or not, under careful use).

Even so, the clus key should be the non-unique datetime, not the unique identity.

SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3956 Visits: 6693
GilaMonster (3/4/2013)
ScottPletcher (3/4/2013)
Again, rigid overly-simplistic rules such as those will give you a poorly performing database with excessive nonclustered indexes to cover all the queries the clus index can't because its keys were so poorly chosen.


So 'keep the keys for a unique index to the columns that need to be enforced unique' is a 'rigid overly-simplistic rule'?

Maybe you want to read what I said again, that for a unique index (one that's designed for enforce uniqueness) you should keep the keys to the columns that need to be unique, otherwise you have a constraint that isn't constraining anything in a useful way.

Oh, and since I've never once seen a system where all queries against a table query on a single column or set of columns, no matter how you select the clustered index you are going to need nonclustered indexes (not that I said anything about how to select the clustered index keys in this thread). Unless of course you have that idealistic and perfect system where each and every table is only ever filtered or joined on a single column or set of columns. Never seen one of those in reality though.

MS has engineers with vastly more expertise at scale than any of us.


True, the CAT team (Mark, Lubor, Cindy and the rest are awesome). Pity those skilled engineers didn't work on the replication tables. Or on Sharepoint. Or on several other examples of terrible database design that came out of MS.



You may or may not need nonclus indexes on a table. I've got plenty of tables with no nonclus indexes. With the proper clus index, one often doesn't need the mass of nonclus "covering indexes" that duplicate most of the original table and slow down all modifications to the base table.

The discussion was on the clustered index, not unique indexes in general. A clus index key needn't be unique on its own, and sometimes that's just fine. Again, it depends on the uses of that specific table.

General rules are nice, but they're not a substitute for carefully and properly selecting the specific clus index best for each table.

SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Robert Davis
Robert Davis
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1702 Visits: 1623
If your goal in indexing is to try to make the clustered index the index used by most of your queries, you are doing it backwards. Clustered indexes are larger. and your queries will perform better, in general, when they use smaller non-clustered indexes.

Also, let's suppose you base your clustering key on what certain queries are querying. Now fast forward a couple of years and you see that the queries hitting your system are completely different. Are you going to change your clustering keys? No, you may drop or add new nonclustered indexes but you don't change the clustering keys because a smart person doesn't base their clustering keys on the queries. The smart person bases it on the data. This is the general practice, there are always exceptions.

Poor database design and indexing schemes like you are advocating are the reasons why applications like SharePoint, SCOM, and BizTalk have wild requirements like MaxDOP = 1 ... to cover up for their horrendous design.



My blog: SQL Soldier
Twitter: @SQLSoldier
My book: Pro SQL Server 2008 Mirroring
Microsoft Certified Master, SQL Server MVP
Database Engineer at BlueMountain Capital Management
Paul Golabowski
Paul Golabowski
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 93
GilaMonster (3/4/2013)
So 'keep the keys for a unique index to the columns that need to be enforced unique' is a 'rigid overly-simplistic rule'?


Bear in mind that this is for an indexed view. Uniqueness is already enforced on the table. I suppose the clustered index on the view does not necessarily need to be unique. Does that paint a slightly different picture for you?
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47427 Visits: 44405
ScottPletcher (3/4/2013)
The discussion was on the clustered index, not unique indexes in general. A clus index key needn't be unique on its own, and sometimes that's just fine. Again, it depends on the uses of that specific table.


The OP's question was about the unique clustered index created on an indexed view. That does have to be unique. From Books Online:

Creating Indexed Views
The first index created on a view must be a unique clustered index. After the unique clustered index has been created, you can create additional nonclustered indexes. The naming conventions for indexes on views are the same as for indexes on tables. The only difference is that the table name is replaced with a view name. For more information, see CREATE INDEX (Transact-SQL).



Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47427 Visits: 44405
dembones79 (3/4/2013)
GilaMonster (3/4/2013)
So 'keep the keys for a unique index to the columns that need to be enforced unique' is a 'rigid overly-simplistic rule'?


Bear in mind that this is for an indexed view. Uniqueness is already enforced on the table. I suppose the clustered index on the view does not necessarily need to be unique. Does that paint a slightly different picture for you?


The clustered index on a view does have to be unique, as per the extract from BoL that I just referenced, so leaving it not unique is not an option here.

I personally hold to the indexing strategy taught by people like Paul Randal (ex storage engine team developer in case you didn't know), keep the clustered index as narrow as possible because of the impact on nonclustered indexes and on the depth of the clustered index itself. Less important on a view where you probably aren't adding other nonclustered indexes, so in this case, without seeing exec plans or data, I might create the clustered index on region and district and leave unit out entirely. Would have to see queries, table designs and some data to tell for sure.

Just one other point, you said 'a distinct list of...'. Distinct isn't allowed in an indexed view either, so you might have to look at alternatives here, maybe a group by without aggregation (max, min, avg also aren't allowed). Would need more info to tell for sure.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search