<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.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>Search results matching tag 'statistics'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=statistics&amp;orTags=0</link><description>Search results matching tag 'statistics'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Geek City: More statistics info available!</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2013/04/09/more-statistics-info.aspx</link><pubDate>Wed, 10 Apr 2013 01:39:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:48605</guid><dc:creator>Kalen Delaney</dc:creator><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;p&gt;I just started using a new DMV (one that’s actually an ‘F’ not a ‘V’, as in Function) that gives us more info about distribution statistics. It returns info about the last statistics update date (which is also available with a function STATS_DATE()). It also provides the number of rows sampled when the statistics were last updated. This is available through DBCC SHOW_STATISTICS, and in &lt;a href="http://sqlblog.com/blogs/kalen_delaney/archive/2013/01/18/accessing-distribution-statistics.aspx"&gt;a blog post a couple of months ago&lt;/a&gt;, I showed you how to get that output into a table for your own querying. One of the coolest things this new DMV shows is the row modification counter, which used to be available in &lt;em&gt;sysindexes&lt;/em&gt; as a count of rows changed, but in SQL 2005 and later, SQL Server keeps track of changes to each column that has statistics on it. These values were not visible before, but now they are! This new object also reports any filter definition and includes rows for all statistics, whether index statistics or column statistics.&lt;/p&gt;  &lt;p&gt;As a table valued function, &lt;em&gt;sys.dm_db_stats_properties&lt;/em&gt; can be used with the CROSS APPLY operator to give information for all statistics in a database, or you can filter to just return&amp;nbsp; user objects and not any system objects, as shown in the code here:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font face="Courier New"&gt;SELECT       &lt;br&gt;&amp;nbsp;&amp;nbsp; sp.object_id, object_name(sp.object_id) as object_name,sp.stats_id, name as stats_name,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; filter_definition, last_updated, rows, rows_sampled, steps, unfiltered_rows, modification_counter         &lt;br&gt;FROM sys.stats AS s         &lt;br&gt;CROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) AS sp        &lt;br&gt;WHERE sp.object_id &amp;gt; 100;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The new DMV was easy to miss because it wasn’t added until SQL Server 2008R2 Service Pack 2 and SQL Server 2012 Service Pack1. But since you should always be running with the latest service pack, you should have this great DMV if you’re any version after SQL Server 2008. &lt;/p&gt;  &lt;p&gt;Let me know if you find this DMV useful, and what you mainly use it for.&lt;/p&gt;  &lt;p&gt;Have fun!&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff00ff" size="4"&gt;~Kalen&lt;/font&gt;&lt;/p&gt;</description></item><item><title>Geek City: Accessing Distribution Statistics</title><link>http://sqlblog.com/blogs/kalen_delaney/archive/2013/01/18/accessing-distribution-statistics.aspx</link><pubDate>Fri, 18 Jan 2013 20:08:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47218</guid><dc:creator>Kalen Delaney</dc:creator><description>&lt;p&gt;Distribution statistics are one of the most important sources of information that the Query Optimizer uses to determine a good query plan. In this post, I’m not going to tell you everything about distribution statistics. I’m just going to show you a few tricks for getting access to the statistics.&lt;/p&gt;  &lt;p&gt;If you want a deeper understanding of what the statistics keep track of, and you don’t have any of my &lt;strong&gt;&lt;em&gt;SQL Server Internals&lt;/em&gt;&lt;/strong&gt; books handy, check out this whitepaper: &lt;a href="http://msdn.microsoft.com/en-us/library/dd535534(v=SQL.100).aspx"&gt;Statistics Used by the Query Optimizer in Microsoft SQL Server 2008&lt;/a&gt;&amp;nbsp; &lt;/p&gt;  &lt;p&gt;Microsoft does provide us a tool called DBCC SHOW_STATISTICS for examining the distribution statistics. &lt;/p&gt;  &lt;p&gt;Microsoft has gradually been making more of the more of the old DBCC commands available as DMVs, even some undocumented ones. For example, one of my favorites, DBCC IND, has now been replaced in SQL Server 2012&amp;nbsp; by &lt;em&gt;sys.dm_db_database_page_allocations&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;I have been wishing for several versions that Microsoft would make the DBCC SHOW_STATISTICS information available as a DMV. But it hasn’t happened yet, and I’m tired of waiting, so I decided to do something about it. &lt;/p&gt;  &lt;p&gt;My solution is not quite as easy to use as a DMV might be, but it allows you to get the information that DBCC SHOW_STATISTICS provides into a set of three tables that can then be saved into a more permanent location of your choice, and/or queried as desired. &lt;/p&gt;  &lt;p&gt;DBCC SHOW_STATISTICS returns three sets of information, with different columns in the output, so three different tables are needed. The DBCC SHOW_STATISTICS command can be called with an argument that specifies that you just want one of the three sets returned. The options are&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;WITH STAT_HEADER&lt;/strong&gt; – returns basic info such as last update date, and number of rows in the table/index. Also reports number of steps returned for HISTOGRAM section.&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;WITH DENSITY_VECTOR&lt;/strong&gt; – returns density info for each left-based subset of columns in the index. For example, an index on (lastname, firstname, city) would have a density value for (lastname), for (lastname, firstname), and for (lastname, firstname, city). Each density value is a single number representing the average number of occurrences and depends on the number of distinct values. For example, if there are only 2 possible values in the column, the density would be 0.5. Multiplying density by the number of rows in the STAT_HEADER section would give the average expected rowcount if a query was executed looking for an equality on the specified column(s). &lt;/p&gt;    &lt;p&gt;&lt;strong&gt;WITH HISTOGRAM&lt;/strong&gt; – returns a set of ordered values from the first column of the index, creating a histogram. This histogram provides the optimizer with selectivity information for specific values or ranges of values in the first column of the index. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;To collect this info, I will use one of my favorite tricks, which is to create a table in the &lt;em&gt;master&lt;/em&gt; database with a name starting with sp_. (I’ve written about this trick several times, including in this &lt;a href="http://sqlblog.com/blogs/kalen_delaney/archive/2008/08/11/geek-city-system-objects.aspx"&gt;earlier blog post.)&lt;/a&gt; Once I have the table(s) created, I can access them from any database. So here are the three tables:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font face="Consolas"&gt;USE Master;        &lt;br&gt;GO&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font face="Consolas"&gt;IF&amp;nbsp; (SELECT object_id('sp_stat_header')) IS NOT NULL        &lt;br&gt;&amp;nbsp; DROP TABLE sp_statsheader;         &lt;br&gt;GO         &lt;br&gt;CREATE TABLE sp_stat_header         &lt;br&gt;(&amp;nbsp;&amp;nbsp; Name sysname,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Updated datetime,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rows bigint,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rows_sampled bigint,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Steps smallint,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Density numeric (10,9),         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Average_key_length smallint,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String_index char(3),         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Filter_expression nvarchar(1000),         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Unfiltered_rows bigint);         &lt;br&gt;GO&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;     &lt;br&gt;&lt;font face="Consolas"&gt;IF&amp;nbsp; (SELECT object_id('sp_density_vector')) IS NOT NULL        &lt;br&gt;&amp;nbsp; DROP TABLE sp_density_vector;         &lt;br&gt;GO         &lt;br&gt;CREATE TABLE sp_density_vector         &lt;br&gt;(&amp;nbsp; all_density numeric(10,8),         &lt;br&gt;&amp;nbsp;&amp;nbsp; average_length smallint,         &lt;br&gt;&amp;nbsp;&amp;nbsp; columns nvarchar(2126) );         &lt;br&gt;GO&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font face="Consolas"&gt;IF&amp;nbsp; (SELECT object_id('sp_histogram')) IS NOT NULL        &lt;br&gt;&amp;nbsp; DROP TABLE sp_histogram;         &lt;br&gt;GO         &lt;br&gt;CREATE TABLE sp_histogram         &lt;br&gt;(&amp;nbsp;&amp;nbsp; RANGE_HI_KEY sql_variant,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; RANGE_ROWS bigint,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EQ_ROWS bigint,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DISTINCT_RANGE_ROWS bigint,         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AVG_RANGE_ROWS bigint);         &lt;br&gt;GO&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font face="Consolas"&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The second trick is to use INSERT … EXEC to execute a DBCC statement and populate the tables. I will build the DBCC&amp;nbsp; command dynamically, after capturing the schema, table and index names in variables. You of course could take this code and turn it into a stored procedure, for which the schema, table and index names are passed as parameters. I’ll use as an example a table in the &lt;em&gt;AdventureWorks2008&lt;/em&gt; sample database, just so you can try running the code, and I can verify that it actually works!&lt;/p&gt;  &lt;p&gt;I will use the table &lt;em&gt;Sales.SalesOrderDetail&lt;/em&gt; and the index&lt;em&gt; IX_SalesOrderDetail_ProductID&lt;/em&gt;. So the object name (@oname) is &lt;em&gt;SalesOrderDetail&lt;/em&gt;, the schema name (@sname) is &lt;em&gt;Sales&lt;/em&gt;, and the index name (@iname) is &lt;em&gt;IX_SalesOrderDetail_ProductID.&lt;/em&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font face="Consolas"&gt;SET NOCOUNT ON;        &lt;br&gt;USE AdventureWorks2008;         &lt;br&gt;GO         &lt;br&gt;DECLARE @oname sysname,&amp;nbsp; @iname sysname, @sname sysname&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font face="Consolas"&gt;SELECT @oname = 'SalesOrderDetail',&amp;nbsp; @sname = 'Sales', @iname = 'IX_SalesOrderDetail_ProductID';        &lt;br&gt;&amp;nbsp; &lt;br&gt;-- Update the object name to include the schema name, because that is the format the DBCC command expects         &lt;br&gt;SELECT @oname = @sname +'.' + @oname;         &lt;br&gt;&lt;/font&gt;&lt;/p&gt;   &lt;font face="Consolas"&gt;TRUNCATE TABLE sp_stat_header;      &lt;br&gt;INSERT INTO sp_stat_header       &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EXEC ('DBCC SHOW_STATISTICS(['+ @oname + '],' + @iname +') WITH STAT_HEADER'); &lt;/font&gt;    &lt;p&gt;&lt;font face="Consolas"&gt;TRUNCATE TABLE sp_density_vector;        &lt;br&gt;INSERT INTO sp_density_vector         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EXEC ('DBCC SHOW_STATISTICS(['+ @oname + '],' + @iname +') WITH DENSITY_VECTOR');&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font face="Consolas"&gt;TRUNCATE TABLE sp_histogram;        &lt;br&gt;INSERT INTO sp_histogram         &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EXEC ('DBCC SHOW_STATISTICS(['+ @oname + '],' + @iname +') WITH HISTOGRAM');&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;      &lt;p&gt;So now you can look at the values collected and filter or query in any way, or use SELECT INTO to save them into another table, so the &lt;em&gt;sp_&lt;/em&gt; tables can be used the next time you want to capture distribution statistics information. &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font face="Consolas"&gt;SELECT * FROM sp_stat_header;        &lt;br&gt;        &lt;br&gt;SELECT * FROM sp_density_vector;         &lt;br&gt;        &lt;br&gt;SELECT * FROM sp_histogram;&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;/blockquote&gt;    &lt;p&gt;Let me know if you find this useful, and especially if you embellish it to create a procedure or an automated process of your own!&lt;/p&gt;  &lt;p&gt;Thanks!&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0080" size="4"&gt;&lt;strong&gt;~Kalen&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;</description></item><item><title>Decoding STATS_STREAM</title><link>http://sqlblog.com/blogs/joe_chang/archive/2012/05/05/decoding-stats-stream.aspx</link><pubDate>Sat, 05 May 2012 10:16:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:43206</guid><dc:creator>jchang</dc:creator><description>&lt;p&gt;Data distribution statistics is one of the foundations of the cost-based query optimizer in all modern database engines
including SQL Server. 
From SQL Server 2005 on, most of the information displayed by DBCC SHOW_STATISTICS is kept in a binary field 
accessible with the STATS_STREAM clause.
Back in SQL Server 2000, it was possible to modify system tables directly,
including the sysindexes stat_blob field.
At the time, I described a decode of the stat_blob field with the purpose of influence the execution plan,
presumably on a development system and not a production system.
&lt;/p&gt;

