Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Including additional columns in a unique clustered index Expand / Collapse
Author
Message
Posted Monday, March 4, 2013 5:05 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 2:20 PM
Points: 1,959, Visits: 2,893
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)
"In America, every man is innocent until proven broke!" Brant Parker
Post #1426526
Posted Monday, March 4, 2013 5:16 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 2:20 PM
Points: 1,959, Visits: 2,893
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)
"In America, every man is innocent until proven broke!" Brant Parker
Post #1426530
Posted Monday, March 4, 2013 5:17 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, July 17, 2014 1:37 PM
Points: 1,612, Visits: 1,536
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 2008
Principal DBA: Outerwall, Inc.
Also available for consulting: SQL DBA Master
Post #1426531
Posted Monday, March 4, 2013 5:25 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, March 4, 2013 5:19 PM
Points: 15, Visits: 86
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?
Post #1426534
Posted Monday, March 4, 2013 5:35 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 42,438, Visits: 35,493
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 2008, MVP
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

Post #1426537
Posted Monday, March 4, 2013 5:46 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 42,438, Visits: 35,493
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 2008, MVP
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

Post #1426540
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse