<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Back to Basics</title>
        <link>http://weblogs.sqlteam.com/mladenp/category/284.aspx</link>
        <description>Back to Basics</description>
        <language>en-US</language>
        <copyright>Mladen Prajdić</copyright>
        <managingEditor>spirit1_fe@yahoo.com</managingEditor>
        <generator>Subtext Version 1.9.4.0</generator>
        <item>
            <title>Back to Basics: Count, Count, Count, Sum or how to Count</title>
            <link>http://weblogs.sqlteam.com/mladenp/archive/2008/02/04/Back-to-Basics-Count-Count-Count-Sum-or-how-to.aspx</link>
            <description>&lt;p&gt;Probably everyone is familiar with the Count(*) function in SQL Server.&lt;/p&gt;
&lt;p&gt;But there seems to be a great deal of confusion amongst youngsters (SQL wise) about how all its possible options work.&lt;/p&gt;
&lt;p&gt;Let us banish the confusion back to the dark realms where in belongs to:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;DECLARE&lt;/span&gt; @t &lt;span class="kwrd"&gt;TABLE&lt;/span&gt; (val &lt;span class="kwrd"&gt;INT&lt;/span&gt;)
&lt;span class="kwrd"&gt;INSERT&lt;/span&gt; &lt;span class="kwrd"&gt;INTO&lt;/span&gt; @t
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 1 &lt;span class="kwrd"&gt;UNION&lt;/span&gt; &lt;span class="kwrd"&gt;ALL&lt;/span&gt;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 1 &lt;span class="kwrd"&gt;UNION&lt;/span&gt; &lt;span class="kwrd"&gt;ALL&lt;/span&gt;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt; &lt;span class="kwrd"&gt;UNION&lt;/span&gt; &lt;span class="kwrd"&gt;ALL&lt;/span&gt;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;NULL&lt;/span&gt; &lt;span class="kwrd"&gt;UNION&lt;/span&gt; &lt;span class="kwrd"&gt;ALL&lt;/span&gt;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 4 &lt;span class="kwrd"&gt;UNION&lt;/span&gt; &lt;span class="kwrd"&gt;ALL&lt;/span&gt;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 4 &lt;span class="kwrd"&gt;UNION&lt;/span&gt; &lt;span class="kwrd"&gt;ALL&lt;/span&gt;
&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; 5

&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;COUNT&lt;/span&gt;(*) &lt;span class="kwrd"&gt;AS&lt;/span&gt; CountAll, &lt;span class="rem"&gt;-- counts all rows&lt;/span&gt;
       &lt;span class="kwrd"&gt;COUNT&lt;/span&gt;(val) &lt;span class="kwrd"&gt;AS&lt;/span&gt; CountAllNoNull, &lt;span class="rem"&gt;-- counts rows that don't contain NULL&lt;/span&gt;
       &lt;span class="kwrd"&gt;COUNT&lt;/span&gt;(&lt;span class="kwrd"&gt;DISTINCT&lt;/span&gt; val) &lt;span class="kwrd"&gt;AS&lt;/span&gt; CountDistinctNoNulls, &lt;span class="rem"&gt;-- counts the number of distinct values&lt;/span&gt;
       &lt;span class="kwrd"&gt;COUNT&lt;/span&gt;(*) - &lt;span class="kwrd"&gt;COUNT&lt;/span&gt;(val) &lt;span class="kwrd"&gt;AS&lt;/span&gt; CountOfNullValues &lt;span class="rem"&gt;-- count of NULL values in the column &lt;/span&gt;
