Kalen Delaney : execution planhttp://sqlblog.com/blogs/kalen_delaney/archive/tags/execution+plan/default.aspxTags: execution planenCommunityServer 2.1 SP2 (Build: 61129.1)Did You Know? Everything's Up to Date in Kansas City!http://sqlblog.com/blogs/kalen_delaney/archive/2008/02/01/kansas-city-seminar.aspxFri, 01 Feb 2008 18:54:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:4826Kalen Delaney1http://sqlblog.com/blogs/kalen_delaney/comments/4826.aspxhttp://sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=4826<P>They are so up-to-date, I have chosen it as one of the handful of&nbsp; cities on my user group tour... </P>
<P>I'll be presenting a full day seminar on <EM>Controlling and Reusing Query Plans</EM> in SQL Server 2005. You can get more information from&nbsp; my schedule page at <A href="http://schedule.kalendelaney.com/" target=_blank>schedule.KalenDelaney.com</A> </P>
<P>Full details, including registration information, can be found here:<BR><A title=http://www.cleardata.biz/events/query-plans-2008.aspx href="http://www.cleardata.biz/events/query-plans-2008.aspx">http://www.cleardata.biz/events/query-plans-2008.aspx</A></P>
<P>If you live anywhere nearby, it would be great to see you there.</P>
<P><FONT color=#ff00ff>~Kalen</FONT></P><img src="http://sqlblog.com/aggbug.aspx?PostID=4826" width="1" height="1">execution planindex hintsplan guidesseminarstraininguser groupsDid you know? Cloning is Legal (and Available in SQL Server 2005)http://sqlblog.com/blogs/kalen_delaney/archive/2007/11/21/cloning-in-sql-server-2005.aspxWed, 21 Nov 2007 18:50:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:3465Kalen Delaney5http://sqlblog.com/blogs/kalen_delaney/comments/3465.aspxhttp://sqlblog.com/blogs/kalen_delaney/commentrss.aspx?PostID=3465<P>&nbsp;</P>
<P>I first heard about the possibility of database cloning way back in version 7.0 and thought it sounded like a great idea. Who wouldn't love to have a bunch of identical little databases running around, of smaller size and easier manageability? </P>
<P>Cloning a database means creating a statistics only copy of it, so that you can examine execution plans for poorly performing queries&nbsp;in a test environment that takes hardly any space at all.&nbsp; A clone database has all the metadata for all your objects, including function and stored procedure definitions. It has all the statistics for all the objects including the histograms and density information returned by DBCC SHOW_STATISTICS. Looking at space usage information shows you only the pages used, but not the pages allocated but unused. </P>
<P>So how can you get a little clone of your own? The original plan was to have a toolbar option to just click and presto, there would be your clone. That never happened. Later, one of the optimizer engineers at Microsoft created a stored procedure, called something like sp_clone_db that took a database name as a parameter, and created a new database with all the metadata and statistics, but none of the data, of the original. That procedure was never&nbsp;included with any version of the product.</P>
<P>Instead, what we have in SQL Server 2005 is the ability to script a database, and include the statistics and histogram information.&nbsp; </P>
<P>In SQL Server Management Studio's Object Explorer, right-click your database name and choose Tasks, and then Generate Scripts....</P>
<P>Choose the name of the database to script, and select the box to "Script all objects in the selected database".&nbsp; Next you'll see a "Choose Script Options" dialog with several dozen options to choose from, mostly indicating whether your want to include certain properties or objects in your database. Most of these you can leave at the default, or deselect the ones you're not interested in. I usually select the option to create the database, so it will include the CREATE DATABASE statement and any necessary filegroups. If you don't do this, and your database has objects created on filegroups other than the primary, your script will need a lot more manual editing. </P>
<P>There are two&nbsp;options that you <STRONG>must</STRONG> change in this "Choose Script Options" dialog. In the General section&nbsp;is the one called "Script Statistics". When you click in the list of options on the right side, you'll see three different options. The default is "Do not script statistics". <FONT color=#ff0000>Choose the option to "Script statistics and histograms."</FONT> When you make this selection, you'll get a warning like the following:</P><A href="http://sqlblog.com/blogs/kalen_delaney/WindowsLiveWriter/87e4c1029f59_B0F8/image05.png"><IMG style="BORDER-TOP-WIDTH:0px;BORDER-LEFT-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;BORDER-RIGHT-WIDTH:0px;" height=116 src="http://sqlblog.com/blogs/kalen_delaney/WindowsLiveWriter/87e4c1029f59_B0F8/image0_thumb3.png" width=545 border=0></A>
<P>If you want a clone, you'll have to use this option, so don't let this warning scare you.</P>
<P>In the Table/View Options section (you'll have to scroll down) <FONT color=#ff0000>change "Script Indexes" to True.</FONT></P>
<P>Click Ok, and after you click Next to&nbsp;leave this "Choose Script Options" dialog, the&nbsp;"Output Options" dialog opens. &nbsp; (If you forget to check the box for scripting all objects in the database in the very&nbsp;first dialog, you'll get about 10 different dialogs asking you to choose which objects you want to copy, before you get to the "Output Options" dialog. Don't say I didn't warn you.)</P>
<P>I usually just choose to copy to a New&nbsp;Query Window and click Next. I then get a chance to confirm my choices, and then I click Finish. The copy process begins.</P>
<P><FONT color=#ff0000><STRONG>WARNING: If you notice in the confirmation list that you forgot something, or want to go back and make any changes, the option to Script Indexes will be set back to false. You'll need to make sure to reset it to True before going forward again.</STRONG></FONT> </P>
<P>Once I get the script in the new window, I need to make sure I change all references to the original database name to a new name. (If you are creating the clone on a different server that doesn't have the original database, then you won't have to do this step.) Make sure you check the filenames and make sure they don't duplicate existing names.&nbsp; Execute when ready.</P>
<P>Or, you might want to spend a few minutes examining the script. You'll see UPDATE STATISTICS statements like you've never seen before, looking something like the following. (My clone was created on the AdventureWorks database):</P>
<P>UPDATE STATISTICS [Sales].[CreditCard]([AK_CreditCard_CardNumber]) WITH STATS_STREAM = 0x0100000002000144 &lt;huge big long hex value&gt; 000000000000510D0000000000,&nbsp; ROWCOUNT = 19118, PAGECOUNT = 93</P>
<P>So the UPDATE STATISTICS statements force a particular histogram into the statistics, as well as the page count and row count.</P>
<P>Once you've created the new database, you should be able run queries to look at sizing information. </P>
<P>EXEC sp_spaceused 'Sales.SalesOrderDetail' </P>
<P>returns the following:</P>
<P>&nbsp;</P>
<P><FONT face="Courier New" size=2>name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; rows&nbsp;&nbsp;&nbsp;reserved data&nbsp;&nbsp;&nbsp; index_size unused<BR>---------------- ------ -------- ------- ---------- ------<BR>SalesOrderDetail 121317&nbsp;0 KB&nbsp;&nbsp;&nbsp;&nbsp; 9880 KB&nbsp;0 KB&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 KB</FONT></P>
<P><FONT face="Courier New"><FONT size=2><FONT face=ta><FONT face=Tahoma>However</FONT>,</FONT> </FONT></FONT></P>
<P><FONT face="Courier New" size=2>SELECT count(*) FROM Sales.SalesOrderDetail </FONT></P>
<P><FONT size=2>returns 0.</FONT></P>
<P><FONT size=2>You can look at execution plans for queries involving any of the cloned tables and pre-existing indexes. You can create new indexes, but be careful. Any new indexes will have statistics created based on the real data in the clone database, which is 0 rows. So the statistics on your new indexes will not be very accurate.</FONT></P>
<P><FONT size=2>Of course, your clone database cannot be used for timing tests, or troubleshooting concurrency issues, since there is no data to lock, but for examining how plans change as you rewrite queries, it's a great idea.</FONT></P>
<P>Have fun!</P>
<P><FONT color=#ff00ff>~Kalen</FONT></P><img src="http://sqlblog.com/aggbug.aspx?PostID=3465" width="1" height="1">execution planmetadatascriptsstatisticsTools