&lt;p&gt;
Starting with SQL Server 2005, it was nolonger possible to directly modify system tables.
An API was provided to access data distribution statistics to allow cloning the statistics from one database
to another. 
The presumed usage is to clone statistics from a large production database to a small development database.
In other database engines, I had heard of the idea of updating statistics on a backup system
to be applied to the production system.
While it was still possible to decode most of the 2005 stats_stream binary,
it appears that a checksum was added so it was not possible to 
apply an externally generated statistics binary unless the "checksum" value could be correctly calculated.
&lt;/p&gt;

&lt;p&gt;
Around this time, I was working on other SQL Server tools, 
most prominently &lt;a href="http://www.qdpma.com\SqlSystemZip.html"&gt;SQL System&lt;/a&gt; for performance monitoring, 
&lt;a href="http://www.qdpma.com\SQLExecStats.html"&gt;Exec Stats&lt;/a&gt; for execution plan analysis and TraceAnalysis for trace processing. 
Work on the SQL Server data distribution cloning tool was discontinued, 
and I could not continue further research into the decoding of SQL Server data distribution statistics.
&lt;/p&gt;

&lt;p&gt;
Since several people have asked about the data distribution statisics decode, 
I am making what I know about stat_stream available.
It would be helpful is other people would contribute to the missing pieces.&lt;/p&gt;

&lt;p&gt;Note that organization of stats_stream changed from SQL Server version 2000 (then sysindexes stat_blob)&amp;nbsp;to 2005 and again to 2008?
It is quite possible there are also changes in version 2012?
Most of what I discuss here applies to version 2008 R2.
&lt;/p&gt;
&lt;h4&gt;Decoding Stats Stream for SQL Server 2008R2&lt;/h4&gt;
&lt;i&gt;Here I am using 1 based reference. Byte index 1 is the first byte. 
&lt;br&gt;In C# and most other programming languages use zero based index.&lt;/i&gt;

&lt;table cellPadding="2"&gt;

&lt;tr align="left"&gt;&lt;th&gt;Position&lt;/th&gt;&lt;th&gt;Length&lt;/th&gt;&lt;th&gt;Value/Type&lt;/th&gt;&lt;th&gt;Purpose&lt;/th&gt;&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;1&lt;/td&gt;

&lt;td&gt;4?&lt;/td&gt;

&lt;td&gt;1&lt;/td&gt;

&lt;td align="left"&gt;unkown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;5&lt;/td&gt;

&lt;td&gt;4?&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;number of vectors&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;9&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;zero&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;13&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;zero&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;17&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;checksum&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;21&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;zero&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;25&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;stats stream length&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;29&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;zero&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;33&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;stats stream length - minus vector variable length
&lt;br&gt;The difference [25]-[33] is 64 for 1 vector (defined as off1).
&lt;br&gt;Each additional vector adds 24 bytes starting at byte position 41
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;37&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;zero&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td colSpan="4"&gt;Start of vector information, 24-bytes per vector&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;41&lt;/td&gt;

&lt;td&gt;1&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;system type id&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;42&lt;/td&gt;

&lt;td&gt;1&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;unkown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;43&lt;/td&gt;

&lt;td&gt;2&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;unkown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;45&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;user type id&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;49&lt;/td&gt;

&lt;td&gt;2&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;length&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;51&lt;/td&gt;

&lt;td&gt;1&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;Prec&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;52&lt;/td&gt;

&lt;td&gt;1&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;Scale&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;53&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;unknown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;57&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;unknown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;61&lt;/td&gt;

&lt;td&gt;2&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;unknown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;63&lt;/td&gt;

&lt;td&gt;2&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;unknown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td&gt;Some of the unknown fields should be for nullable, collation, etc&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td colSpan="4"&gt;Addition vectors if present&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+1&lt;sup&gt;*&lt;/sup&gt;&lt;/td&gt;