&lt;span class="kwrd"&gt;FROM&lt;/span&gt;   @t
&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;There are 3 ways in which Count() works:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;COUNT(*)&lt;/strong&gt; - Counts all rows in the table&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;COUNT(ColumnName)&lt;/strong&gt; - Counts all rows in the table that don't contain NULL in the specified column&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;COUNT(DISTINCT ColumnName) &lt;/strong&gt;- Counts all DISTINCT rows in the table that don't contain NULL in the specified column&lt;/p&gt;
&lt;p&gt;We can see that with the use of these 3 options we can get easily get the number of NULL values in the column. &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;So where does the Sum come in here? Sum can be used to get a count of practically anything with just one table scan.&lt;/p&gt;
&lt;p&gt;Let's illustrate:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;SUM&lt;/span&gt;(&lt;span class="kwrd"&gt;CASE&lt;/span&gt; &lt;span class="kwrd"&gt;WHEN&lt;/span&gt; val &amp;lt; 3 &lt;span class="kwrd"&gt;THEN&lt;/span&gt; 1 &lt;span class="kwrd"&gt;ELSE&lt;/span&gt; 0 &lt;span class="kwrd"&gt;END&lt;/span&gt;) &lt;span class="kwrd"&gt;AS&lt;/span&gt; LessThanTree,
       &lt;span class="kwrd"&gt;SUM&lt;/span&gt;(&lt;span class="kwrd"&gt;CASE&lt;/span&gt; &lt;span class="kwrd"&gt;WHEN&lt;/span&gt; val &amp;gt; 4 &lt;span class="kwrd"&gt;THEN&lt;/span&gt; 1 &lt;span class="kwrd"&gt;ELSE&lt;/span&gt; 0 &lt;span class="kwrd"&gt;END&lt;/span&gt;) &lt;span class="kwrd"&gt;AS&lt;/span&gt; MoreThanFour
       &lt;span class="rem"&gt;-- SUM(CASE WHEN &amp;lt;Any condition you can think of&amp;gt; THEN 1 ELSE 0 END) AS Col1&lt;/span&gt;
&lt;span class="kwrd"&gt;FROM&lt;/span&gt;   @t&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;To follow the primary school logic here: &lt;/p&gt;
&lt;p&gt;If something fits the condition return 1 else return 0. The sum of all 1's returns the number of items that satisfy the desired condition.&lt;/p&gt;
&lt;p&gt;And the condition is limited only by your business requirement.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You see there's no need to write nested select statements similar to this:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; (&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;COUNT&lt;/span&gt;(*) &lt;span class="kwrd"&gt;FROM&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; &amp;lt;someCondition1&amp;gt;) &lt;span class="kwrd"&gt;AS&lt;/span&gt; cnt1, 
       (&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; &lt;span class="kwrd"&gt;COUNT&lt;/span&gt;(*) &lt;span class="kwrd"&gt;FROM&lt;/span&gt; MyTable &lt;span class="kwrd"&gt;WHERE&lt;/span&gt; &amp;lt;someCondition1&amp;gt;) &lt;span class="kwrd"&gt;AS&lt;/span&gt; cnt2&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://weblogs.sqlteam.com/jeffs/"&gt;&lt;strong&gt;&lt;font color="#004080"&gt;Jeff&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt; has also has a good post about translating these kind of queries to case statements &lt;a target="_blank" href="http://weblogs.sqlteam.com/jeffs/archive/2008/01/09/rewrite-correlated-sub-query-with-case-sql.aspx"&gt;&lt;strong&gt;&lt;font color="#004080"&gt;here&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;And this might even provide a nice interview question...&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.sqlteam.com%2fmladenp%2farchive%2f2008%2f02%2f04%2fBack-to-Basics-Count-Count-Count-Sum-or-how-to.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.sqlteam.com%2fmladenp%2farchive%2f2008%2f02%2f04%2fBack-to-Basics-Count-Count-Count-Sum-or-how-to.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://weblogs.sqlteam.com/mladenp/aggbug/60485.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Mladen Prajdić</dc:creator>
            <guid>http://weblogs.sqlteam.com/mladenp/archive/2008/02/04/Back-to-Basics-Count-Count-Count-Sum-or-how-to.aspx</guid>
            <pubDate>Mon, 04 Feb 2008 16:24:04 GMT</pubDate>
            <wfw:comment>http://weblogs.sqlteam.com/mladenp/comments/60485.aspx</wfw:comment>
            <comments>http://weblogs.sqlteam.com/mladenp/archive/2008/02/04/Back-to-Basics-Count-Count-Count-Sum-or-how-to.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/mladenp/comments/commentRss/60485.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/mladenp/services/trackbacks/60485.aspx</trackback:ping>
        </item>
        <item>
            <title>How to solve problems and why is that perceived as hard?</title>
            <link>http://weblogs.sqlteam.com/mladenp/archive/2007/12/18/How-to-solve-problems-and-why-is-that-perceived-as.aspx</link>
            <description>&lt;p&gt;it's my opinion that today we unfortunately live in a world that requires some kind of harder problem solving skills from maybe 20% of the population. And I'm being very very generous.&lt;/p&gt; &lt;p&gt;So putting that percentage with the question in the title would give an answer similar to: "Because not every one can do it." DOH, right?  I'd say you're wrong since everyone had a problem to solve at least once in a lifetime. So what's the problem? Why is it perceived as hard? Let's take a little trip through your past.&lt;/p&gt; &lt;p&gt;When you were just an infant, what was your biggest problem? Getting fed, changed and burped, right? You cried for a bit and your problem was solved by your caretakers. Then you got a little older and you added playtime to the list (hopefully you could do burping by yourself, and potty training was fully successful). You played with your toys in your little world and could do wonders there. Life was simple. Then you started going to school and all hell broke loose. you started puberty and thought everyone else is an idiot. School ended more or less painless and since this is not a blog about gardening or ballet but about technology and your reading it means you probably went to a science oriented university. Unfortunately 95% of primary and secondary schools and universities aren't driven towards problem solving but towards cramming information. After you finish that and come to work to realize you know nothing about anything in the real world. You get thrown into the harsh reality with stuff coming at you from all over the place all the time. Things just get more complex with more and more parts mixing in unbelievable ways.&lt;/p&gt; &lt;p&gt;And herein lies the beauty of engineering. Since the dawn of time engineering has been about solving problems and not everyone thinks like an engineer. If you remember what you've learned when playing with your Lego's :) or at school at a class called Systems Theory or something similar sounding (if you paid attention and wasn't too tired from previous nights activities :)) you can see the whole concept of problem solving in basically all areas of life comes down to one simple methodology:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;Modular Design also know as Black Box Design.&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Every single good technical principle today in every single piece of technology that has to work well relies on that simple methodology. Remember this sentence for the rest of your life:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;Solutions to big problems are built from solutions to smaller problems.&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/DatabasevsApplication_133CD/Completed_6.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="216" alt="Completed" src="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/DatabasevsApplication_133CD/Completed_thumb_2.png" width="305" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;To do this well you have to have two very important skill sets:&lt;/p&gt; &lt;p&gt;- An ability of Visualization&lt;/p&gt; &lt;p&gt;- A good understanding of the tools you work with&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;h4&gt;&lt;strong&gt;An ability of Visualization&lt;/strong&gt;&lt;/h4&gt; &lt;p&gt;Visualization is necessary because without it you simply can't split your one big problem into smaller parts. Good sense of visualization leads to better sense of abstraction. Abstraction simply means lessening the problem scope. So visualization and abstraction are tied into a continuous circular reference. You visualize a solution and split it into pieces/modules/black boxes. You essentially abstract it into smaller parts. &lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/DatabasevsApplication_133CD/renault-clio-v6_6.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 0px 0px; border-right-width: 0px" height="300" alt="renault-clio-v6" src="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/DatabasevsApplication_133CD/renault-clio-v6_thumb_2.jpg" width="300" align="left" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Like this Renault Clio V6 engine on the left. Smaller parts or modules only have to perform a single task. A single unit of work. If they don't, abstract them further. This gives the ability of module reuse which lessens costs and increases robustness which in turn increases fault tolerance and the lifetime of the final product. To achieve all this a single black box/module has to be deterministic. The output must always be the same for the same input. In other words the inner state of the module is time independent.&lt;/p&gt; &lt;p&gt;With visualization and abstraction comes a &lt;strong&gt;sense of beauty&lt;/strong&gt;. Beauty is one of the the best ways to evaluate how good a solution is. A good solution is pleasing to the senses. It's symmetrical. It simply looks good. And of course it also works good. It's a simple law of nature. Good and beautiful things work better and thus prevail. &lt;/p&gt; &lt;p&gt;A good ability of Visualization means you can design things that will work well, look good and generally be optimal. &lt;/p&gt; &lt;p&gt;A redesign process when you abstract more, keeping the functionality is called refactoring. Consequently refactored solutions are generally better since they are more beautiful which is one of the best indicators of quality.&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt; &lt;h4&gt;&lt;strong&gt;A good understanding of the tools you work with&lt;/strong&gt;&lt;/h4&gt; &lt;p&gt;&lt;a href="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/DatabasevsApplication_133CD/Hammer_Needle_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 0px 0px; border-right-width: 0px" height="114" alt="Hammer_Needle" src="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/DatabasevsApplication_133CD/Hammer_Needle_thumb_1.png" width="120" align="left" border="0" /&gt;&lt;/a&gt;Understanding your tools at disposal gives you the ability to develop a solution better, faster and more elegant. You have to know the abilities and limits of your tools. You must also be open to possibilities of new tools doing a better job than the old ones. Don't be afraid of the change. Change is good. But also don't try to solve everything just with a hammer or just with a needle. Combine them.&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;h4&gt;&lt;strong&gt;Pretty picture for easier visualization of abstraction of visualization of abstraction of ....&lt;/strong&gt;&lt;/h4&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/DatabasevsApplication_133CD/Workflow_2.gif"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="543" alt="Workflow" src="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/DatabasevsApplication_133CD/Workflow_thumb.gif" width="655" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;Let's not kid ourselves. Solving problems isn't a piece of cake. It takes time, patience, care for details, etc... So don't be overly bothered if you don't succeed on the first try. Practice makes perfect and we always have to learn new things.&lt;/p&gt; &lt;p&gt;But the first thing you have to do is: &lt;/p&gt; &lt;h4&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;KNOW AND DEFINE THE REAL PROBLEM!&lt;/font&gt;&lt;/strong&gt;&lt;/h4&gt; &lt;p&gt; &lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.sqlteam.com%2fmladenp%2farchive%2f2007%2f12%2f18%2fHow-to-solve-problems-and-why-is-that-perceived-as.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.sqlteam.com%2fmladenp%2farchive%2f2007%2f12%2f18%2fHow-to-solve-problems-and-why-is-that-perceived-as.aspx" border="0" /&gt;&lt;/a&gt;  &lt;table width="100%"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td align="left"&gt;  &lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td align="left"&gt; &lt;p&gt;&lt;img id="imgAdd" alt="" src="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/225/r_OneWhitePixel.bmp" onload="javascript: try { SwitchToAdd(); } catch(e) {}" name="imgAdd" /&gt; &lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;img src="http://weblogs.sqlteam.com/mladenp/aggbug/60434.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Mladen Prajdić</dc:creator>
            <guid>http://weblogs.sqlteam.com/mladenp/archive/2007/12/18/How-to-solve-problems-and-why-is-that-perceived-as.aspx</guid>
            <pubDate>Tue, 18 Dec 2007 08:48:06 GMT</pubDate>
            <wfw:comment>http://weblogs.sqlteam.com/mladenp/comments/60434.aspx</wfw:comment>
            <comments>http://weblogs.sqlteam.com/mladenp/archive/2007/12/18/How-to-solve-problems-and-why-is-that-perceived-as.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/mladenp/comments/commentRss/60434.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/mladenp/services/trackbacks/60434.aspx</trackback:ping>
        </item>
        <item>
            <title>Introduction to locking in SQL Server 2005</title>
            <link>http://weblogs.sqlteam.com/mladenp/archive/2007/12/12/Introduction-to-locking-in-SQL-Server-2005.aspx</link>
            <description>&lt;p&gt; &lt;/p&gt;
