<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://strangenut.com/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>SQL Server Blog : beginner</title><link>http://strangenut.com/blogs/dacrowlah/archive/tags/beginner/default.aspx</link><description>Tags: beginner</description><dc:language>en</dc:language><generator>CommunityServer 2008 SP1 (Build: 30619.63)</generator><item><title>How to find all tables with a column of a particular name in SQL Server 2005 and 2008</title><link>http://strangenut.com/blogs/dacrowlah/archive/2009/02/13/how-to-find-all-tables-with-a-column-of-a-particular-name-in-sql-server-2005-and-2008.aspx</link><pubDate>Fri, 13 Feb 2009 07:37:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:47</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/dacrowlah/rsscomments.aspx?PostID=47</wfw:commentRss><comments>http://strangenut.com/blogs/dacrowlah/archive/2009/02/13/how-to-find-all-tables-with-a-column-of-a-particular-name-in-sql-server-2005-and-2008.aspx#comments</comments><description>&lt;p&gt;This is a simple query for finding all instances of a column with a particular name along with the table that it belongs to.&amp;nbsp; This will not work on SQL Server 2000.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-autospace:none;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;color:blue;"&gt;select&lt;/span&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt; &lt;span style="color:green;"&gt;sys.objects&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;name&lt;/span&gt; &lt;span style="color:blue;"&gt;as&lt;/span&gt; TableName&lt;span style="color:gray;"&gt;,&lt;/span&gt; &lt;span style="color:green;"&gt;sys.columns&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;name&lt;/span&gt; &lt;span style="color:blue;"&gt;as&lt;/span&gt; ColumnName&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="text-autospace:none;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-autospace:none;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&lt;span style="color:blue;"&gt;from&lt;/span&gt; &lt;span style="color:green;"&gt;sys.columns&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="text-autospace:none;"&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&amp;nbsp;&lt;span style="color:gray;"&gt;inner&lt;/span&gt; &lt;span style="color:gray;"&gt;join&lt;/span&gt; &lt;span style="color:green;"&gt;sys.objects&lt;/span&gt; &lt;span style="color:blue;"&gt;on&lt;/span&gt; &lt;span style="color:green;"&gt;sys.objects&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:fuchsia;"&gt;object_id&lt;/span&gt; &lt;span style="color:gray;"&gt;=&lt;/span&gt; &lt;span style="color:green;"&gt;sys.columns&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:fuchsia;"&gt;object_id&lt;/span&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;&amp;nbsp;&lt;span style="color:blue;"&gt;where&lt;/span&gt; &lt;span style="color:green;"&gt;sys.columns&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:blue;"&gt;name&lt;/span&gt; &lt;span style="color:gray;"&gt;=&lt;/span&gt; &lt;span style="color:red;"&gt;&amp;#39;countyid&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=47" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/database/default.aspx">database</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/beginner/default.aspx">beginner</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category></item><item><title>How to enable CLR Stored procedures on SQL Server 2005 and 2008</title><link>http://strangenut.com/blogs/dacrowlah/archive/2008/10/26/how-to-enable-clr-stored-procedures-on-sql-server-2005-and-2008.aspx</link><pubDate>Mon, 27 Oct 2008 01:53:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:34</guid><dc:creator>dacrowlah</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/dacrowlah/rsscomments.aspx?PostID=34</wfw:commentRss><comments>http://strangenut.com/blogs/dacrowlah/archive/2008/10/26/how-to-enable-clr-stored-procedures-on-sql-server-2005-and-2008.aspx#comments</comments><description>&lt;p&gt;In order to make CLR stored procedures run, you must first enable the CLR in SQL Server, other wise you will just get this error:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Execution of user code in the .NET Framework is disabled. Enable &amp;quot;clr enabled&amp;quot; configuration option.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Enabling the CLR is fairly simple, just run this SQL Statement in Query Analyzer, while connected to the database that needs this option enabled:&lt;/p&gt;
&lt;pre&gt;&lt;p&gt;EXEC sp_configure &amp;#39;show advanced options&amp;#39; , &amp;#39;1&amp;#39;; &lt;br /&gt;reconfigure; &lt;br /&gt;&lt;br /&gt;EXEC sp_configure &amp;#39;clr enabled&amp;#39; , &amp;#39;1&amp;#39; ;&lt;br /&gt;reconfigure; &lt;br /&gt;&lt;br /&gt;EXEC sp_configure &amp;#39;show advanced options&amp;#39; , &amp;#39;0&amp;#39;; &lt;br /&gt;reconfigure;&lt;/p&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=34" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/database/default.aspx">database</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/beginner/default.aspx">beginner</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/stored+procedure/default.aspx">stored procedure</category></item><item><title>Making the case for stored procedures</title><link>http://strangenut.com/blogs/dacrowlah/archive/2008/10/26/making-the-case-for-stored-procedures.aspx</link><pubDate>Sun, 26 Oct 2008 07:23:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:33</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/dacrowlah/rsscomments.aspx?PostID=33</wfw:commentRss><comments>http://strangenut.com/blogs/dacrowlah/archive/2008/10/26/making-the-case-for-stored-procedures.aspx#comments</comments><description>&lt;p&gt;On a few of the mailing lists I am on for developers (primarily ones related to web development), there is a recurring topic that always gets people into rather heated debates: to use stored procedures or prepared statements?&amp;nbsp; Now, either one you choose to use, is largely dependent on two things: preference/comfort level, and the appropriate tool for the appropriate job.&amp;nbsp; This will be my attempt at defining (my opinion of) the right tool for the right job, while allowing for maximum flexibility.&lt;br /&gt;&lt;br /&gt;Now, for those who are not aware of the difference, a prepared statement is straight SQL sent to the db for execution, but with explicitly declared parameters so as to prevent SQL injection attacks. The techniques on how to do this vary from language to language, but in ColdFusion it is through the use of the &amp;lt;cfqueryparam&amp;gt; tag, and in .NET, you use the SqlCommand object, type of text, and where you want the parameter values, you place a &amp;ldquo;?&amp;rdquo;, then add the parameters in the proper order.&lt;br /&gt;&lt;br /&gt;Now, prepared statements are not a bad thing at all; they offer some of the same benefits of stored procedures: explicit parameterization, elevated chances of execution plan reuse, saves overhead on the db side of having to do type checking/conversion, etc.&amp;nbsp; One additional benefit of using prepared statements, it&amp;rsquo;s a heck of a lot easier to write secure dynamic SQL for complex query building.&lt;br /&gt;&lt;br /&gt;However, stored procedures are a far better choice 99% of the time; complex dynamic SQL being the sole exception that I would consent to allowing in any database I was in charge of administering or code being checked in by a developer who worked for me. Stored procedures have several distinct advantages over prepared statements (or inline SQL in general) , and some of them become more apparent only after having built large, complex, enterprise level applications that have multiple developers and support staff working on them.&lt;br /&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Guaranteed execution plan reuse&lt;/li&gt;
&lt;li&gt;As long as you aren&amp;rsquo;t doing dynamic SQL within your stored proc and concatenating strings for execution, absolutely no chance of a SQL injection attack happening&lt;/li&gt;
&lt;li&gt;Performance increases for saving on permissions checks &amp;ndash; when you submit a prepared statement for execution the database server needs to check the permissions chain on every object that that query touches, EVERY time it executes.&amp;nbsp; This is not the same as an execution plan. With a stored procedure you create the procedure and GRANT EXECUTE for that proc to the Id that your web application connects to the database as.&lt;/li&gt;
&lt;li&gt;Carefully controlled access to data &amp;ndash; sometimes your data has sensitive information (like Social Security Numbers, secret question/answer information, etc) and while you have to both store that information, and maintain a level of security around it so that not everyone in the IT department has access to it, you create a stored proc, and enforce access to the data through stored procedures.&amp;nbsp; This method also gives you the ability to give partial access to data (such as a redacted SSN, instead of the full SSN)&lt;/li&gt;
&lt;li&gt;Modularity of code &amp;ndash; complex logic for doing multiple operations to data in the database is easily ported from one process to another.&amp;nbsp; As an example, one application I routinely work on has 3 different methods for creating a user (new user auto-registration, administrative interface for creating them by uploading an excel file, and one that creates them via a scheduled batch job from excel files uploaded by clients) and there is some pretty complex logic for finding duplicate users across multiple clients and preventing new users from being created who exist elsewhere.&amp;nbsp;&amp;nbsp; Since this piece of code is in the stored procedure, adding new sources and tools to create users is dead simple&amp;hellip; call the stored procedure and that is that.&amp;nbsp; One argument that I heard to try and minimize the impact of this advantage &amp;ldquo;what if you had a single, well architected application written in a single language and the query was easily available from other sections of code within the application, why is there a modularity advantage for this?&amp;nbsp; Well, if you have a single language in your application, and its easily accessible everywhere else, it does lower the points this one gives you, but that was kind of my point here, it lends flexibility even if you don&amp;rsquo;t anticipate needing it now.&lt;/li&gt;
&lt;li&gt;Minimizing the amount of traffic sent over the wire.&amp;nbsp; Sometimes as developers we think 10Mbps/100Mbps/1000Mbps Ethernet has so much bandwidth that sending this little tiny 20 line query over the wire has no impact and won&amp;rsquo;t cause any appreciable slowdown.&amp;nbsp; Well, that is true for the most part, but wait till your application starts getting a ton of traffic&amp;hellip; the more packets you send over the wire, the higher your collision rate on the network, and you&amp;rsquo;ll see reduced network efficiency, sad but true; also one of the most often overlooked reasons for using stored procedures. &lt;/li&gt;
&lt;li&gt;Wise choices in location of your business logic will greatly reduce your round trips to the database when using stored procedures.&amp;nbsp; If you are performing an operation that can be completed from a solid set of parameters from start to finish and the end result (success/failure) is the only thing you need to know, a stored procedure is what you need.&amp;nbsp; Since this was just used as an example I&amp;rsquo;ll use it again: say you are creating a user you have first name, last name, email, address, list of positions, departments, and supervisors.&amp;nbsp; If you have ALL of that logic in your middle tier and not in a stored procedure, you will have to keep going back and doing lookups for data, making more round trips to save, add, and then complete.&amp;nbsp; All of these round trips add to network overhead, time spent encoding/decoding data to send across the wire, process... locate, return&amp;hellip;&amp;nbsp; lots of data, lots of network chatter.&amp;nbsp; This would all be eliminated with a stored procedure, you would feed one set of data to the proc, and execute it, and everything will be handled server side and the result handed back to you.&lt;/li&gt;
&lt;li&gt;Centralization of maintenance, changes, security, and stronger separation of tiers. &lt;/li&gt;
&lt;li&gt;Ease in separation in application tiers for troubleshooting.&amp;nbsp; If you write a stored proc, use it in your application, and suddenly start seeing weird results in the application, you can go, execute the proc, and eyeball the results&amp;hellip; do they look weird? No? Then the chances are good the problem lies in your application layer, start looking there.&amp;nbsp; Many years of supporting messy applications and code taught me this lesson all too well.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=33" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/query+execution+plans/default.aspx">query execution plans</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/performance+tuning/default.aspx">performance tuning</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/database/default.aspx">database</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/beginner/default.aspx">beginner</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/stored+procedure/default.aspx">stored procedure</category></item><item><title>The Basics of Database Normalization - Explained</title><link>http://strangenut.com/blogs/dacrowlah/archive/2008/07/30/the-basics-of-database-normalization-explained.aspx</link><pubDate>Wed, 30 Jul 2008 22:05:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:30</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/dacrowlah/rsscomments.aspx?PostID=30</wfw:commentRss><comments>http://strangenut.com/blogs/dacrowlah/archive/2008/07/30/the-basics-of-database-normalization-explained.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;Data Normalization&lt;/b&gt;, refers to creating a structure to store your data in that results in reduction/elimination of redundant data.&amp;nbsp; In a pure normalized form, a piece of data exists only in one place.&amp;nbsp;&amp;nbsp; Anywhere that the data is displayed is only making a reference to that data and joining tables together to recreate a human readable data structure.&amp;nbsp; You gain several things from having a well normalized database schema, and you lose some things.&amp;nbsp; Things that you gain are forced integrity of your data, everything belongs to something, attribute values are enforced to be in a known set of choices, and things are generally more clean and structured.&amp;nbsp; Things you lose.. simplicity.&amp;nbsp; The process of normalization results in generation of more tables, and so querying against data stored in these becomes more complex as you need to start doing joins and knowing what data will be there, what data might not be there;&amp;nbsp; you can sometimes start getting very odd results if you are not careful.&lt;/p&gt;
&lt;p&gt;As with many other subjects, there is a set of vocabulary terms that are used to express complex concepts succintly; data normalization is no different. There are varying degrees of normalization, each one being progressively more strict about what conditions must be met to attain a given level; these are referred to as &amp;quot;Normal Forms&amp;quot;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;b&gt;First Normal Form&lt;/b&gt; - typically abbreviated as &lt;b&gt;1NF&lt;/b&gt;, this is the most basic form, stating simply that a table must enforce that each row be unique, and not allow nulls in any column.&amp;nbsp; So, an example to illustrate this lets take a table that most of you will have worked with at one point or another: Addresses.&amp;nbsp; We will define the structure of the table as such:&lt;/p&gt;
&lt;pre&gt;create table Addresses&lt;br /&gt;(&lt;br /&gt;	AddressId int identity(1,1) not null primary key,&lt;br /&gt;	StreetAddress1 nvarchar(100) not null,&lt;br /&gt;	StreetAddress2 nvarchar(100) not null,&lt;br /&gt;	City nvarchar(100) not null,&lt;br /&gt;	State nvarchar(100) not null,&lt;br /&gt;	Zip nvarchar(100) not null,&lt;br /&gt;	IsMailingAddress bit not null,&lt;br /&gt;	IsBillingAddress bit not null,&lt;br /&gt;	IsPrimaryAddress bit not null	&lt;br /&gt;)&lt;br /&gt;go&lt;/pre&gt;
&lt;p&gt;This table adheres to the basic definition of 1NF, it has an identity column that has a unique key on it, which by definition, enforces that the row will be unique, even if the data is duplicated in every other column.&amp;nbsp; If we changed the table to allow the IsMailingAddress (or any other column for that matter to contain a null value, then this table would no longer be properly normalized.&amp;nbsp; There is some degree of contention about 1NF though; some people argue that &lt;a target="_blank" title="Wikipedia Entry for Edgar Codd" href="http://en.wikipedia.org/wiki/Edgar_F._Codd"&gt;Dr. Edgar F. Codd&lt;/a&gt; (the man who worked for IBM and invented the idea of relational data models) stated that null values must be allowed in a relational database.&amp;nbsp; If you go with this concept, you will never be able to achieve a fully normalized database.&amp;nbsp; For the sake of this discussion though, we will avoid a detailed explanation of this so as to keep things in scope.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Second Normal Form -&lt;/b&gt; also referred to as 2NF, this states that, first, the table must meet the guidelines of 1NF to be considered 2NF compliant.&amp;nbsp; Further, that all columns in the table must rely on the key to express their uniqueness as a row in the table.&amp;nbsp; Lets think about it like this if you have a table &lt;i&gt;[Employees]&lt;/i&gt; with the columns &lt;i&gt;[EmployeeId],[EmployeeName],&lt;/i&gt; and &lt;i&gt;[Skill]&lt;/i&gt;, and that a row in the table relied on a combination of EmployeeId and Skill to express uniqueness as a row, then the table is not meet the guidelines for 2NF.&amp;nbsp; Due to the fact that EmployeeName does not depend on Skill, it only depends on EmployeeId (other employees can have the same skill, but other employees cannot have the same EmployeeId)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Third Normal Form -&lt;/b&gt; 3NF is the more commonly used structure when designing a new data model.&amp;nbsp; It states that first a table must meet the requirements of 2NF, and goes one step further to state that all transitive or otherwise extraneous attributes have to be eliminated, and split off into what are commonly called Type tables.&amp;nbsp; Let&amp;#39;s look at again at the table above; notice how we have 3 columns that are attributes of an address &lt;i&gt;IsMailingAddress, IsBillingAddress, &lt;/i&gt;and &lt;i&gt;IsPrimaryAddress&lt;/i&gt;.&amp;nbsp; These 3 columns are transitive properties, and would need to be removed from this table.&amp;nbsp; We would first create a table &lt;i&gt;AddressType&lt;/i&gt;, and have the columns &lt;i&gt;AddressTypeId, AddressTypeName,&amp;nbsp; &lt;/i&gt;and perhaps an &lt;i&gt;AddressTypeDescription&lt;/i&gt; column.&amp;nbsp; Next, we would create a table &lt;i&gt;AddressTypes &lt;/i&gt;(note, these naming conventions can be adapted to best suit you, this is simply a convention that I have become accustomed to and ultimately I prefer a verbose name that directly implies what type of data we are working with), this table would have only two columns &lt;i&gt;AddressId&lt;/i&gt;, and &lt;i&gt;AddressTypeId&lt;/i&gt;.&lt;i&gt;&amp;nbsp; &lt;/i&gt;The goal of this is to only explicitly state what types the address IS, not also specify what they are NOT.&amp;nbsp;&amp;nbsp; Since not every address is a billing address, its extraneous data... we just don&amp;#39;t care.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;In most cases, 3NF is the most you will ever need to know about.&amp;nbsp; There are additional ones though, but their definitions are difficult to explain in concise terms to those new to database design, so I will address those later in detail.&amp;nbsp; Briefly though, they are Boyce-Codd Normal Form (BCNF), Fourth Normal Form (4NF), Fifth Normal Form (5NF), and Sixth Normal Form (6NF).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=30" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/database/default.aspx">database</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/beginner/default.aspx">beginner</category></item><item><title>What is a table scan?</title><link>http://strangenut.com/blogs/dacrowlah/archive/2008/02/24/what-is-a-table-scan.aspx</link><pubDate>Mon, 25 Feb 2008 01:18:00 GMT</pubDate><guid isPermaLink="false">887d9c72-1b14-4886-84ba-c54c709ac717:4</guid><dc:creator>dacrowlah</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://strangenut.com/blogs/dacrowlah/rsscomments.aspx?PostID=4</wfw:commentRss><comments>http://strangenut.com/blogs/dacrowlah/archive/2008/02/24/what-is-a-table-scan.aspx#comments</comments><description>&lt;p&gt;In the world of RDBMS&amp;#39; you may hear the term &amp;quot;table scan&amp;quot; thrown around, this is a term used to describe an event that occurs when you search for data in a table, and your query either doesn&amp;#39;t take advantage of an existing index, or there is no index on the table. Generally, a table scan is not a good thing to have happening on a frequent basis in your database.&amp;nbsp; There are some instances where it&amp;#39;s not a bad thing, and I will try now to explain what it is, why it&amp;#39;s happening, and when its not that bad of an occurence.&lt;/p&gt;

&lt;h3&gt;What Happens During a Table Scan&lt;br /&gt;&lt;/h3&gt;&lt;p&gt;Suppose you aren&amp;#39;t much of an organized person, and your cubicle at work isn&amp;#39;t really well organized.&amp;nbsp; You&amp;#39;ve got papers laying all around, some filed, some stacked, some in the filing cabinet, some in folders. Generally speaking, it&amp;#39;s hard to find data like that; then suppose your boss asks you for all of your notes related to a specific project that you are working on.&amp;nbsp; Unless you work for a very large company with ample developers and enough resources to have you dedicate yourself to one project at a time, you probably have multiple projects going on, and notes on all of them are mixed around the cubicle.&lt;/p&gt;&lt;p&gt;So how do you find everything? Answer: you look through every stack, folder, drawer, and box you&amp;#39;ve got; and then hope against hope you have it all.&amp;nbsp; Well, this is essentially a table scan, your cubicle is a table,&amp;nbsp; and you my friend, are the Query Processing engine.&lt;/p&gt;&lt;p&gt;When a query is submitted to SQL Server, it attempts to determine the best way to execute a query, generating what is called a &amp;quot;Query Execution Plan&amp;quot;, that describes how it will go about fulfilling the request to find every note related to the project, and return them to the boss.&amp;nbsp; The reason these are typically considered a Very Bad Thing ® is because when SQL Server has to search through all of the data in a table to satisfy a query, it consumes more resources than it really needs to.&lt;/p&gt;
&lt;h3&gt;How to Avoid Table Scans&lt;/h3&gt;
&lt;p&gt;If you are an organized person, you keep all of your project notes neatly organized, filed away, and close to each other.&amp;nbsp; SQL Server has its way of doing this as well, it&amp;#39;s called an index; an index is a way of keeping track of what data you have,and where it resides in order to enable a fast retrieval of it when it is requested.&lt;/p&gt;&lt;p&gt;Now, building an index in SQL Server is easy, however knowing when and where to build them is a bit more of an art.&amp;nbsp; SQL Server performance tuning is absolutely dependant on indexes, and being knowledgeable enough about how to write queries that take advantage of them is essential.&amp;nbsp; Generally speaking, if a column in a table is referenced frequently in queries, then it should have an index on it, the more read intensive your database is, the more this holds true.&amp;nbsp; It is not usually a good idea to have indexes on every column in a table for multiple reasons, chief among these is space considerations; under the covers, indexes are copies of the data from specific columns in a table extracted and organized for fast searching, so to build an index will take up space, roughly equivalent to the volume of data being indexed.&lt;/p&gt;&lt;p&gt;Another consideration to take in account when building indexes is whether or not your data is added to or updated very frequently; the more often&amp;nbsp; data in a table changes, the greater the impact indexes will have in performance.&amp;nbsp; Every time indexed data changes, SQL Server has to ensure that the index is kept up to date, which adds to the processing overhead which often times is overlooked in database design and performance considerations.&lt;/p&gt;&lt;h3&gt;When a Table Scan Isn&amp;#39;t Such a Bad Thing ®&lt;/h3&gt;
&lt;p&gt;As I mentioned previously, sometimes its not so bad to have table scans.&amp;nbsp; If you are retrieving every row from a table, and plan on using it, it doesn&amp;#39;t really matter that you are doing a table scan because there is really no other way to do it. Another time that it doesn&amp;#39;t matter so much is when there is very little data in a table, like a type table or a small lookup table, because SQL Server can intelligently cache data when it needs to, and when there is very little data it can actually degrade performance some when it does have an index because of the overhead of maintaining the index. Striking a good balance of indexing vs. non-indexing is a skill that will come with time and experience.&lt;/p&gt;&lt;p&gt;It&amp;#39;s a good idea to learn how to read an execution plan and know what your server is doing internally when it is running your queries; an upcoming post will cover this.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://strangenut.com/aggbug.aspx?PostID=4" width="1" height="1"&gt;</description><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/query+execution+plans/default.aspx">query execution plans</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/performance+tuning/default.aspx">performance tuning</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/Indexes/default.aspx">Indexes</category><category domain="http://strangenut.com/blogs/dacrowlah/archive/tags/beginner/default.aspx">beginner</category></item></channel></rss>