&lt;td&gt;9&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;Updated?, 9 byte datetime2?&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+10&lt;/td&gt;

&lt;td&gt;3&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;unknown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+13&lt;/td&gt;

&lt;td&gt;8&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;Rows&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+21&lt;/td&gt;

&lt;td&gt;8&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td align="left"&gt;Rows sampled&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+29&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte real&lt;/td&gt;

&lt;td align="left"&gt;Density - Header&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+33&lt;/td&gt;

&lt;td&gt;4x33=132&lt;/td&gt;

&lt;td&gt;4 byte real&lt;/td&gt;

&lt;td align="left"&gt;Density - vector, upto 33 values&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+165&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte int&lt;/td&gt;

&lt;td align="left"&gt;Steps (first copy)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+169&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte int&lt;/td&gt;

&lt;td align="left"&gt;Steps (second copy)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+173&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte int&lt;/td&gt;

&lt;td align="left"&gt;number of vectors&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+177&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte int&lt;/td&gt;

&lt;td align="left"&gt;Step size (in bytes)&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+181&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte real&lt;/td&gt;

&lt;td align="left"&gt;Average Key length - header&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+185&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte real&lt;/td&gt;

&lt;td align="left"&gt;Unfiltered rows&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+189&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte int&lt;/td&gt;

&lt;td align="left"&gt;unknown&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+193&lt;/td&gt;

&lt;td&gt;4x33=132&lt;/td&gt;

&lt;td&gt;4 byte real&lt;/td&gt;

&lt;td align="left"&gt;Average key length - vector&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td&gt;&amp;nbsp;&lt;/td&gt;

&lt;td&gt;Some fields may represent string index (bool), or filter expression&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+325&lt;/td&gt;

&lt;td&gt;8&lt;/td&gt;

&lt;td&gt;8 byte int&lt;/td&gt;

&lt;td align="left"&gt;unkown, values 0x11, 0x13 and 0x19 observed, 
&lt;br&gt;may determine the # of post histogram 8 bytes values starting at off1+341?&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+333&lt;/td&gt;

&lt;td&gt;8&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;8 byte 0?&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+341&lt;/td&gt;

&lt;td&gt;8&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;offset for value after histogram?&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+349&lt;/td&gt;

&lt;td&gt;8&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;another offset&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off1+357&lt;/td&gt;

&lt;td&gt;8&lt;/td&gt;

&lt;td&gt;0&lt;/td&gt;

&lt;td align="left"&gt;another offset if value of [off1+25] is 19 or more?&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td colSpan="4"&gt;more offsets if value of [off1+25] is 25 or more?&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td colSpan="4"&gt;Eventually, this sequence appears: 0x10001100 followed by three 4-byte real, 
&lt;br&gt;
a value in native type of the stat, and then
ending with 0x040000 &lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off2&lt;/td&gt;

&lt;td&gt;2&lt;/td&gt;

&lt;td&gt;0x10 - 16&lt;/td&gt;

&lt;td align="left"&gt;length of core columns
 determines the organization of histogram structures?--&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off2+2&lt;/td&gt;

&lt;td&gt;2&lt;/td&gt;

&lt;td&gt;17 or higher&lt;/td&gt;

&lt;td align="left"&gt;size of step, excluding 3 byte trailer&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off2+4&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte real&lt;/td&gt;

&lt;td align="left"&gt;Eq Rows&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off2+8&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte real&lt;/td&gt;

&lt;td align="left"&gt;Range Rows&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off2+12&lt;/td&gt;

&lt;td&gt;4&lt;/td&gt;

&lt;td&gt;4 byte real&lt;/td&gt;

&lt;td align="left"&gt;Avg Range Rows&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off2+16&lt;/td&gt;

&lt;td&gt;native length&lt;/td&gt;

&lt;td&gt;native type&lt;/td&gt;

&lt;td align="left"&gt;Range Hi Key&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off2+16+x&lt;/td&gt;

&lt;td&gt;3 byte&lt;/td&gt;

&lt;td&gt;0x040000&lt;/td&gt;

&lt;td align="left"&gt;step terminator?, x is the size of the type&lt;/td&gt;
&lt;/tr&gt;

&lt;tr align="right"&gt;
&lt;td&gt;off3&lt;/td&gt;

&lt;td&gt;?&lt;/td&gt;

&lt;td&gt;?&lt;/td&gt;

&lt;td align="left"&gt;additional info&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;sup&gt;*&lt;/sup&gt;off1 = value of 4(8) byte int at position [25] - value of [33]&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;sup&gt;**&lt;/sup&gt;off2 = off1 + 341 + 16 if value of [off1+325] is 0x11, or 24 if 0x13 or 0x19&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;sup&gt;***&lt;/sup&gt;off3 = off1 + 341 + value of 4(8) byte int at [off1+341]&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;


&lt;p&gt;
So far, for SQL Sever 2008 R2, I have only looked at fixed length not nullable statistics.
Variable length statistics has different organization, particularly in the histogram part.
String statistics may have extended information after the histogram, per new feature of SQL Server 2008?
&lt;/p&gt;

&lt;p&gt;Umachandar provides a SQL function for converting 4-byte binary to real or 8-byte binary float, 
and vice versa.&lt;/p&gt;


&lt;p&gt;Supporting SQL functions and procedures: 
&lt;p&gt;
The updated tools now has a stored procedure that accepts table and index (or column stat) as input parameters,
in addition to the original procedure that has the stats stream binary.&lt;/p&gt;

&lt;p&gt;
Updates on 
&lt;a href="http://www.qdpma.com\CBO\StatStream.html"&gt;QDPMA Stats Stream - Updated&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.qdpma.com\tools\decode_stat_stream2.zip"&gt;decoding stats stream - Updated&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.qdpma.com\tools\decode_stat_stream.zip"&gt;decoding stats stream&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;ps&lt;br&gt;An interesting fact is that it is not necessary for statistics to be highly accurate to be effective. Normally we are interested in distribution differences to shift the execution plan from one to another. The boundaries for this can be very wide. False statistics in certain circumstances might&amp;nbsp;guard against catastrophically bad&amp;nbsp;execution plans, example in out of bounds situations.&amp;nbsp;Another is in skewed distributions, but this should be handled&amp;nbsp;by other means, to ensure high and low distributions get different execution plans.&lt;/p&gt;</description></item><item><title>Connect Digest : 2011-09-22</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2011/09/22/connect-digest-2011-08-22.aspx</link><pubDate>Thu, 22 Sep 2011 11:31:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:37568</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;&lt;font size="4"&gt;Where is SYSDATE()? &lt;/font&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;I've talked about this one before, but due to a recent conversation about building a datetime value from separate date/time columns or variables, it may be worth bringing up again. When SQL Server 2008 introduced several new date/time types, they also added new built-in functions, like SYSDATETIME() and SYSDATETIMEOFFSET(). But they forgot about the same functions they forgot about back when GETDATE() and GETUTCDATE() were introduced: functions that return only date or only time. So we're still stuck with using implicit conversion to assign CURRENT_TIMESTAMP or SYSDATETIME() to a DATE or TIME variable, only to throw away the irrelevant part.&lt;br&gt;&lt;/p&gt;&lt;p style="margin-left:40px;"&gt;&lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/293333" title="http://connect.microsoft.com/SQLServer/feedback/details/293333" target="_blank"&gt;Connect #293333 : SQL 2008: Add sysdate()&lt;/a&gt; &lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;font size="4"&gt;Where is my built-in sequence?&lt;/font&gt;&lt;/p&gt;&lt;p&gt;In Denali we've seen the addition of SEQUENCE, an easy and more efficient way to centralize IDENTITY-like identifier values. But often we want to generate these numbers without incrementing a counter or running a bunch of inserts. To generate numbers on the fly in cases where I haven't been able to create my own numbers table, I often use master..spt_values.number, convoluted CTEs, or ROW_NUMBER() OVER () a cross join against arbitrary system tables. Why can't there be an in-memory table of numbers that we could reference, without all this extra scaffolding?&lt;br&gt;&lt;/p&gt;&lt;p style="margin-left:40px;"&gt;&lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/258733/add-a-built-in-table-of-numbers" title="http://connect.microsoft.com/SQLServer/feedback/details/258733/add-a-built-in-table-of-numbers" target="_blank"&gt;Connect #258733 : Add a built-in table of numbers&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;font size="4"&gt;&lt;br&gt;Why is CLR still so hard?&lt;/font&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;For ages I've wished it were easier to implement CLR solutions. RegEx is still not in SQL Server, and CLR has proven much more efficient than traditional XML, numbers table and looping methods in dealing with string splitting operations as well. And probably dozens of use cases that I haven't had the necessity to research myself. But it is often hard to help folks implement this on their own, never mind deal with strict DBA/management environments where CLR is still off-limits. Wouldn't it be great if it were far easier to implement C# code directly within a stored procedure or function, without all the overhead of deploying a DLL, creating an assembly, etc.&lt;br&gt;&lt;/p&gt;&lt;p style="margin-left:40px;"&gt;&lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/265266/add-server-side-compilation-ability-to-sql-clr" title="http://connect.microsoft.com/SQLServer/feedback/details/265266/add-server-side-compilation-ability-to-sql-clr" target="_blank"&gt;Connect #265266 : Add server-side compilation ability to SQL CLR&lt;/a&gt; &lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;font size="4"&gt;Will CASE always evaluate in left-to-right order?&lt;/font&gt;&lt;/p&gt;&lt;p&gt;The documentation for CASE states that the WHEN expressions are evaluated in textual order. Most people infer from this that any subsequent expression will not be evaluated, but there have been several cases where this has not been true. While I didn't find this particular manifestation of the bug (that was Itzik), and while I do hope they correct this scenario, I'm much more interested in clarity in the documentation so that people know whether or not they can rely on this behavior to &lt;span style="font-weight:bold;font-style:italic;"&gt;always&lt;/span&gt; be true.&lt;/p&gt;&lt;p style="margin-left:40px;"&gt;&lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/690017/case-coalesce-wont-always-evaluate-in-textual-order" title="http://connect.microsoft.com/SQLServer/feedback/details/690017/case-coalesce-wont-always-evaluate-in-textual-order" target="_blank"&gt;Connect #690017 : CASE / COALESCE won't always evaluate in textual order&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;font size="4"&gt;Why can't I have partition-level stats?&lt;/font&gt;&lt;/p&gt;&lt;p&gt;Partitioning is great, but if you have disproportionate partition sizes, or a lot of partitions, statistics can become a real challenge. Consider a table with 100 million rows, but only 1 million rows in the currently active partition. If you want to update the statistics on the active partition, your probable approach today is to update statistics on the whole table. (The same type of issue exists with filtered indexes - auto statistics updates kicks in based on the row count in the table, not the subset in the index.)&lt;/p&gt;&lt;div style="margin-left:40px;"&gt;&lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/468517/update-statistics-at-the-partition-level" title="http://connect.microsoft.com/SQLServer/feedback/details/468517/update-statistics-at-the-partition-level" target="_blank"&gt;Connect #468517 : Update Statistics at the partition level&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/328093/statistics-are-saved-on-table-level-and-not-on-partition-level" title="http://connect.microsoft.com/SQLServer/feedback/details/328093/statistics-are-saved-on-table-level-and-not-on-partition-level" target="_blank"&gt;Connect #328093 : Statistics are saved on table level and not on partition level&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/457024/update-statistics-top-100-percent-and-sort-warnings" title="http://connect.microsoft.com/SQLServer/feedback/details/457024/update-statistics-top-100-percent-and-sort-warnings" target="_blank"&gt;Connect #457024 : Update statistics, top 100 percent and Sort warnings&lt;/a&gt;&lt;br&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;br&gt;&lt;/p&gt;</description></item><item><title>How to Find the Statistics Used to Compile an Execution Plan</title><link>http://sqlblog.com/blogs/paul_white/archive/2011/09/20/how-to-find-the-statistics-used-to-compile-an-execution-plan.aspx</link><pubDate>Tue, 20 Sep 2011 15:22:15 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:38595</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;In this post, I show you how to determine exactly which statistics objects were used by the query optimizer to produce an execution plan.&lt;/p&gt;  &lt;h3&gt;Trace Flags&lt;/h3&gt;  &lt;p&gt;We will need three undocumented trace flags.&amp;#160; The first one (&lt;strong&gt;3604&lt;/strong&gt;) is well-known – it redirects trace output to the client so it appears in the SSMS messages tab.&lt;/p&gt;  &lt;p&gt;The second trace flag is &lt;strong&gt;9292&lt;/strong&gt;.&amp;#160; With this enabled, we get a report of statistics objects which are considered ‘interesting’ by the query optimizer when compiling, or recompiling the query in question.&amp;#160; For potentially useful statistics, just the header is loaded.&lt;/p&gt;  &lt;p&gt;The third trace flag is &lt;strong&gt;9204&lt;/strong&gt;.&amp;#160; With this enabled, we see the ‘interesting’ statistics which end up being fully loaded and used to produce cardinality and distribution estimates for some plan alternative or other.&amp;#160; Again, this only happens when a plan is compiled or recompiled – not when a plan is retrieved from cache.&lt;/p&gt;  &lt;p&gt;You can enable and disable these flags with the usual DBCC TRACEON and TRACEOFF commands, but it is also possible to enable them just for a particular statement using the undocumented QUERYTRACEON query hint (demonstrated below).&lt;/p&gt;  &lt;h3&gt;Sample Query&lt;/h3&gt;  &lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:400px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; FREEPROCCACHE&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    p.Name,&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    total_quantity = &lt;span style="color:#0000ff;"&gt;SUM&lt;/span&gt;(th.Quantity)&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; AdventureWorks.Production.Product &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; AdventureWorks.Production.TransactionHistory &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; th &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    th.ProductID = p.ProductID&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    th.ActualCost &amp;gt;= $5.00&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt; p.Color = N&lt;span style="color:#006080;"&gt;'Red'&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;GROUP&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    p.Name&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;BY&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    p.Name&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&lt;span style="color:#0000ff;"&gt;OPTION&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    QUERYTRACEON 3604,&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    QUERYTRACEON 9292,&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    QUERYTRACEON 9204&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;)&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The DBCC FREEPROCCACHE is just there to empty the plan cache so we get a compilation.&amp;#160; You can also evict the current plan from cache if you know its handle (SQL Server 2008) or use a RECOMPILE query hint.&amp;#160; Using RECOMPILE is often convenient, but you may get a different plan compared to that obtained without the hint.&amp;#160; Note that compiling the query is enough – we do not need to execute the query; simply requesting an ‘estimated plan’ will do.&amp;#160; It doesn’t hurt to run it either though, just to be clear.&lt;/p&gt;

&lt;h3&gt;Sample Output&lt;/h3&gt;

&lt;div style="border-bottom:silver 1px solid;text-align:left;border-left:silver 1px solid;padding-bottom:4px;line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;padding-left:4px;width:97.5%;padding-right:4px;font-family:'Courier New', courier, monospace;direction:ltr;max-height:500px;font-size:8pt;overflow:auto;border-top:silver 1px solid;cursor:text;border-right:silver 1px solid;padding-top:4px;" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats header loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.Product, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 1, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: ProductID, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.Product, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 1, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: ProductID, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats header loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.Product, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 3, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: Name, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.Product, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 3, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: Name, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats header loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.Product, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 11, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: Color, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.Product, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 11, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: Color, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats header loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.TransactionHistory, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 2, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: ProductID, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.TransactionHistory, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 2, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: ProductID, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats header loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.TransactionHistory, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 5, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: ActualCost, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;Stats loaded: &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    DbName: AdventureWorks, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ObjName: AdventureWorks.Production.TransactionHistory, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    IndexId: 5, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:#f4f4f4;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    ColumnName: ActualCost, &lt;/pre&gt;


    &lt;pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;background-color:white;margin:0em;border-left-style:none;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;border-right-style:none;font-size:8pt;overflow:visible;padding-top:0px;"&gt;    EmptyTable: &lt;span style="color:#0000ff;"&gt;FALSE&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;There’s no sign of an official way to get this very useful information in Denali, despite it being requested many times over the years.&amp;#160; Trace flag 9204 works at least as far back as SQL Server 2005.&amp;#160; Both 92xx flags work in 2008, R2, and Denali CTP 3.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

&lt;p&gt;© 2011 Paul White&lt;/p&gt;

&lt;p&gt;&lt;a href="mailto:SQLkiwi@gmail.com"&gt;SQLkiwi@gmail.com&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://twitter.com/SQL_Kiwi"&gt;@SQL_Kiwi&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Ola Hallengren adds STATISTICS support to his solution</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2010/12/13/ola-hallengren-adds-statistics-support-to-his-solution.aspx</link><pubDate>Mon, 13 Dec 2010 19:56:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:31576</guid><dc:creator>AaronBertrand</dc:creator><description>
&lt;p&gt;Last week, Ola published a very useful update to his &lt;a href="http://ola.hallengren.com/Versions.html" title="http://ola.hallengren.com/Versions.html" target="_blank"&gt;Backup, Integrity Check and Index Optimization scripts&lt;/a&gt;: the solution now supports updating statistics.&amp;nbsp; There are several options, such as only updating when the data has been modified and using the RESAMPLE and NORECOMPUTE options.&amp;nbsp; An example call:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;EXEC&lt;/font&gt; dbo.IndexOptimize
&lt;font color="#666666"&gt;    @Databases                  = &lt;font color="red"&gt;'USER_DATABASES'&lt;/font&gt;,
    @FragmentationHigh_LOB      = &lt;font color="red"&gt;'INDEX_REBUILD_OFFLINE'&lt;/font&gt;,
    @FragmentationHigh_NonLOB   = &lt;font color="red"&gt;'INDEX_REBUILD_ONLINE'&lt;/font&gt;,
    @FragmentationMedium_LOB    = &lt;font color="red"&gt;'INDEX_REORGANIZE_STATISTICS_UPDATE'&lt;/font&gt;,
    @FragmentationMedium_NonLOB = &lt;font color="red"&gt;'INDEX_REORGANIZE_STATISTICS_UPDATE'&lt;/font&gt;,
    @FragmentationLow_LOB       = &lt;font color="red"&gt;'STATISTICS_UPDATE'&lt;/font&gt;,
    @FragmentationLow_NonLOB    = &lt;font color="red"&gt;'STATISTICS_UPDATE'&lt;/font&gt;,
    @UpdateColumnStatistics     = &lt;font color="red"&gt;'Y'&lt;/font&gt;,
    @OnlyModifiedStatistics     = &lt;font color="red"&gt;'Y'&lt;/font&gt;;&lt;/font&gt;
&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you haven't already tried Ola's maintenance solution, I highly suggest you &lt;a href="http://ola.hallengren.com/Versions.html" title="http://ola.hallengren.com/Versions.html" target="_blank"&gt;check it out&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Fooling the Query Optimizer</title><link>http://sqlblog.com/blogs/ben_nevarez/archive/2010/01/13/fooling-the-query-optimizer.aspx</link><pubDate>Thu, 14 Jan 2010 04:57:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21032</guid><dc:creator>Ben Nevarez</dc:creator><description>&lt;P class=MsoNormal&gt;Did you ever wanted to know which execution plans the Query Optimizer would generate for your queries should your tables have millions of records? You can actually generate those plans by using the undocumented ROWCOUNT and PAGECOUNT options of the UPDATE STATISTICS statement. These options can be used on small or empty tables and can be helpful&amp;nbsp;for testing&amp;nbsp;in some scenarios where you may not want to spent time or disk space creating big tables.&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;By using this method you are tricking the Query Optimizer as it will generate execution plans using cardinality estimations as if the table really had millions of records. Note that this option, available since SQL Server 2005, only helps in creating the execution plan for your queries. Actually running the query will use the real data and of course will execute faster than a table with millions of records.&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;UPDATE STATISTICS WITH ROWCOUNT, PAGECOUNT does not change the table statistics, only the counter of number of rows and pages of a table. But the Query Optimizer uses this information to estimate the cardinality of queries as I will show later. Also keep in mind that these are undocumented and unsupported options and should not be used in any production environment.&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;Let us see an example. Run the following query to create a new table on the AdventureWorks database&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;*&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;into&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;Address &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;from&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; Person&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;Address &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;Inspect the number of rows by running the following queries. It must show 19,614 rows.&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;*&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;from&lt;/SPAN&gt; &lt;SPAN style="COLOR:green;"&gt;sys&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;partitions &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;where&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&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:fuchsia;"&gt;object_id&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'dbo.Address'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal style="mso-layout-grid-align:none;"&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal style="mso-layout-grid-align:none;"&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;*&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;from&lt;/SPAN&gt; &lt;SPAN style="COLOR:green;"&gt;sys&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;dm_db_partition_stats 
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;where&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&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:fuchsia;"&gt;object_id&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'dbo.Address'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal&gt;Run the following query&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;*&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;from&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;Address &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;where&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; city &lt;SPAN style="COLOR:gray;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR:red;"&gt;'London' &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal&gt;Running this query will create new statistics for the city column and will show the following plan. Note that the estimated number of rows is 434 and it is using a simple Table Scan operator&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="TEXT-ALIGN:center;" align=center&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="http://sqlblog.com/blogs/ben_nevarez/clip_image002_5DF90560.jpg"&gt;&lt;IMG title=clip_image002 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:inline;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=142 alt=clip_image002 src="http://sqlblog.com/blogs/ben_nevarez/clip_image002_thumb_1ADEBA63.jpg" width=334 border=0&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;We can see where the Query Optimizer is getting the estimated number of rows by inspecting the statistics object. Run this query to see the name of the statistics object&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;*&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;from&lt;/SPAN&gt; &lt;SPAN style="COLOR:green;"&gt;sys&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:green;"&gt;stats &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;where&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&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:fuchsia;"&gt;object_id&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'dbo.Address'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;) &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal&gt;Then use the displayed statistics object name in the following statement (the name may be different in your case)&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;dbcc&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:blue;"&gt;show_statistics&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR:red;"&gt;'dbo.Address'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;,&lt;/SPAN&gt; _WA_Sys_00000004_46136164&lt;SPAN style="COLOR:gray;"&gt;) &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal&gt;By looking at the histogram you can find the value 434 on EQ_ROWS for the RANGE_HI_KEY value ‘London’ (Statistics and histograms are explained on previous posts in this blog)&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;A href="http://sqlblog.com/blogs/ben_nevarez/clip_image004_08C42013.jpg"&gt;&lt;IMG title=clip_image004 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:block;FLOAT:none;MARGIN-LEFT:auto;BORDER-LEFT:0px;MARGIN-RIGHT:auto;BORDER-BOTTOM:0px;" height=93 alt=clip_image004 src="http://sqlblog.com/blogs/ben_nevarez/clip_image004_thumb_76A985C2.jpg" width=540 border=0&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;Now run the UPDATE STATISTICS WITH ROWCOUNT, PAGECOUNT (you can specify any other value for rowcount and pagecount)&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;update&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:blue;"&gt;statistics&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;Address&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;with&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;rowcount&lt;/SPAN&gt; &lt;SPAN style="COLOR:gray;"&gt;=&lt;/SPAN&gt; 1000000&lt;SPAN style="COLOR:gray;"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;pagecount&lt;/SPAN&gt; &lt;SPAN style="COLOR:gray;"&gt;=&lt;/SPAN&gt; 100000 &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal&gt;If you inspect the number of rows again from sys.partitions or sys.dm_db_partition_stats, as shown previously, it will now show 1,000,000 rows. sys.dm_db_partition_stats also shows the new number of pages. Clear the plan cache and run the query again&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;dbcc&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; freeproccache &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;go &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;*&lt;/SPAN&gt; &lt;SPAN style="COLOR:blue;"&gt;from&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;Address &lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;where&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; city &lt;SPAN style="COLOR:gray;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR:red;"&gt;'London' &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal&gt;Note that the estimated number of rows has changed from 434 to 22,127.1 and that a different plan was generated using this new cardinality estimation. The Query Optimizer decided to parallelize this plan. But this is a very simple query, more dramatic plan changes can happen with more complex queries.&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;A href="http://sqlblog.com/blogs/ben_nevarez/clip_image006_3DC0D532.jpg"&gt;&lt;IMG title=clip_image006 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:block;FLOAT:none;MARGIN-LEFT:auto;BORDER-LEFT:0px;MARGIN-RIGHT:auto;BORDER-BOTTOM:0px;" height=135 alt=clip_image006 src="http://sqlblog.com/blogs/ben_nevarez/clip_image006_thumb_637B0FB6.jpg" width=508 border=0&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;After execution the actual number of rows obviously is still is 434 but the Query Optimizer is not able to see this value.&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;If you look at the statistics object again, using DBCC SHOW_STATISTICS as shown before, the histogram has not changed. One way to obtain the estimated number of rows shown in the new execution plan is calculating the percentage or fraction of rows for the value ‘London’ from the statistics sample, which in this case is 19,614, as shown on the header of the statistics object. So the fraction is 434 / 19,614 or 022127052. Then obtain the same percentage from the new “current” number of rows which is 1,000,000 calculated as 1,000,000 * 0.022127052 and we get 22,127.1 which is the estimated number of rows displayed in the plan.&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;Finally, drop the table you just created&lt;/P&gt;
&lt;P class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE:10pt;COLOR:blue;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt;drop&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE:10pt;FONT-FAMILY:'Courier New';mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:blue;"&gt;table&lt;/SPAN&gt; dbo&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;Address &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;</description></item><item><title>Lies, Damned Lies and Statistics – Part III (SQL Server 2008)</title><link>http://sqlblog.com/blogs/elisabeth_redei/archive/2009/12/17/lies-damned-lies-and-statistics-part-iii-sql-server-2008.aspx</link><pubDate>Thu, 17 Dec 2009 11:14:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:20058</guid><dc:creator>Elisabeth Redei</dc:creator><description>&lt;P&gt;In previous posts (&lt;A href="http://sqlblog.com/blogs/elisabeth_redei/archive/2009/08/10/lies-damned-lies-and-statistics-part-i.aspx"&gt;http://sqlblog.com/blogs/elisabeth_redei/archive/2009/08/10/lies-damned-lies-and-statistics-part-i.aspx&lt;/A&gt;&amp;nbsp; and &lt;A title=http://sqlblog.com/blogs/elisabeth_redei/archive/2009/03/01/lies-damned-lies-and-statistics-part-ii.aspx href="http://sqlblog.com/blogs/elisabeth_redei/archive/2009/03/01/lies-damned-lies-and-statistics-part-ii.aspx)I"&gt;http://sqlblog.com/blogs/elisabeth_redei/archive/2009/03/01/lies-damned-lies-and-statistics-part-ii.aspx)I&lt;/A&gt; have talked about the performance problems that may surface because of low statistics sample rate on large tables. &lt;/P&gt;
&lt;P&gt;One of the things I wrote about are issues with naturally skewed data in an indexed column. In this context, skewed data means that some values are represented more than others. One example is a "Country" column in an Address table where an international company is more likely to have more customers or employees in the U.K then let’s say Finland. A “Lastname" column in a Customers table would typically have skewed data as well – there are a lot more Smiths around than Rédei for example . Your SalesOrder table is bound to have more entries for some customers than for others as well. &lt;/P&gt;
&lt;P&gt;The solutions I listed -&amp;nbsp; that have been listed by many before me - were never really appealing to me and consequently I was never good at persuading anyone to use them and this has always annoyed me. &lt;/P&gt;
&lt;P&gt;SQL Server 2008 offers a great solution to this problem - filtered statistics. As you probably know, SQL Server 2008 offers you the ability to create filtered indexes, i.e. the index is created on a horizontal partition of the column. But did you know you can create filtered statistics on an unfiltered index? &lt;/P&gt;
&lt;P&gt;This solution is much more appealing simply because you implement it at the index level rather than individual query level. In addition, your data will change and this solution can easily adopt to those changes. Another attractive perk is that it would be quite difficult to mess things up with this approach – unless you make a serious effort to! &lt;/P&gt;
&lt;P&gt;Jason Massie has touched on the subject on SQLServerpedia, &lt;A href="http://sqlserverpedia.com/blog/sql-server-bloggers/filtered-stats-to-counter-data-skew-issues/"&gt;http://sqlserverpedia.com/blog/sql-server-bloggers/filtered-stats-to-counter-data-skew-issues/&lt;/A&gt;, but I wanted to see how it worked if you filtered on ranges of data covering all values in the index. &lt;/P&gt;
&lt;P&gt;Again, I have a table, test1, with with values in the c2 column that ranges from 1 to 500.I have created a non-clustered index Idx1 on column c2. &lt;/P&gt;
&lt;P&gt;In each bucket, I have about 2 400 rows, but c2 = 45 returns 0 rows: &lt;/P&gt;
&lt;P&gt;SELECT c2, COUNT(*) as NumRows &lt;BR&gt;FROM test1 &lt;BR&gt;group by c2 &lt;BR&gt;order by c2 &lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/elisabeth_redei/Fig1_NumRows2400_50C1732A.jpg"&gt;&lt;IMG title=Fig1_NumRows2400 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:inline;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=204 alt=Fig1_NumRows2400 src="http://sqlblog.com/blogs/elisabeth_redei/Fig1_NumRows2400_thumb_6D0EB54A.jpg" width=141 border=0&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;DBCC SHOW_STATISTICS ('test1','idx1') WITH HISTOGRAM reveals that the value 45 has fallen between the chairs, even after an UPDATE STATISTICS test1 WITH FULLSCAN: &lt;/P&gt;
&lt;P&gt;DBCC SHOW_STATISTICS ('test1','idx1') WITH HISTOGRAM:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/elisabeth_redei/Fig3_ShowStatistics2400_256CB656.jpg"&gt;&lt;IMG title=Fig3_ShowStatistics2400 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:inline;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=139 alt=Fig3_ShowStatistics2400 src="http://sqlblog.com/blogs/elisabeth_redei/Fig3_ShowStatistics2400_thumb_7200CD2C.jpg" width=466 border=0&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;DBCC SHOW_STATISTICS ('test1','idx1') WITH STAT_HEADER shows that all 1 190 402 rows are represented by 200 “buckets” or ranges (the Steps column):&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/elisabeth_redei/Fig3a_ShowStatisticsStatHeader2400_41A9D2A9.jpg"&gt;&lt;IMG title=Fig3a_ShowStatisticsStatHeader2400 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:inline;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=41 alt=Fig3a_ShowStatisticsStatHeader2400 src="http://sqlblog.com/blogs/elisabeth_redei/Fig3a_ShowStatisticsStatHeader2400_thumb_5024CECE.jpg" width=691 border=0&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consequently, the following query gives me a table scan because the statistics that the optimizer have at hand, implies that 2 400 rows will be returned: &lt;/P&gt;
&lt;P&gt;SELECT c1, c2 FROM test1 WHERE c2 = 45 &lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/elisabeth_redei/Fig4_ExecPlan2400_2588EDF1.jpg"&gt;&lt;IMG title=Fig4_ExecPlan2400 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:inline;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=184 alt=Fig4_ExecPlan2400 src="http://sqlblog.com/blogs/elisabeth_redei/Fig4_ExecPlan2400_thumb_4F3C4317.jpg" width=559 border=0&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;So the problem is not the sample rate; SQL Server has looked at all values in the index when it was building the statistics – it’s just that the histogram in this case is to coarse for the data it represents.&lt;/P&gt;
&lt;H2&gt;&lt;/H2&gt;
&lt;H2&gt;How can I solve this problem?&lt;/H2&gt;
&lt;P&gt;The trick is to increase the number of ranges that represent the data so rather than having 200 to represent 1 200 000 rows you can have 600 or a 1000.&lt;/P&gt;
&lt;P&gt;To start with, I need to know my domain of values: &lt;/P&gt;
&lt;P&gt;SELECT MIN(c2) AS minVal, AVG(c2) avgVal, MAX(c2) maxVal FROM test1 &lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/elisabeth_redei/Fig4_Ranges_430AA323.jpg"&gt;&lt;IMG title=Fig4_Ranges style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:inline;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=88 alt=Fig4_Ranges src="http://sqlblog.com/blogs/elisabeth_redei/Fig4_Ranges_thumb_59E9749F.jpg" width=291 border=0&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;I have only some 1 200 000 rows in this table, so I am going to go ahead and try with 3 ranges (ideally you should have an idea how many rows are in each range): &lt;/P&gt;
&lt;P&gt;CREATE STATISTICS testStatLow &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ON test1 (c2) &lt;BR&gt;WHERE c2 &amp;lt; 200 &lt;BR&gt;GO &lt;/P&gt;
&lt;P&gt;CREATE STATISTICS testStatMed &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ON test1 (c2) &lt;BR&gt;WHERE c2 &amp;gt; 200 AND c2 &amp;lt; 400 &lt;BR&gt;GO &lt;/P&gt;
&lt;P&gt;CREATE STATISTICS testStatHi &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ON test1 (c2) &lt;BR&gt;WHERE c2 &amp;gt; 400 &lt;BR&gt;GO &lt;/P&gt;
&lt;P&gt;I am then going to update the statistics with some sample rate to see if I - in addition to getting better query plans – I can save time on my maintenance job that updates the statistics for all tables: &lt;/P&gt;
&lt;P&gt;UPDATE STATISTICS test1 WITH SAMPLE 20 percent &lt;/P&gt;
&lt;P&gt;And then look at the statistics:&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;dbcc show_statistics ('test1','testStatLow') with STAT_HEADER &lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/elisabeth_redei/Fig7_Stat_header2400_0C009F1D.jpg"&gt;&lt;IMG title=Fig7_Stat_header2400 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:inline;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=47 alt=Fig7_Stat_header2400 src="http://sqlblog.com/blogs/elisabeth_redei/Fig7_Stat_header2400_thumb_59D94ED2.jpg" width=653 border=0&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Rows sampled vs. Rows shows that a little bit more than half of the rows were scanned (remember that SQL Server can decide to increase the sample rate if it decides that it is necessary to get a good representation of the data). The data in the column has been divided into yet another 194 buckets/ranges, drastically reducing the risk for problems with skewed data. &lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Let’s run the query again to see if my new filtered statistics makes a difference: &lt;/P&gt;
&lt;P&gt;SELECT c1, c2 FROM test1 WHERE c2 = 45&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/elisabeth_redei/Fig6_ExecPlan2400_7ABC9BAC.jpg"&gt;&lt;IMG title=Fig6_ExecPlan2400 style="BORDER-RIGHT:0px;BORDER-TOP:0px;DISPLAY:inline;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height=211 alt=Fig6_ExecPlan2400 src="http://sqlblog.com/blogs/elisabeth_redei/Fig6_ExecPlan2400_thumb_36B8B795.jpg" width=471 border=0&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;There we go, the optimizer chose to do an index seek to find my non-existing row. Much better!&lt;/EM&gt;&lt;/P&gt;
&lt;H2&gt;What about maintenance for the statistics?&lt;/H2&gt;
&lt;P&gt;My idea was that perhaps I can get away with a lower sample rate now that the index has been logically divided into several histograms. And as you just saw, I can for this particular scenario. However, after an update, I ran:&lt;/P&gt;
&lt;P&gt;UPDATE STATISTICS test1 WITH SAMPLE 20 PERCENT &lt;/P&gt;
&lt;P&gt;… which took about 6 seconds whereas &lt;/P&gt;
&lt;P&gt;UPDATE STATISTICS test1 WITH FULLSCAN&lt;/P&gt;
&lt;P&gt;.. took about 1 second.&lt;/P&gt;
&lt;P&gt;So in my somewhat constructed scenario, my gain is with the performance of the query but I am not saving on the precious time I have available for doing index maintenance.&lt;/P&gt;
&lt;P&gt;Hm… I can sense an upcoming blog on the subject of the Long and Winding Shortcut!&lt;/P&gt;</description></item><item><title>For want of a nail</title><link>http://sqlblog.com/blogs/michael_zilberstein/archive/2009/12/07/19582.aspx</link><pubDate>Tue, 08 Dec 2009 00:55:55 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:19582</guid><dc:creator>mz1313</dc:creator><description>&lt;p&gt;&lt;i&gt;For want of a nail the shoe was lost.     &lt;br /&gt;&lt;/i&gt;&lt;i&gt;For want of a shoe the horse was lost.     &lt;br /&gt;&lt;/i&gt;&lt;i&gt;For want of a horse the rider was lost.     &lt;br /&gt;&lt;/i&gt;&lt;i&gt;For want of a rider the battle was lost.     &lt;br /&gt;&lt;/i&gt;&lt;i&gt;For want of a battle the kingdom was lost.     &lt;br /&gt;&lt;/i&gt;&lt;i&gt;And all for the want of a horseshoe nail.&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I'm now in the middle of severity A case that is best depicted by this rhyme. While Microsoft engineers look for a root cause, I'll describe here how small bug can &amp;quot;kill&amp;quot; strong server. The story begins when for some unknown reason &amp;quot;rows&amp;quot; column in sys.partitions DMV begins to show 4.6 billion rows for clustered index of one of the entities tables in my datawarehouse database. COUNT(*) on the table returns 1 million rows.&lt;/p&gt;  &lt;p&gt;Second step - daily job that executes sp_updatestats stored procedure. This procedure updates statistics in the database using sample data . Let's say, I randomly took 5% of table's data and calculated number of unique values for Column1 - n values. So total unique values of Column1 in my table is 20*n. But how do I know, how many rows per unique value of Column1 are there in the table? Very simple: SQL Server just takes rows column from sys.partitions and divides by our 20*n number.&lt;/p&gt;  &lt;p&gt;Third step - query: it joins between our Table and another one WHERE Column1 = x. Optimizer compiles the query and builds execution plan based on existing statistics (e.g. 4 billion rows in the table). Expected number of rows from our table is millions - so Optimizer chooses HASH JOIN as the best way to perform JOIN operation.&lt;/p&gt;  &lt;p&gt;Now that Optimizer made a decision about execution plan, it asks for a memory quota for query execution. Based on the estimation about millions of rows. Remember that memory quota for query execution isn't taken from data buffer cache, so on 32 bit systems we can't utilize too much memory. For example, I executed single query as described earlier and while it was running checked memory usage of my session.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/WindowsLiveWriter/Forwantofanail_27E1/image_2.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;border-top:0px;border-right:0px;" border="0" alt="image" src="http://sqlblog.com/blogs/michael_zilberstein/WindowsLiveWriter/Forwantofanail_27E1/image_thumb.png" width="752" height="170" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So we have 252Mb granted while less than 1Mb used. Total available memory is 1.6Gb, so you can calculate yourself how many concurrent queries system supports at its current state. It is exactly the situation we had: 5 sessions were running and 65 were waiting for memory grant (if you monitor wait types - there will be a lot of RESOURCE_SEMAPHORE).&lt;/p&gt;  &lt;p&gt;P.S. DBCC UPDATEUSAGE fixes the initial problem. So while Microsoft engineers look for the reason why sys.partitions went wild, I monitor it in order to fix the problem before it &amp;quot;kills&amp;quot; the server.&lt;/p&gt;</description></item><item><title>Rebuilding Indexes vs. Updating Statistics</title><link>http://sqlblog.com/blogs/ben_nevarez/archive/2009/10/06/rebuilding-indexes-vs-updating-statistics.aspx</link><pubDate>Tue, 06 Oct 2009 07:04:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17301</guid><dc:creator>Ben Nevarez</dc:creator><description>&lt;p&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;One of the questions I was asked recently while speaking at user groups, was regarding the order that jobs like rebuilding indexes or updating statistics should be performed as part of the database maintenance activities. Then I started writing this post about this topic on the weekend but was interrupted several times, including one of them to watch the premiere on VH1 of the movie Anvil: The Story of Anvil.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;In general, the order should not matter, at least if you carefully consider these important points:&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;1) By default, the UPDATE STATISTICS statement uses only a sample of records of the table. Using UPDATE STATISTICS WITH FULLSCAN will scan the entire table. &lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;2) By default, the UPDATE STATISTICS statement updates both index and column statistics. Using the COLUMNS option will update column statistics only. Using the INDEX option will update index statistics only.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;3) Rebuilding an index, for example by using ALTER INDEX … REBUILD, will also update index statistics with the equivalent of using WITH FULLSCAN. Rebuilding indexes does not update column statistics.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;4) Reorganizing an index, for example using ALTER INDEX … REORGANIZE, does not update any statistics.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;So depending on your maintenance jobs and scripts several scenarios can exist.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;The simplest scenario is if you want to rebuild all the indexes and update all the statistics. As mentioned before, if you rebuild all your indexes then all the index statistics will also be updated by scanning all the rows on the table. Then you just need to update your column statistics by running UPDATE STATISTICS WITH FULLSCAN, COLUMNS. Since the first job only updates index statistics and the second one only updates column statistics, it does not matter which one you execute first.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;Some other more complicated scenarios include when you have a job which rebuilds your indexes depending on their fragmentation level. In these cases perhaps you want to update only those index statistics that were not touched by the index rebuild job, plus all the column statistics. &lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;Of course, the worst case scenario would be if you first rebuild your indexes, which also updates the index statistics by scanning the entire table, and later you run UPDATE STATISTICS using the default values, which again updates the index statistics but this time with a default sample. Not only are you updating your index statistics twice but you are overwriting the better of the two choices.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;Let me show you how these commands work with some examples using the AdventureWorks database. Create a new table dbo.SalesOrderDetail&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;select&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:gray;"&gt;*&lt;/span&gt; &lt;span style="color:blue;"&gt;into&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail       &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;from&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; sales&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail       &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;The next query uses the sys.stats catalog view and shows that there are no statistics objects for the new table. &lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;select&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; name&lt;span style="color:gray;"&gt;,&lt;/span&gt; auto_created&lt;span style="color:gray;"&gt;,&lt;/span&gt; &lt;span style="color:fuchsia;"&gt;stats_date&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; stats_id&lt;span style="color:gray;"&gt;)&lt;/span&gt; &lt;span style="color:blue;"&gt;as&lt;/span&gt; update_date &lt;span style="color:blue;"&gt;from&lt;/span&gt; &lt;span style="color:green;"&gt;sys&lt;/span&gt;&lt;span style="color:gray;"&gt;.&lt;/span&gt;&lt;span style="color:green;"&gt;stats&lt;/span&gt;       &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;where&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&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:fuchsia;"&gt;object_id&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:red;"&gt;'dbo.SalesOrderDetail'&lt;/span&gt;&lt;span style="color:gray;"&gt;)       &lt;p&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;Use this query again to inspect the status of the statistics after each of the following commands. Now run the following query&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;select&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:gray;"&gt;*&lt;/span&gt; &lt;span style="color:blue;"&gt;from&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail       &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;where&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; SalesOrderID &lt;span style="color:gray;"&gt;=&lt;/span&gt; 43670 &lt;span style="color:gray;"&gt;and&lt;/span&gt; OrderQty &lt;span style="color:gray;"&gt;=&lt;/span&gt; 1      &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;Use the previous sys.stats query to verify that two statistics objects were created, one for the SalesOrderID column and another one for the OrderQty column (they both have names starting with _WA_Sys as shown in the next figure). Now create the following index and again run the query to verify that a new statistics object for the ProductID column has been created. Notice the value of the auto_created column which tells if the statistics were created by the query optimizer.&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;create&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;index&lt;/span&gt; ix_ProductID &lt;span style="color:blue;"&gt;on&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail&lt;span style="color:gray;"&gt;(&lt;/span&gt;ProductID&lt;span style="color:gray;"&gt;)&lt;/span&gt;       &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="text-align:center;" align="center"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal" style="text-align:center;" align="center"&gt;&lt;a href="http://sqlblog.com/blogs/ben_nevarez/clip_image002_4C71EA23.jpg"&gt;&lt;img title="clip_image002" style="border-right:0px;border-top:0px;display:inline;border-left:0px;border-bottom:0px;" height="78" alt="clip_image002" src="http://sqlblog.com/blogs/ben_nevarez/clip_image002_thumb_5A725C90.jpg" width="399" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal"&gt;Run the next command to update the column statistics only. You can validate that only the column statistics were updated by looking at the update_date column which uses the STATS_DATE function to display the last date the statistics were updated. &lt;/p&gt;  &lt;p class="MsoNormal"&gt;   &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;update&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;statistics&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail &lt;span style="color:blue;"&gt;with&lt;/span&gt; &lt;span style="color:blue;"&gt;fullscan&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt; &lt;span style="color:green;"&gt;columns       &lt;p&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:green;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="text-align:center;mso-layout-grid-align:none;" align="center"&gt;&lt;span style="font-size:10pt;color:green;font-family:'Courier New';mso-no-proof:yes;"&gt;&lt;a href="http://sqlblog.com/blogs/ben_nevarez/clip_image004_53814C8A.jpg"&gt;&lt;img title="clip_image004" style="border-right:0px;border-top:0px;display:inline;border-left:0px;border-bottom:0px;" height="79" alt="clip_image004" src="http://sqlblog.com/blogs/ben_nevarez/clip_image004_thumb_286AE1F5.jpg" width="406" border="0" /&gt;&lt;/a&gt;      &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:green;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:green;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;This command will do the same for the index statistics&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;update&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;statistics&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail &lt;span style="color:blue;"&gt;with&lt;/span&gt; &lt;span style="color:blue;"&gt;fullscan&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt; &lt;span style="color:blue;"&gt;index       &lt;p&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:green;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;These commands will update both index and column statistics&lt;span style="font-size:10pt;color:green;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;update&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;statistics&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail &lt;span style="color:blue;"&gt;with&lt;/span&gt; &lt;span style="color:blue;"&gt;fullscan       &lt;p&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;update&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;statistics&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail &lt;span style="color:blue;"&gt;with&lt;/span&gt; &lt;span style="color:blue;"&gt;fullscan&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt; &lt;span style="color:gray;"&gt;all       &lt;p&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:gray;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;See how an index rebuild only updates index statistics&lt;span style="font-size:10pt;color:gray;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:gray;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;alter&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;index&lt;/span&gt; ix_ProductID &lt;span style="color:blue;"&gt;on&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail &lt;span style="color:blue;"&gt;rebuild       &lt;p&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;Here you can verify that reorganizing an index does not update statistics&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;alter&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;index&lt;/span&gt; ix_ProductID&lt;span style="mso-spacerun:yes;"&gt;&amp;#160; &lt;/span&gt;&lt;span style="color:blue;"&gt;on&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail &lt;span style="color:blue;"&gt;reorganize       &lt;p&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;Finally, remove the table you have just created&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;     &lt;p&gt;&amp;#160;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal" style="mso-layout-grid-align:none;"&gt;&lt;span style="font-size:10pt;color:blue;font-family:'Courier New';mso-no-proof:yes;"&gt;drop&lt;/span&gt;&lt;span style="font-size:10pt;font-family:'Courier New';mso-no-proof:yes;"&gt; &lt;span style="color:blue;"&gt;table&lt;/span&gt; dbo&lt;span style="color:gray;"&gt;.&lt;/span&gt;SalesOrderDetail       &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/p&gt;</description></item></channel></rss>