&lt;p&gt;I've written an article here on &lt;a target="_blank" href="http://www.sqlteam.com"&gt;&lt;strong&gt;&lt;font color="#004080"&gt;SQL Team&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt; about locking in SQL Server. &lt;/p&gt;
&lt;p&gt;It's an introductory article that shows lock modes, lock granularity and lock compatibility matrix&lt;/p&gt;
&lt;p&gt;and it will be followed by a few more advanced ones on the topic of locking.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Part 1:  &lt;a target="_blank" href="http://www.sqlteam.com/article/introduction-to-locking-in-sql-server"&gt;&lt;font color="#004080"&gt;&lt;strong&gt;Introduction to locking in SQL Server 2005&lt;/strong&gt;&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.sqlteam.com/mladenp/aggbug/60428.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Mladen Prajdić</dc:creator>
            <guid>http://weblogs.sqlteam.com/mladenp/archive/2007/12/12/Introduction-to-locking-in-SQL-Server-2005.aspx</guid>
            <pubDate>Wed, 12 Dec 2007 19:36:13 GMT</pubDate>
            <wfw:comment>http://weblogs.sqlteam.com/mladenp/comments/60428.aspx</wfw:comment>
            <comments>http://weblogs.sqlteam.com/mladenp/archive/2007/12/12/Introduction-to-locking-in-SQL-Server-2005.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/mladenp/comments/commentRss/60428.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/mladenp/services/trackbacks/60428.aspx</trackback:ping>
        </item>
        <item>
            <title>Back To Basics: What is a Clustered and a Non-Clustered index (plus an appealing visual prop)</title>
            <link>http://weblogs.sqlteam.com/mladenp/archive/2007/09/18/Back-To-Basics-What-is-a-Clustered-and-a-Non-Clustered.aspx</link>
            <description>&lt;p&gt;Indexes are a constant problem in understanding for beginners (and the "not so beginners") in the database world. And don't you just love the &lt;/p&gt;
