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

Primary key or not to primary key, that is the question Expand / Collapse
Author
Message
Posted Wednesday, March 20, 2013 8:43 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: 2 days ago @ 1:53 PM
Points: 35,366, Visits: 31,905
Eugene Elutin (3/19/2013)
But, when you have NIC installed, it makes it unsecured, as it's based on the MAC address of NIC and can be predicted.


Just to be clear for other that may be reading this, "normal" GUIDs no longer use the MAC address and are nothing more than random numbers. Sequential GUIDs do.

The current "normal" GUIDs used by SQL Server are "Type 4" GUIDs, which are nothing more than a very large random number, and even Microsoft says that although the chance of duplicating one is still incredibly small, there's no longer a guarantee that they're globally unique. Best put a unique index on it if it's a key column.

I agree with the "scramble" on the sort, though. There is a "block" sort order but they don't sort GUIDs from left to right. I've not played with sequential GUIDs but I imagine that because of the way they sort, they're gonna cause fragmentation no matter what.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1433263
Posted Wednesday, March 20, 2013 8:52 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:02 PM
Points: 20,734, Visits: 32,515
TheSQLGuru (3/20/2013)
tafountain (3/20/2013)
TheSQLGuru,

I did not intend to spark a debate :) but here are my responses.

TheSQLGuru (3/19/2013)
1) if you are worried with space, GUIDs have no place in your system.

This statement is a little unsubstantiated. GUIDs are fine, sure they're larger than INT but they fulfull requirements that an INT cannot. For example we need to maintain uniqueness across servers. A GUID does this for us. Yes, they have drawbacks but I would not totally exclude them as an option because of them.

TheSQLGuru (3/19/2013)
2) why aren't you using NEWSEQUENTIALID??

These do not provide uniqueness across tables, let alone across servers.

TheSQLGuru (3/19/2013)
3) Your PK on the parent does a clustered index on the NEWID, since you didn't specify one.

Is this a question or a statement? Not sure what you're trying to communicate.

TheSQLGuru (3/19/2013)
4) You are fragmenting the heck out of your nc indexes on those GUIDs.

Yes - this is very true. However most of the indexes are defragmented in under 30 seconds (most under 10 seconds) during our nightly maintenance.

TheSQLGuru (3/19/2013)

5) Did I mention that GUIDs SUCK yet?!? Oh, and for multiple systems, you can almost ALWAYS come up with a simple multi-part numeric arrangement that is guaranteed to be unique across all systems. I have used a tiny/smallint ServerID and integer identity construct at numerous clients to eliminate GUIDS they "HAD to have", always with GREAT effect.

Ok, now you have my interest :). I wouldn't mind understanding your implementation.



1) As I already said, you can (and I have several times) maintained uniqueness across multiple servers with an int identity and a separate ServerID field (tiny or small int) that is used as the compound PK for the table. Given that ServerA ALWAYS uses 1 for ServerID, ServerB ALWAYS uses 2 for ServerID, etc, you are GUARANTEED to NEVER get duplicate PK values with that 5 or 6 byte PK (instead of the SIXTEEN bytes of a GUID).

2) I believe you are wrong in stating that NEWSEQUENTIALID isn't UNIQUE. The only time it would not be is if there is no NIC on the server. Got many of those lying around?? I haven't seen one in the 20+ years I have been with databases, 15 of them as a SQL Server consultant. Here is the reference, which comes directly from SQL Server Books Online: http://msdn.microsoft.com/en-us/library/aa379322(VS.85).aspx

3) My "PK is clustered index" is a statement. If you simply define a PK on a table it is clustered by default unless you override that with explicit syntax to make it non-clustered.

4) It may not matter (much) on your system, but the extra bytes required for a GUID over other datatype(s) or compound keys REALLY make a difference on systems with any reasonable number of rows.


Regarding this:



3) My "PK is clustered index" is a statement. If you simply define a PK on a table it is clustered by default unless you override that with explicit syntax to make it non-clustered.



Or if you already have a clustered index defined on the table when you define a PK. The PK will be nonclustered if there is already a clustered index on a table.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1433274
Posted Wednesday, March 20, 2013 9:04 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 6:45 AM
Points: 104, Visits: 389
As far as the NEWSEQUENTIALID goes, SSCrazy hit most of the points I didn't elaborate on. We have multiple requirements and some of those include the application code generates the identifier before saving the record to the database. All in all, I agree from an implementation perspective an INT column defined with IDENTITY sounds more plausible but it, just like GUIDs, have thier own pro's and con's.
Post #1433285
Posted Wednesday, March 20, 2013 10:33 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 21, 2014 8:55 AM
Points: 2,873, Visits: 5,185
tafountain (3/20/2013)
As far as the NEWSEQUENTIALID goes, SSCrazy hit most of the points I didn't elaborate on. ...


Who are you calling SSCrazy, SSC Journeyman?

GUID's are evil! B-r-r-r-r-r!


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1433337
Posted Thursday, March 21, 2013 12:54 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, April 23, 2014 6:45 AM
Points: 104, Visits: 389
Eugene Elutin (3/20/2013)
tafountain (3/20/2013)
As far as the NEWSEQUENTIALID goes, SSCrazy hit most of the points I didn't elaborate on. ...


Who are you calling SSCrazy, SSC Journeyman?

GUID's are evil! B-r-r-r-r-r!


Oops, I copied his title and not his name lol - Eugene.
Post #1433982
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse