<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://interactiveasp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Database Naming Conventions</title><link>http://interactiveasp.net/blogs/natesstuff/archive/2008/07/02/database-naming-conventions.aspx</link><description>Naming conventions are like arm pits. Everyone has them and they all stink! Well, at least that's the perspective of pretty much every developer an DBA alike. I will present my own personal philosophy for naming conventions on databases and hopefully</description><dc:language>en</dc:language><generator>CommunityServer 2008 (Build: 30417.1769)</generator><item><title>re: Database Naming Conventions</title><link>http://interactiveasp.net/blogs/natesstuff/archive/2008/07/02/database-naming-conventions.aspx#6666</link><pubDate>Wed, 21 Jul 2010 22:47:38 GMT</pubDate><guid isPermaLink="false">b80005ef-4071-4968-b08e-765d7d71b33e:6666</guid><dc:creator>Nathan Zaugg</dc:creator><description>&lt;p&gt;Here is the 4-1-1 on why it&amp;#39;s important to use identity columns over &amp;quot;natural&amp;quot; keys. &amp;nbsp;Listed in order of importance.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; * It is important for the health of a database to insert data into the clustered index in an ascending order. &amp;nbsp;Background: &amp;nbsp;There are two types of indexes called a Clustered Index and a Non-Clustered index. &amp;nbsp;A clustered index is the physical ordering of the rows in the pages on the database. &amp;nbsp;A non-clusterd index references the clustered index to make it&amp;#39;s own lists of the order of the rows, but it just keeps a reference to the clustered index. &amp;nbsp;You may only have one clustered index because you can only have one physical ordering of data. &amp;nbsp;Okay, so now to the question &amp;quot;Why is it bad to insert rows out of order&amp;quot;. &amp;nbsp;The answer is that because the clustered index is physically ordered, if you have a row that goes in the middle of other rows, those other rows need to be shifted down. &amp;nbsp;Actually it doesn&amp;#39;t bother moving the data down, it performs something called a page tear. &amp;nbsp;Because all rows are stored on 8K pages any rows behind the row that is inserted are simply copied to a new page. &amp;nbsp;This leaves both pages half empty and means your database has to perform twice as many requests to the disk. &amp;nbsp;This is a big deal! &amp;nbsp;The performance will be acceptable in development (you may never notice) but in production it will cause nothing but problems.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; * There really is no such thing as a natural key - there would be very few real-life examples of where the data is guaranteed to be unique (an imperative for being a primary key). It then takes very little time for an application to want to insert a duplicate record (violating the key) because the key was poorly selected. In fact, because of these problems, I very frequently see tables with almost ALL columns in the composite key because it takes nearly all columns to make the row unique. &amp;nbsp;So what&amp;#39;s the point of a key at all?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; * Natural keys typically include more than a single value (see reason above). &amp;nbsp;This makes it difficult to make relationships to other tables because each column that constitutes the key will have to be present on the child table to make that relationship.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; * When you join two tables together, you typically join the tables on as key. &amp;nbsp;For the parent table it will probably be a primary key and for the child table it will be a foreign key (non-clustered index). &amp;nbsp;There are many ways the DBMS may choose to join the tables but typically it will be able to use the clustered index of the parent table and join the parent to the child. &amp;nbsp;This will use a clustered-index scan which is the fastest. &amp;nbsp;If both keys are non-clustered-indexes (because you are trying to avoid the page tearing on insert) the result is a hash-join or even a full table scan which are much slower! Also your join statements in SQL are going to get really long and ugly.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; * If your primary key is missing or is an non-clustered index, a key is generated for you anyway. &amp;nbsp;This is so it can distinguish between two different rows in the heap that have the same values and it really *needs* that row internally. &amp;nbsp;For example, if you add a non-clustered index what would it point to? &amp;nbsp;This also means that you will have two non-clustered indexes to deal with when joining tables together which hurts join performance (see point above)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; * The surrogate key gains meaning in the application and is often used as the predicate in SQL queries.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; * The table schema is more difficult to change. For example, you cannot move a field out of one table and into another when it is part of the natural key. &amp;nbsp;The integrity of the entire table is put at risk.&lt;/p&gt;
&lt;p&gt;Having said that, you can have a primary key that is a non-clustered index and you can opt for a different column to be your clustered index. &amp;nbsp;This is a very a-typical configuration and can be helpful. &amp;nbsp;Remember, clustered index seeks/scans are far faster than using a non-clustered index. &amp;nbsp;If you constantly query on a field or set of fields that can be unique, you would see a big performance boost in those queries. &amp;nbsp;Pretty much everything else you do with that table will suffer though and you still have the problem of getting it in there in order or keeping the database defragmented. &amp;nbsp;Another potential gain is that the natural order of the rows will be the same as the clustered index. &amp;nbsp;There are situations where you would want to take advantage of these side-effects but only an experienced DBA could make a good judgment call there.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://interactiveasp.net/aggbug.aspx?PostID=6666" width="1" height="1"&gt;</description></item></channel></rss>