&lt;p&gt;hardcore mathematical explanation of B-Trees and their traversal. Personaly I much rather have visual props and a story to support an explanation. &lt;/p&gt;
&lt;p&gt;Appealing visual props are even better. That's how this post originated.  So let us begin!&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a atomicselection="true" href="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/clusteredvsnonclustered_D69B/librarianSmall.jpg"&gt;&lt;img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="180" alt="librarianSmall" width="150" align="left" border="0" src="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/WindowsLiveWriter/clusteredvsnonclustered_D69B/librarianSmall_thumb.jpg" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt; Imagine you live in a pre-computer-in-every-nook-and-crane world (around 1960's :)). &lt;/p&gt;
&lt;p&gt; You wake up one morning with a huge craving to read Agatha Christie's books. So you go to the library which has a few million books. &lt;/p&gt;
&lt;p&gt; You walk up to the cute librarian (look left for the appealing visual prop :)) and ask her if she can show you to the Agatha Christie &lt;/p&gt;
&lt;p&gt; books. She goes to the huge drawer (drawer #1) of little cards which hold just basic info about each book like &lt;a target="_blank" href="http://en.wikipedia.org/wiki/International_Standard_Book_Number"&gt;&lt;strong&gt;&lt;font color="#004080"&gt;ISBN number&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt;, author and &lt;/p&gt;
&lt;p&gt; title and are sorted by ISBN number. She pulls out the first card, looks at its bookshelf id and takes you to the correct bookshelf. &lt;/p&gt;
&lt;p&gt; You get your book and are on your merry way home. When you read your book you go back to the library, return &lt;/p&gt;
&lt;p&gt; the books and ask the cute librarian (again look left for the appealing visual prop :)) to get you all the books by Agatha Christie written &lt;/p&gt;
&lt;p&gt;in the 1951. This time she goes again to the huge drawer of little cards (drawer #2) but these are different than the previous ones. These hold the year of &lt;/p&gt;
&lt;p&gt;publishing and the ISBN number of the book. She writes down the ISBN numbers of all books and goes back to the first huge drawer and &lt;/p&gt;
&lt;p&gt;looks up cards that match ISBN numbers she found earlier. Again you get your books and are on your merry way home. &lt;/p&gt;
&lt;p&gt;You read them return them, get some more books until you muster up the courage to ask the cute librarian out and you live happily ever after.&lt;/p&gt;
&lt;p&gt;Nice story isn't it?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;"So what the hell am i talking about?" you might ask. Well a library is a very good analogy for indexes. How? Let's review:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;library                                                              = table&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;drawer with ISBN Number                               = Clustered index &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;drawer with year and ISBN Number                = Non-Clustered index &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;cute librarian                                                  = a perk :)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;So what you acctually did in this story is this: &lt;/p&gt;
&lt;p&gt;You executed a query (i want some books with a condition) and a query processor (cute librarian) performed a Clustered index&lt;/p&gt;
&lt;p&gt;seek on the first book hunt and a Non-Clustered index seek on the second book hunt. &lt;/p&gt;
&lt;p&gt;It produced a shortest plan for the desired action (go to the bookshelf and return with the books) and it retured the desired data back (your books).&lt;/p&gt;
&lt;p&gt;On both times she had to return a full row (a book) of data by locating it in the Clustered index (looking in the Drawer #1).&lt;/p&gt;
&lt;p&gt;This operation is also known as a Bookmark Lookup and is the most expensive operation you can do with indexes short of index scan.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;And how do those huge card drawers translate to clustered and non clustered index?&lt;/p&gt;
&lt;p&gt;ISBN Number is the column on which the clustered index is defined. The books on the shelves are sorted accordingly. The bookshelf numbers &lt;/p&gt;
&lt;p&gt;are pages of data in the table. The second drawer contains cards with year of publisihing and an ISBN Number. Year of publisihing is a Non-Clustrered index. &lt;/p&gt;
&lt;p&gt;Each non-clustered index is connected to a Clustered index, thus the ISBN Number on the second drawer's cards.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You can see that as long as the ISBN Number is contained in the Clustered index the non clustered won't be changed. &lt;/p&gt;
&lt;p&gt;This means that reindexing (not rebuilding) the Clustered Index won't rebuild the non clustered index as long as ISBN Number stays in &lt;/p&gt;
&lt;p&gt;the Clustered Index. &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;But admit it! You don't care about indexes, you just care about the cute librarian :)&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://weblogs.sqlteam.com/mladenp/archive/2007/09/18/Back-To-Basics-What-is-a-Clustered-and-a-Non-Clustered.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://weblogs.sqlteam.com/mladenp/archive/2007/09/18/Back-To-Basics-What-is-a-Clustered-and-a-Non-Clustered.aspx" /&gt;&lt;/a&gt;
&lt;table width="100%" unselectable="on"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td align="left"&gt;  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td align="left"&gt;
            &lt;p&gt;&lt;img id="imgAdd" alt="" onload="javascript: try { SwitchToAdd(); } catch(e) {}" name="imgAdd" src="http://weblogs.sqlteam.com/images/weblogs_sqlteam_com/mladenp/225/r_OneWhitePixel.bmp" /&gt; &lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;&lt;img src="http://weblogs.sqlteam.com/mladenp/aggbug/60327.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Mladen Prajdić</dc:creator>
            <guid>http://weblogs.sqlteam.com/mladenp/archive/2007/09/18/Back-To-Basics-What-is-a-Clustered-and-a-Non-Clustered.aspx</guid>
            <pubDate>Tue, 18 Sep 2007 18:15:42 GMT</pubDate>
            <wfw:comment>http://weblogs.sqlteam.com/mladenp/comments/60327.aspx</wfw:comment>
            <comments>http://weblogs.sqlteam.com/mladenp/archive/2007/09/18/Back-To-Basics-What-is-a-Clustered-and-a-Non-Clustered.aspx#feedback</comments>
            <slash:comments>10</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/mladenp/comments/commentRss/60327.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/mladenp/services/trackbacks/60327.aspx</trackback:ping>
        </item>
    </channel>
</rss>