<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Fool &#187; SQL Tips</title>
	<atom:link href="http://sqlfool.com/category/tips-suggestions/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com</link>
	<description>Adventures in SQL Tuning - a blog for the rest of us</description>
	<lastBuildDate>Wed, 02 Nov 2011 20:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Are You Approaching Your Partition Range Limits?</title>
		<link>http://sqlfool.com/2011/11/are-you-approaching-your-partition-range-limits/</link>
		<comments>http://sqlfool.com/2011/11/are-you-approaching-your-partition-range-limits/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 20:32:01 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[maintenace]]></category>
		<category><![CDATA[partitioning]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1586</guid>
		<description><![CDATA[In my post last week, How To Estimate Data Utilization, I said that it may be my last post for a while. Well... apparently I lied. For those of you who use table partitioning, you know that you need to define a partitioning scheme and function prior to applying partitioning to an index. Personally, I [...]]]></description>
			<content:encoded><![CDATA[<p>In my post last week, <a href="http://sqlfool.com/2011/10/how-to-estimate-data-utilization/" target="_blank">How To Estimate Data Utilization</a>, I said that it may be my last post for a while.  Well... apparently I lied.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>For those of you who use <a href="http://sqlfool.com/2008/11/102/" target="_blank">table partitioning</a>, you know that you need to define a partitioning scheme and function prior to applying partitioning to an index.  Personally, I tend to build the function for a couple of years out, and I tend to create them through the end of a calendar year.  Now, if I failed to expand a partition range at the end of the year, then come January 1st, all of my data would be written to the same partition.  Not the end of the world, no, but it causes all kinds of nasty performance and maintenance issues.  Thus, as part of my end-of-year / maternity-leave preparations, I'm in the process of examining all partitioned functions to identify those that need to have their partition ranges expanded. For those interested, here's the script I used:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#Results'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Results;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #Results
<span style="color: #808080;">&#40;</span>
      databaseName  <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , schemaName    <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , functionName  <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , data_space_id <span style="color: #0000FF;">INT</span>
    , maxRangeValue <span style="color: #0000FF;">SQL_VARIANT</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">/* Grab results for each database and store in our temp table.  
   And no, I don't *need* to select from sys.indexes and perform 
   left joins, but I'm overly cautious and want to make sure 
   I'm not accidentally missing any databases. :) */</span>
&nbsp;
<span style="color: #008080;">--EXECUTE master.dbo.sp_msforeachdb</span>
<span style="color: #0000FF;">EXECUTE</span> sp_foreachdb <span style="color: #FF0000;">'USE ?;
INSERT INTO #Results
SELECT DB_NAME() AS databaseName
    , sps.name AS schemaName
    , spf.name AS functionName
    , sps.data_space_id 
    , MAX(prv.value) AS maxRangeValue
FROM sys.indexes AS i
LEFT JOIN sys.partition_schemes AS sps WITH (NOLOCK)
    ON i.data_space_id = sps.data_space_id
LEFT JOIN sys.partition_functions AS spf WITH (NOLOCK)
    ON sps.function_id = spf.function_id
LEFT JOIN sys.partition_range_values AS prv WITH (NOLOCK)
    ON spf.function_id = prv.function_id
GROUP BY sps.name
    , spf.name
    , sps.data_space_id;'</span>;
<span style="color: #008080;">/*  
    sp_foreachdb was written by SQL MVP Aaron Bertrand and can be downloaded 
    at http://www.mssqltips.com/sqlservertip/2201/making-a-more-reliable-and-flexible-spmsforeachdb/
    Alternatively, you can also use sys.sp_MSforeachdb
*/</span>
&nbsp;
<span style="color: #008080;">/* Make sure we're not missing any major databases */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> databaseName <span style="color: #0000FF;">FROM</span> #Results<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Retrieve our results */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> #Results
<span style="color: #0000FF;">WHERE</span> schemaName <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> maxRangeValue;</pre></div></div>

<p>Example Results:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">databaseName        schemaName                      functionName                          data_space_id   maxRangeValue
------------------- ------------------------------- ------------------------------------- --------------- -------------------------
HistoricalMart      dailyRangeDate_ps               dailyRangeDate_pf                     65609           2011-12-31 00:00:00.000
AdventureWorks      yearlyRangeSmallDateTime_ps     yearlyRangeSmallDateTime_pf           65605           2012-01-01 00:00:00.000
dbaTools            monthlyRangeDateTime_ps         monthlyRangeDateTime_pf               65604           2012-12-01 00:00:00.000</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/11/are-you-approaching-your-partition-range-limits/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How To Estimate Data Utilization</title>
		<link>http://sqlfool.com/2011/10/how-to-estimate-data-utilization/</link>
		<comments>http://sqlfool.com/2011/10/how-to-estimate-data-utilization/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 19:11:12 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Business Intelligence]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[DMV]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1578</guid>
		<description><![CDATA[Recently, on a conference call presenting data growth rates and database capacity projections, I had a top-line executive ask, "But how much of that data are we actually using today?" The question was met with silence; unless you have rigorous auditing in place -- and kudos to you if you do -- it's a difficult [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, on a conference call presenting data growth rates and database capacity projections, I had a top-line executive ask, "But how much of that data are we actually <em>using</em> today?"  The question was met with silence; unless you have rigorous auditing in place -- and kudos to you if you do -- it's a difficult question to answer.  But it begs the question, is there some way to gleam this information from SQL Server?  I think the answer is "yes," if you make some assumptions and understand what you're looking at.  </p>
<p>SQL Server collects stats about every time an index is used and how it is used (i.e. whether a user seeked or scanned the index, etc.).  It also provides a DMV to view these stats: <a href="http://msdn.microsoft.com/en-us/library/ms188755.aspx" target="_blank">sys.dm_db_index_usage_stats</a>.</p>
<p>This DMV provides a wealth of great information, but to answer our question of "What data is actually being used?", we have to refine our criteria.  Are we talking in terms of table counts or data size?  I'd argue that data size is more important than table counts; one unqueried millow-row table is more wasteful than a hundred ten-row tables.  </p>
<p>Also, are we looking at indexes or content?  From a database perspective, I'm more interested in indexes: how much space are we wasting on unused indexes?  To identify this, I need to look at the activity on each individual index.</p>
<p>From a business perspective, I would be more interested in content (i.e. tables): how much business information is being stored that no one is even looking at?  To answer this question, I need to roll up all index usage to see if *any* of the indexes on a table were used.  Since both were of interest to me, I decided to write queries to answer both questions.  </p>
<p>Lastly, we need to understand the flaws with this data.  Chiefly, I cannot tell whether a user requested one row from a million-row table, or if [s]he needed all of the data in the table.  This is a pretty important issue, especially with large historical data stores, and it's where I have to make the biggest assumption: if even one person looked at one row in the table, I count all pages in the table as having been accessed.  </p>
<p>Now, you may make different decisions than I did above, and that's fine... each environment and project has different needs.  But these assumptions are very important to understanding the output of the query below:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> master;
GO
&nbsp;
<span style="color: #008080;">/* 
    This will give you an approximation of how much data is being utilized on a server.
    Since the data is only valid as of the last server reboot, we should start off with
    an idea of how much data we've accrued.  
*/</span>
&nbsp;
<span style="color: #008080;">/* Find out when the server was last rebooted */</span>
<span style="color: #008080;">-- 2008</span>
<span style="color: #0000FF;">SELECT</span> sqlserver_start_time <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_os_sys_info</span>;
<span style="color: #008080;">-- 2005</span>
<span style="color: #0000FF;">SELECT</span> create_date <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">=</span> <span style="color: #FF0000;">'tempdb'</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">/* Create a temporary table to hold our data, since we're going to iterate through databases */</span>
<span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#Results'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Results;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #Results
<span style="color: #808080;">&#40;</span>
      databaseName  <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , tableName     <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , indexID       <span style="color: #0000FF;">INT</span>
    , records       <span style="color: #0000FF;">BIGINT</span>
    , activity      <span style="color: #0000FF;">BIGINT</span>
    , totalPages    <span style="color: #0000FF;">BIGINT</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/*  
    sp_foreachdb was written by SQL MVP Aaron Bertrand and can be downloaded 
    at http://www.mssqltips.com/sqlservertip/2201/making-a-more-reliable-and-flexible-spmsforeachdb/
    Alternatively, you can also use sys.sp_MSforeachdb
*/</span>
<span style="color: #008080;">--EXECUTE master.dbo.sp_foreachdb</span>
<span style="color: #0000FF;">EXECUTE</span> sys.<span style="color: #202020;">sp_MSforeachdb</span>
<span style="color: #FF0000;">'   USE ?; 
&nbsp;
    -- You can gleam a lot of information about historical data usage from partitions
    -- but for now, we will just roll up any partitions we may have
    WITH myCTE AS
    (
        SELECT p.[object_id] AS objectID
            , p.index_id
            , SUM(p.[rows]) AS records
            , SUM(au.total_pages) AS totalPages
        FROM sys.partitions AS p WITH (NOLOCK)
        JOIN sys.allocation_units AS au WITH (NOLOCK)
            ON p.hobt_id = au.container_id
        GROUP BY p.[object_id] 
            , p.index_id
    )
&nbsp;
    -- Grab all tables and join to our usage stats DMV
    INSERT INTO #Results
    SELECT DB_NAME() AS databaseName
        , t.name
        , x.index_id
        , MAX(x.records) AS records
        , ISNULL(SUM(us.user_lookups + us.user_scans + us.user_seeks), 0) AS activity
        , SUM(x.totalPages) AS totalPages
    FROM sys.tables AS t WITH (NOLOCK)
    JOIN myCTE AS x
        ON t.[object_id] = x.objectID
    LEFT JOIN sys.dm_db_index_usage_stats AS us WITH (NOLOCK)
        ON us.[object_id] = x.objectID
        AND us.index_id = x.index_id
        AND us.database_id = DB_ID()
    GROUP BY t.name
    , x.index_id;'</span>
&nbsp;
<span style="color: #008080;">/* Because we're looping through databases, make sure we're not missing any major ones */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> databaseName <span style="color: #0000FF;">FROM</span> #Results<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Retrieve actual % data utilization, which is performed at the index level */</span>
<span style="color: #0000FF;">SELECT</span> databaseName
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>queriedPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> TotalQueriedPages
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> TotalPages
    , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>queriedPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span>, <span style="color: #000;">0</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'%DataUtil'</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">SELECT</span> databaseName
        , tableName
        , indexID
        , <span style="color: #0000FF;">CASE</span> <span style="color: #008080;">-- If we have any activity at all on an index, count it as activity</span>
            <span style="color: #0000FF;">WHEN</span> activity <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0.0</span>
            <span style="color: #0000FF;">ELSE</span> totalPages
          <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> queriedPages
        , totalPages
    <span style="color: #0000FF;">FROM</span> #Results
    <span style="color: #0000FF;">WHERE</span> databaseName <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'master'</span>, <span style="color: #FF0000;">'tempdb'</span>, <span style="color: #FF0000;">'msdb'</span>, <span style="color: #FF0000;">'model'</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span> x
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> databaseName
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> databaseName;
&nbsp;
<span style="color: #008080;">/* Retrieve % content utilization, which is performed at the table level */</span>
<span style="color: #0000FF;">SELECT</span> databaseName
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>queriedPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> TotalQueriedPages
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> TotalPages
    , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>queriedPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span>, <span style="color: #000;">0</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'%ContentUtil'</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">SELECT</span> databaseName
        , tableName
        , <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span>records<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> records
        , <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>activity<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> queriedPages
        , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> totalPages
    <span style="color: #0000FF;">FROM</span> #Results
    <span style="color: #0000FF;">WHERE</span> databaseName <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'master'</span>, <span style="color: #FF0000;">'tempdb'</span>, <span style="color: #FF0000;">'msdb'</span>, <span style="color: #FF0000;">'model'</span><span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> databaseName
        , tableName
<span style="color: #808080;">&#41;</span> x
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> databaseName
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> databaseName;</pre></div></div>

<p>Results:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">databaseName               TotalQueriedPages   TotalPages           %DataUtil
-------------------------- ------------------- -------------------- ----------------------
Database1 		   127618701           130607247            0.969619893356378
Database2 		   567188              1614958              0.351209133612143
Database3 		   34269036            34579469             0.991022620966216
Database4 		   137970594           170733391            0.803399928206158
Database5 		   74632930            101543575            0.66909214627557
Database6 		   55809933            72884205             0.765734157938039
Database7 		   560810026           620609815            0.902175272517656
&nbsp;
databaseName               TotalQueriedPages   TotalPages           %ContentUtil
-------------------------- ------------------- -------------------- ----------------------
Database1 		   127763715           130607247            0.970721679051682
Database2 		   571125              1614958              0.353646967908763
Database3 		   34269036            34579469             0.991022620966216
Database4 		   137970921           170733391            0.803399928206158
Database5 		   96144726            101543575            0.861947682777784
Database6 		   72269666            72884205             0.991568146820268
Database7 		   620525938           620609815            0.998240279711804</pre></div></div>

<p>The first result set examines the utilization of indexes, and the second result set examines the utilization of data at the content (table) level.  For example, if we look at Database6, we'll see that we are only utilizing 77% of our indexes, but we're looking at 99% of our table data.  So this is a good indicator that we have some unused indexes to clean up in that database.  </p>
<p>Know a better way to answer this question using SQL Server DMV's?  Please leave me a comment so I can learn from your experience.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In unrelated news, this may be my last blog post for a little while.  I'm due with my second child a week from today and expect all of my free time to be consumed by him for a little while.  That and, quite frankly, I do not trust myself near a computer, especially a database, in such a sleep-deprived state.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/10/how-to-estimate-data-utilization/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Calculate Rows Inserted per Second for All Tables</title>
		<link>http://sqlfool.com/2011/07/calculate-rows-inserted-per-second/</link>
		<comments>http://sqlfool.com/2011/07/calculate-rows-inserted-per-second/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 21:24:06 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[maintenace]]></category>
		<category><![CDATA[monitor]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1565</guid>
		<description><![CDATA[Ever needed to calculate the number of rows inserted every second, for every table in every database on a server? Or, have you ever needed to validate that all processes have stopped writing to tables? These types of questions come up routinely for me. To help with this, I've written the following script, which examines [...]]]></description>
			<content:encoded><![CDATA[<p>Ever needed to calculate the number of rows inserted every second, for every table in every database on a server?  Or, have you ever needed to validate that all processes have stopped writing to tables?  These types of questions come up routinely for me.  To help with this, I've written the following script, which examines metadata values using sys.partitions.  This method isn't as accurate as running SELECT COUNT(*) FROM, but it's much faster.  Keep in mind, since it's just looking at row counts, it's not much help on tables that have a lot of update/delete activity.  But it does what I need it to do, and I use it pretty regularly, so I thought I'd share in case anyone else can benefit from it too.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Declare Parameters */</span>
<span style="color: #0000FF;">DECLARE</span> @newBaseline <span style="color: #0000FF;">BIT</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #008080;">-- change to 0 when you don't want to replace the baseline, i.e. after initial run</span>
  , @delay <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'00:00:30'</span>; <span style="color: #008080;">-- change as needed</span>
&nbsp;
<span style="color: #0000FF;">IF</span> @newBaseline <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
<span style="color: #0000FF;">BEGIN</span>
    <span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#baseline'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
        <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #baseline;
&nbsp;
    <span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #baseline
    <span style="color: #808080;">&#40;</span>
         database_name  SYSNAME
       , table_name     SYSNAME
       , table_rows     <span style="color: #0000FF;">BIGINT</span>
       , captureTime    <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NULL</span>
    <span style="color: #808080;">&#41;</span>;
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#current'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #current;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #current
<span style="color: #808080;">&#40;</span>
     database_name  SYSNAME
   , table_name     SYSNAME
   , table_rows     <span style="color: #0000FF;">BIGINT</span>
   , captureTime    <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">IF</span> @newBaseline <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
<span style="color: #0000FF;">BEGIN</span>
    <span style="color: #0000FF;">EXECUTE</span> sp_MSforeachdb <span style="color: #FF0000;">'USE ?; 
        INSERT INTO #baseline
        SELECT DB_NAME()
            , o.name As [tableName]
            , SUM(p.[rows]) As [rowCnt]
            , GETDATE() As [captureTime]
        FROM sys.indexes As i
        JOIN sys.partitions As p
            ON i.[object_id] = p.[object_id]
           AND i.index_id  = p.index_id
        JOIN sys.objects As o
            ON i.[object_id] = o.[object_id]
        WHERE i.[type] = 1
        GROUP BY o.name;'</span>
&nbsp;
    <span style="color: #0000FF;">WAITFOR</span> DELAY @delay;
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">EXECUTE</span> sp_MSforeachdb <span style="color: #FF0000;">'USE ?; 
INSERT INTO #current
SELECT DB_NAME()
    , o.name As [tableName]
    , SUM(p.[rows]) As [rowCnt]
    , GETDATE() As [captureTime]
FROM sys.indexes As i
JOIN sys.partitions As p
    ON i.[object_id] = p.[object_id]
   AND i.index_id  = p.index_id
JOIN sys.objects As o
    ON i.[object_id] = o.[object_id]
WHERE i.[type] = 1
GROUP BY o.name;'</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span>  c.<span style="color: #808080;">*</span>
      , c.<span style="color: #202020;">table_rows</span> <span style="color: #808080;">-</span> b.<span style="color: #202020;">table_rows</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'new_rows'</span>
      , <span style="color: #FF00FF;">DATEDIFF</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">second</span>, b.<span style="color: #202020;">captureTime</span>, c.<span style="color: #202020;">captureTime</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'time_diff'</span>
      , <span style="color: #808080;">&#40;</span>c.<span style="color: #202020;">table_rows</span> <span style="color: #808080;">-</span> b.<span style="color: #202020;">table_rows</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #FF00FF;">DATEDIFF</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">second</span>, b.<span style="color: #202020;">captureTime</span>, c.<span style="color: #202020;">captureTime</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'rows_per_sec'</span>
<span style="color: #0000FF;">FROM</span> #baseline <span style="color: #0000FF;">AS</span> b
<span style="color: #808080;">JOIN</span> #current <span style="color: #0000FF;">AS</span> c
    <span style="color: #0000FF;">ON</span> b.<span style="color: #202020;">table_name</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">table_name</span>
   <span style="color: #808080;">AND</span> b.<span style="color: #202020;">database_name</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">database_name</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> new_rows <span style="color: #0000FF;">DESC</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/07/calculate-rows-inserted-per-second/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>T-SQL Script for Estimating Compression Savings</title>
		<link>http://sqlfool.com/2011/06/estimate-compression-savings/</link>
		<comments>http://sqlfool.com/2011/06/estimate-compression-savings/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 14:51:40 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[2008]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[partitioning]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1539</guid>
		<description><![CDATA[A couple of weeks ago, I was working on a Microsoft PDW proof-of-concept (POC) and had to measure compression ratios. In order to do this, I fired up SSMS and wrote a little script. The script will iterate through all tables in a database and run the sp_estimate_data_compression_savings stored procedure. This will only work in [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I was working on a <a href="http://www.microsoft.com/sqlserver/en/us/editions/data-warehouse.aspx" target="_blank">Microsoft PDW</a> proof-of-concept (POC) and had to measure compression ratios.  In order to do this, I fired up SSMS and wrote a little script.  The script will iterate through all tables in a database and run the <a href="http://msdn.microsoft.com/en-us/library/cc280574.aspx" target="_blank">sp_estimate_data_compression_savings</a> stored procedure.  <strong>This will only work in SQL Server 2008+ versions running Enterprise edition</strong>.</p>
<p>If you're not familiar with this stored procedure, it basically will tell you what effect PAGE or ROW compression will have on your table/index/partition, etc.  There are pro's and con's with compression.  What I've tended to see is that compression has very positive results on space, IO, and query duration, with a negative impact on CPU and write speed.  Like most things, it's a trade-off and the results will vary by environment, so I recommend you do some testing before you apply compression to all tables.  I tend to use compression mostly for my historical tables and partitions and leave my recent data uncompressed.  And, back to the script, I use this stored procedure to estimate the impact of compression and to determine whether to use PAGE or ROW compression.  PAGE is a higher level of compression, which means it's also more expensive in terms of CPU, so if the difference between the two results is negligible, I'm more apt to just use ROW compression.  </p>
<p>Now that my impromptu compression discussion is done, let's get to the actual script.  One final word of caution, however. <strong>This is an IO intensive process</strong>, so you may want to run it after peak business hours.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @printOnly  <span style="color: #0000FF;">BIT</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #008080;">-- change to 1 if you don't want to execute, just print commands</span>
    , @tableName    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>
    , @schemaName   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
    , @sqlStatement <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>
    , @tableCount   <span style="color: #0000FF;">INT</span>
    , @statusMsg    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">IF</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> tempdb.<span style="color: #202020;">sys</span>.<span style="color: #202020;">tables</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%#tables%'</span><span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #tables; 
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #tables
<span style="color: #808080;">&#40;</span>
      database_name     sysname
    , schemaName        sysname <span style="color: #808080;">NULL</span>
    , tableName         sysname <span style="color: #808080;">NULL</span>
    , processed         <span style="color: #0000FF;">bit</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">IF</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> tempdb.<span style="color: #202020;">sys</span>.<span style="color: #202020;">tables</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%#compression%'</span><span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #compressionResults;
&nbsp;
<span style="color: #0000FF;">IF</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> tempdb.<span style="color: #202020;">sys</span>.<span style="color: #202020;">tables</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%#compression%'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span> 
&nbsp;
    <span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #compressionResults
    <span style="color: #808080;">&#40;</span>
          objectName                    <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
        , schemaName                    <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>
        , index_id                      <span style="color: #0000FF;">int</span>
        , partition_number              <span style="color: #0000FF;">int</span>
        , size_current_compression      <span style="color: #0000FF;">bigint</span>
        , size_requested_compression    <span style="color: #0000FF;">bigint</span>
        , sample_current_compression    <span style="color: #0000FF;">bigint</span>
        , sample_requested_compression  <span style="color: #0000FF;">bigint</span>
    <span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #tables
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>
    , SCHEMA_NAME<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>schema_id<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
    , name
    , <span style="color: #000;">0</span> <span style="color: #008080;">-- unprocessed</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">tables</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @tableCount <span style="color: #808080;">=</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> #tables;
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> #tables <span style="color: #0000FF;">WHERE</span> processed <span style="color: #808080;">=</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> @tableName <span style="color: #808080;">=</span> tableName
        , @schemaName <span style="color: #808080;">=</span> schemaName
    <span style="color: #0000FF;">FROM</span> #tables <span style="color: #0000FF;">WHERE</span> processed <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @statusMsg <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Working on '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>@tableCount <span style="color: #808080;">-</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' of '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@tableCount <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">FROM</span> #tables
    <span style="color: #0000FF;">WHERE</span> processed <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
&nbsp;
    <span style="color: #0000FF;">RAISERROR</span><span style="color: #808080;">&#40;</span>@statusMsg, <span style="color: #000;">0</span>, <span style="color: #000;">42</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">WITH</span> NOWAIT;
&nbsp;
    <span style="color: #0000FF;">SET</span> @sqlStatement <span style="color: #808080;">=</span> <span style="color: #FF0000;">'EXECUTE sp_estimate_data_compression_savings '</span><span style="color: #FF0000;">''</span> 
                        <span style="color: #808080;">+</span> @schemaName <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @tableName <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', NULL, NULL, '</span><span style="color: #FF0000;">'PAGE'</span><span style="color: #FF0000;">';'</span> <span style="color: #008080;">-- ROW, PAGE, or NONE</span>
&nbsp;
    <span style="color: #0000FF;">IF</span> @printOnly <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    <span style="color: #0000FF;">BEGIN</span> 
&nbsp;
        <span style="color: #0000FF;">SELECT</span> @sqlStatement;
&nbsp;
    <span style="color: #0000FF;">END</span>
    <span style="color: #0000FF;">ELSE</span>
    <span style="color: #0000FF;">BEGIN</span>
&nbsp;
        <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #compressionResults
        <span style="color: #0000FF;">EXECUTE</span> <span style="color: #AF0000;">sp_executesql</span> @sqlStatement;
&nbsp;
    <span style="color: #0000FF;">END</span>;
&nbsp;
    <span style="color: #0000FF;">UPDATE</span> #tables
    <span style="color: #0000FF;">SET</span> processed <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    <span style="color: #0000FF;">WHERE</span> tableName <span style="color: #808080;">=</span> @tableName
        <span style="color: #808080;">AND</span> schemaName <span style="color: #808080;">=</span> @schemaName;
&nbsp;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> #compressionResults;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/06/estimate-compression-savings/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Identity Columns: Are You Nearing The Limits?</title>
		<link>http://sqlfool.com/2011/01/identity-columns-are-you-nearing-the-limits/</link>
		<comments>http://sqlfool.com/2011/01/identity-columns-are-you-nearing-the-limits/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 16:03:00 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[maintenace]]></category>
		<category><![CDATA[monitor]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1475</guid>
		<description><![CDATA[I use identity columns frequently. After all, identity columns make great clustering keys. But it's important when using identity columns to check on the amount of values you have left before you reach the limit of your data type. An identity column has a fixed amount of values it can use based upon whether you [...]]]></description>
			<content:encoded><![CDATA[<p>I use identity columns frequently.  After all, <a href="http://www.simple-talk.com/sql/learn-sql-server/effective-clustered-indexes/" target="_blank">identity columns make great clustering keys</a>.  But it's important when using identity columns to check on the amount of values you have left before you reach the limit of your data type.  An identity column has a fixed amount of values it can use based upon whether you specified tinyint, smallint, int, or bigint when you defined the column.  If you reach this limit, your inserts <del datetime="2011-01-13T15:41:01+00:00">will blow up and cause a Chernobyl-like SQL meltdown</del> will begin to fail.  I just finished an audit of my tables and thought I'd share the script.  I would like to warn that this script is *not* perfect... namely, it doesn't handle negative integer values very elegantly.  It also doesn't know if you started your seed at zero, approached your max positive limit, then reseeded to the negative limit (see my "quick and dirty fix" tip at the end of this article).</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Define how close we are to the value limit
   before we start throwing up the red flag.
   The higher the value, the closer to the limit. */</span>
<span style="color: #0000FF;">Declare</span> @threshold <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> .85;
&nbsp;
<span style="color: #008080;">/* Create a temp table */</span>
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> #identityStatus
<span style="color: #808080;">&#40;</span>
      database_name     <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , table_name        <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , column_name       <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , data_type         <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , last_value        <span style="color: #0000FF;">bigint</span>
    , max_value         <span style="color: #0000FF;">bigint</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Use an undocumented command to run a SQL statement
   in each database on a server */</span>
<span style="color: #0000FF;">Execute</span> sp_msforeachdb <span style="color: #FF0000;">'
    Use [?];
    Insert Into #identityStatus
    Select '</span><span style="color: #FF0000;">'?'</span><span style="color: #FF0000;">' As [database_name]
        , Object_Name(id.object_id, DB_ID('</span><span style="color: #FF0000;">'?'</span><span style="color: #FF0000;">')) As [table_name]
        , id.name As [column_name]
        , t.name As [data_type]
        , Cast(id.last_value As bigint) As [last_value]
        , Case 
            When t.name = '</span><span style="color: #FF0000;">'tinyint'</span><span style="color: #FF0000;">'   Then 255 
            When t.name = '</span><span style="color: #FF0000;">'smallint'</span><span style="color: #FF0000;">'  Then 32767 
            When t.name = '</span><span style="color: #FF0000;">'int'</span><span style="color: #FF0000;">'       Then 2147483647 
            When t.name = '</span><span style="color: #FF0000;">'bigint'</span><span style="color: #FF0000;">'    Then 9223372036854775807
          End As [max_value]
    From sys.identity_columns As id
    Join sys.types As t
        On id.system_type_id = t.system_type_id
    Where id.last_value Is Not Null'</span>;
&nbsp;
<span style="color: #008080;">/* Retrieve our results and format it all prettily */</span>
<span style="color: #0000FF;">Select</span> database_name
    , table_name
    , column_name
    , data_type
    , last_value
    , <span style="color: #0000FF;">Case</span> 
        <span style="color: #0000FF;">When</span> last_value <span style="color: #808080;">&lt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Then</span> <span style="color: #000;">100</span>
        <span style="color: #0000FF;">Else</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span> <span style="color: #808080;">-</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>last_value <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">float</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> max_value<span style="color: #808080;">&#41;</span> <span style="color: #808080;">*</span> <span style="color: #000;">100</span> 
      <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #808080;">&#91;</span>percentLeft<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">Case</span> 
        <span style="color: #0000FF;">When</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>last_value <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">float</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> max_value <span style="color: #808080;">&gt;=</span> @threshold
            <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'warning: approaching max limit'</span>
        <span style="color: #0000FF;">Else</span> <span style="color: #FF0000;">'okay'</span>
        <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #808080;">&#91;</span>id_status<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">From</span> #identityStatus
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> percentLeft;
&nbsp;
<span style="color: #008080;">/* Clean up after ourselves */</span>
<span style="color: #0000FF;">Drop</span> <span style="color: #0000FF;">Table</span> #identityStatus;</pre></div></div>

<p>If you find yourself quickly approaching your max limit and need to implement a quick and dirty fix, you can <a href="http://sqlfool.com/2008/11/max-int-identity-value-reached-dbcc-checkident" target-"_blank">reseed your identity column</a>.  Of course, this only works if you started at zero instead of the actual lower, negative limit.</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/01/identity-columns-are-you-nearing-the-limits/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>SQL Agent Job Script</title>
		<link>http://sqlfool.com/2010/12/sql-agent-job-script/</link>
		<comments>http://sqlfool.com/2010/12/sql-agent-job-script/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 00:00:28 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1457</guid>
		<description><![CDATA[This blog post is brought to you by the awesomeness that is the SQL Server Twitter community. Contributions were made by several awesome Tweeples, including Denny Cherry, Kendra Little, Ken Simmons, and Magnus Ahlkvist, among others. What started this is something you've all probably run into at one time or another. We're currently having some [...]]]></description>
			<content:encoded><![CDATA[<p>This blog post is brought to you by the awesomeness that is the SQL Server Twitter community.  Contributions were made by several awesome Tweeples, including <a href="http://www.twitter.com/mrdenny" target="_blank">Denny Cherry</a>, <a href="http://www.twitter.com/Kendra_Little" target="_blank">Kendra Little</a>, <a href="http://www.twitter.com/KenSimmons" target="_blank">Ken Simmons</a>, and <a href="http://www.twitter.com/MagnusAhlkvist" target="_blank">Magnus Ahlkvist</a>, among others.  </p>
<p>What started this is something you've all probably run into at one time or another.  We're currently having some resource contention on our server when a ton of processes kickoff and overlap in the morning.  Now, <a href="http://sqlfool.com/2009/02/view-sql-agent-jobs-tsql/" target="_blank">I have a script that I've used in the past</a> for monitoring SQL agent jobs, but this time I wanted to add some historical run-time information.  I know the sysjobhistory table contains the information I need, but it has some... let's say, creative ways of storing the data.  Opinions on the reasons why vary:<br />
<br />
<a href="http://sqlfool.com/wp-content/uploads/2010/12/tweets.jpg"><img src="http://sqlfool.com/wp-content/uploads/2010/12/tweets-187x300.jpg" alt="" title="tweets" width="187" height="300" class="aligncenter size-medium wp-image-1458" /></a><br />
<br />
Regardless the reason, I needed to do some conversions. <a href="http://itknowledgeexchange.techtarget.com/sql-server/" target="_blank">Denny</a> shared with us the msdb.dbo.agent_datetime function to convert run_date and run_time into an actual datetime value.  I have to say, this certainly cleans up the code quite a bit!  Then Magnus shared a method to convert run_duration into seconds, which he modified from a script on <a href="http://www.sqlservercentral.com/Forums/Topic206218-5-1.aspx" target="_blank">SQLServerCentral</a>.  I was able to use these two tidbits to update my old script.  You can now run this script to get back a list of all enabled procs, a script that will disable them, a breakdown of the schedule, and a historical run-time average.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/*  How many days do you want to include in your run-time average?
    Recent values tend to be more useful. */</span>
<span style="color: #0000FF;">Declare</span> @daysToAverage <span style="color: #0000FF;">smallint</span> <span style="color: #808080;">=</span> <span style="color: #000;">30</span>;
&nbsp;
<span style="color: #0000FF;">Declare</span> @avgRunTime <span style="color: #0000FF;">Table</span>
<span style="color: #808080;">&#40;</span>
      job_id      <span style="color: #0000FF;">uniqueidentifier</span>
    , avgRunTime  <span style="color: #0000FF;">int</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* We need to parse the schedule into something we can understand */</span>
<span style="color: #0000FF;">Declare</span> @weekDay <span style="color: #0000FF;">Table</span> <span style="color: #808080;">&#40;</span>
      mask       <span style="color: #0000FF;">int</span>
    , maskValue  <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">32</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> @weekDay
<span style="color: #0000FF;">Select</span> <span style="color: #000;">1</span>, <span style="color: #FF0000;">'Sunday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">2</span>, <span style="color: #FF0000;">'Monday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">4</span>, <span style="color: #FF0000;">'Tuesday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">8</span>, <span style="color: #FF0000;">'Wednesday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">16</span>, <span style="color: #FF0000;">'Thursday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">32</span>, <span style="color: #FF0000;">'Friday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">64</span>, <span style="color: #FF0000;">'Saturday'</span>;
&nbsp;
<span style="color: #008080;">/* First, let's get our run-time average */</span>
<span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> @avgRunTime
<span style="color: #0000FF;">Select</span> job_id
    , <span style="color: #FF00FF;">Avg</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>run_duration<span style="color: #808080;">/</span><span style="color: #000;">10000</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">*</span> <span style="color: #000;">3600</span> <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span>run_duration<span style="color: #808080;">/</span><span style="color: #000;">100</span><span style="color: #808080;">%</span>100<span style="color: #808080;">&#41;</span><span style="color: #808080;">*</span><span style="color: #000;">60</span> <span style="color: #808080;">+</span> run_duration<span style="color: #808080;">%</span>100<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'avgRunTime'</span> <span style="color: #008080;">/* convert HHMMSS to seconds */</span>
<span style="color: #0000FF;">From</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobhistory</span>
<span style="color: #0000FF;">Where</span> step_id <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #008080;">-- only grab our total run-time</span>
    And run_status <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #008080;">-- only grab successful executions</span>
    And msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">agent_datetime</span><span style="color: #808080;">&#40;</span>run_date, run_time<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;=</span> <span style="color: #FF00FF;">DateAdd</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">day</span>, <span style="color: #808080;">-</span>@daysToAverage, <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">By</span> job_id;
&nbsp;
<span style="color: #008080;">/* Now let's get our schedule information */</span>
<span style="color: #0000FF;">With</span> myCTE
<span style="color: #0000FF;">As</span><span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">Select</span> sched.<span style="color: #202020;">name</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'scheduleName'</span>
        , sched.<span style="color: #202020;">schedule_id</span>
        , jobsched.<span style="color: #202020;">job_id</span>
        , <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Once'</span> 
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> 
                And sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
                    <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Daily'</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> 
                <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Every '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_interval</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' days'</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">Then</span> 
                <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span> 
                    <span style="color: #0000FF;">Select</span> maskValue 
                    <span style="color: #0000FF;">From</span> @weekDay <span style="color: #0000FF;">As</span> x 
                    <span style="color: #0000FF;">Where</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">&amp;</span> x.<span style="color: #202020;">mask</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                    <span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> mask <span style="color: #0000FF;">For</span> XML Raw<span style="color: #808080;">&#41;</span>
                , <span style="color: #FF0000;">'&quot;/&gt;&lt;row maskValue=&quot;'</span>, <span style="color: #FF0000;">', '</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&lt;row maskValue=&quot;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&quot;/&gt;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                        And sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
                            <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'; weekly'</span> 
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'; every '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' weeks'</span> <span style="color: #0000FF;">End</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">16</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'On day '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_interval</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' of every '</span>
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' months'</span> 
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">32</span> <span style="color: #0000FF;">Then</span> 
                <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'First'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Second'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Third'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Fourth'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">16</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Last'</span>
                <span style="color: #0000FF;">End</span> <span style="color: #808080;">+</span> 
                <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Sunday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Monday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">3</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Tuesday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Wednesday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">5</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Thursday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">6</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Friday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">7</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Saturday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Day'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">9</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Weekday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">10</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Weekend'</span>
                <span style="color: #0000FF;">End</span>
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                        And sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'; monthly'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'; every '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' months'</span> <span style="color: #0000FF;">End</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">64</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'StartUp'</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">128</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Idle'</span>
          <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'frequency'</span>
        , IsNull<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Every '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_subday_interval</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> 
            <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' seconds'</span>
                <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' minutes'</span>
                <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' hours'</span>
            <span style="color: #0000FF;">End</span>, <span style="color: #FF0000;">'Once'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'subFrequency'</span>
        , <span style="color: #FF00FF;">Replicate</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">Len</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_start_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_start_time</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'startTime'</span>
        , <span style="color: #FF00FF;">Replicate</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">Len</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_end_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_end_time</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'endTime'</span>
        , <span style="color: #FF00FF;">Replicate</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">Len</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_time</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'nextRunTime'</span>
        , <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_date</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'nextRunDate'</span>
    <span style="color: #0000FF;">From</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysschedules</span> <span style="color: #0000FF;">As</span> sched
    Join msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobschedules</span> <span style="color: #0000FF;">As</span> jobsched
        <span style="color: #0000FF;">On</span> sched.<span style="color: #202020;">schedule_id</span> <span style="color: #808080;">=</span> jobsched.<span style="color: #202020;">schedule_id</span>
    <span style="color: #0000FF;">Where</span> sched.<span style="color: #202020;">enabled</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">/* Finally, let's look at our actual jobs and tie it all together */</span>
<span style="color: #0000FF;">Select</span> job.<span style="color: #202020;">name</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'jobName'</span>
    , sched.<span style="color: #202020;">scheduleName</span>
    , sched.<span style="color: #202020;">frequency</span>
    , sched.<span style="color: #202020;">subFrequency</span>
    , <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">startTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">startTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' - '</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">endTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">endTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> 
        <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'scheduleTime'</span> <span style="color: #008080;">-- HH:MM</span>
    , <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">1</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">5</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">7</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' '</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'nextRunDate'</span>
      <span style="color: #008080;">/* Note: the sysjobschedules table refreshes every 20 min, 
        so nextRunDate may be out of date */</span>
    , <span style="color: #FF0000;">'Execute msdb.dbo.sp_update_job @job_id = '</span><span style="color: #FF0000;">''</span> 
        <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>job.<span style="color: #202020;">job_id</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', @enabled = 0;'</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'disableScript'</span>
    , art.<span style="color: #202020;">avgRunTime</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'avgRunTime_inSec'</span> <span style="color: #008080;">-- in seconds</span>
    , <span style="color: #808080;">&#40;</span>art.<span style="color: #202020;">avgRunTime</span> <span style="color: #808080;">/</span> <span style="color: #000;">60</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'avgRunTime_inMin'</span> <span style="color: #008080;">-- convert to minutes</span>
<span style="color: #0000FF;">From</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobs</span> <span style="color: #0000FF;">As</span> job
Join myCTE <span style="color: #0000FF;">As</span> sched
    <span style="color: #0000FF;">On</span> job.<span style="color: #202020;">job_id</span> <span style="color: #808080;">=</span> sched.<span style="color: #202020;">job_id</span>
<span style="color: #0000FF;">Left</span> Join @avgRunTime <span style="color: #0000FF;">As</span> art
    <span style="color: #0000FF;">On</span> job.<span style="color: #202020;">job_id</span> <span style="color: #808080;">=</span> art.<span style="color: #202020;">job_id</span>
<span style="color: #0000FF;">Where</span> job.<span style="color: #202020;">enabled</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #008080;">-- do not display disabled jobs</span>
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> nextRunDate;</pre></div></div>

<p>If this doesn't do exactly what you want, check out <a href="http://sqlsoft.co.uk/sqljobvis.php" target="_blank">SQLJobVis</a>, which Ken recommended.  It's a free tool that helps visualize the job history of SQL jobs.  </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/12/sql-agent-job-script/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Metadata for Table Valued Parameters</title>
		<link>http://sqlfool.com/2010/10/metadata-for-table-valued-parameters/</link>
		<comments>http://sqlfool.com/2010/10/metadata-for-table-valued-parameters/#comments</comments>
		<pubDate>Thu, 28 Oct 2010 15:10:27 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1390</guid>
		<description><![CDATA[Table-valued parameters (TVP) are a great feature that was new in SQL Server 2008 that allow you to insert a dataset into a table. Previously, the most common way of doing this was by passing and parsing XML. As I've previously posted, TVP's perform an astounding 94% faster than singleton inserts and 75% faster than [...]]]></description>
			<content:encoded><![CDATA[<p>Table-valued parameters (TVP) are a great feature that was new in SQL Server 2008 that allow you to insert a dataset into a table.  Previously, the most common way of doing this was by passing and parsing XML.  As I've <a href="http://sqlfool.com/2008/11/performance-comparison-of-singleton-xml-and-tvp-inserts/" target="_blank">previously posted, </a> TVP's perform an astounding 94% faster than singleton inserts and 75% faster than XML inserts.  But for some reason, TVP's still aren't widely used and understood.  In this post, I'll walk you through how to use these and how to query the metadata for TVP's.  </p>
<p>I've <a href="http://sqlfool.com/2008/11/one-to-many-inserts-with-table-valued-parameters/" target="_blank">previously posted about what TVP's are</a> and how to use them.  But in honor of <a href="http://sears.com/zombies" target="_blank">Halloween this week</a>, I've updated my demo script:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Create some tables to work with */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">orders</span>
<span style="color: #808080;">&#40;</span>
      order_id      <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>   Not Null
    , orderDate     <span style="color: #0000FF;">DATE</span>                Not Null
    , customer      <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>         Not Null
&nbsp;
    <span style="color: #0000FF;">CONSTRAINT</span> PK_orders
        <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span><span style="color: #808080;">&#40;</span>order_id<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">orderDetails</span>
<span style="color: #808080;">&#40;</span>
      orderDetail_id    <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>   Not Null
    , order_id          <span style="color: #0000FF;">INT</span>                 Not Null
    , lineItem          <span style="color: #0000FF;">INT</span>                 Not Null
    , product           <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>         Not Null
&nbsp;
    <span style="color: #0000FF;">CONSTRAINT</span> PK_orderDetails
        <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span><span style="color: #808080;">&#40;</span>orderDetail_id<span style="color: #808080;">&#41;</span>
&nbsp;
    <span style="color: #0000FF;">CONSTRAINT</span> FK_orderDetails_orderID
        <span style="color: #0000FF;">FOREIGN</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#40;</span>order_id<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">REFERENCES</span> dbo.<span style="color: #202020;">orders</span><span style="color: #808080;">&#40;</span>order_id<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">/* Create our new table types */</span>
<span style="color: #0000FF;">CREATE</span> TYPE dbo.<span style="color: #202020;">orderTable</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span> 
<span style="color: #808080;">&#40;</span> 
      orderDate     <span style="color: #0000FF;">DATE</span>
    , customer      <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> TYPE dbo.<span style="color: #202020;">orderDetailTable</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span> 
<span style="color: #808080;">&#40;</span> 
      lineItem      <span style="color: #0000FF;">INT</span>
    , product       <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
&nbsp;
<span style="color: #008080;">/* Create a new procedure using a table-valued parameter */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> dbo.<span style="color: #202020;">insert_orderTVP_sp</span>
      @myOrderTable orderTable READONLY
    , @myOrderDetailTable orderDetailTable READONLY
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
    <span style="color: #0000FF;">DECLARE</span> @myOrderID <span style="color: #0000FF;">INT</span>;
&nbsp;
    <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">orders</span>
    <span style="color: #0000FF;">SELECT</span> orderDate
        , customer
    <span style="color: #0000FF;">FROM</span> @myOrderTable;
&nbsp;
    <span style="color: #0000FF;">SET</span> @myOrderID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">SCOPE_IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
    <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">orderDetails</span>
    <span style="color: #0000FF;">SELECT</span> @myOrderID
        , lineItem
        , product
    <span style="color: #0000FF;">FROM</span> @myOrderDetailTable;
&nbsp;
    <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">OFF</span>;
&nbsp;
<span style="color: #0000FF;">END</span>
GO
&nbsp;
&nbsp;
<span style="color: #008080;">/* Call our new proc! */</span>
<span style="color: #0000FF;">DECLARE</span> @myTableHeaderData <span style="color: #0000FF;">AS</span> orderTable
    , @myTableDetailData <span style="color: #0000FF;">AS</span> orderDetailTable;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myTableHeaderData
<span style="color: #808080;">&#40;</span>orderDate, customer<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'Zombie'</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myTableDetailData
<span style="color: #808080;">&#40;</span>lineItem, product<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">10</span>, <span style="color: #FF0000;">'Brains'</span> <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">20</span>, <span style="color: #FF0000;">'More Brains'</span>;
&nbsp;
<span style="color: #0000FF;">EXECUTE</span> dbo.<span style="color: #202020;">insert_orderTVP_sp</span> 
      @myTableHeaderData
    , @myTableDetailData;
&nbsp;
<span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @myTableHeaderData;
<span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @myTableDetailData;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myTableHeaderData
<span style="color: #808080;">&#40;</span>orderDate, customer<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'Vampire'</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myTableDetailData
<span style="color: #808080;">&#40;</span>lineItem, product<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">10</span>, <span style="color: #FF0000;">'Blood Type O+'</span> <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">20</span>, <span style="color: #FF0000;">'Blood Type B-'</span> <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">30</span>, <span style="color: #FF0000;">'Blood Type AB+'</span> <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">40</span>, <span style="color: #FF0000;">'Blood Type A+'</span>;
&nbsp;
<span style="color: #0000FF;">EXECUTE</span> dbo.<span style="color: #202020;">insert_orderTVP_sp</span> 
      @myTableHeaderData
    , @myTableDetailData;
&nbsp;
&nbsp;
<span style="color: #008080;">/* Check our data */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">orders</span>;
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">orderDetails</span>;</pre></div></div>

<p>Once you've run this, you should see the following data:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">order_id    orderDate  customer
----------- ---------- --------------------
1           2010-10-28 Zombie
2           2010-10-28 Vampire
&nbsp;
(2 row(s) affected)
&nbsp;
orderDetail_id order_id    lineItem    product
-------------- ----------- ----------- --------------------
1              1           10          Brains
2              1           20          More Brains
3              2           10          Blood Type O+
4              2           20          Blood Type B-
5              2           30          Blood Type AB+
6              2           40          Blood Type A+
&nbsp;
(6 row(s) affected)</pre></div></div>

<p>Now that we've successfully created a couple of table types to support our TVP's, how do we go back and find out which objects we've created?  You can query the sys.types catalog view to find out.  Just search for system_type_id 243, which identifies the record as a table type.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Let's check out our new data types */</span>
<span style="color: #0000FF;">SELECT</span> name
    , system_type_id
    , is_table_type
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">types</span>
<span style="color: #0000FF;">WHERE</span> system_type_id <span style="color: #808080;">=</span> <span style="color: #000;">243</span>;
GO</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">name                 system_type_id is_table_type
-------------------- -------------- -------------
orderTable           243            1
orderDetailTable     243            1
&nbsp;
(2 row(s) affected)</pre></div></div>

<p>Even better yet, you can use the sys.table_types catalog view.  This gives us the same information as sys.types but also gives us the type_table_object_id, which we'll need shortly.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> name
    , system_type_id
    , is_table_type
    , type_table_object_id
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">table_types</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">name                 system_type_id is_table_type type_table_object_id
-------------------- -------------- ------------- --------------------
orderTable           243            1             917578307
orderDetailTable     243            1             933578364
&nbsp;
(2 row(s) affected)</pre></div></div>

<p>What if you need to look up the table type definition?  You can do this using the type_table_object_id and joining to sys.columns.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> tt.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'table_type_name'</span>
    , c.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'column_name'</span>
    , t.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'data_type'</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">table_types</span> <span style="color: #0000FF;">AS</span> tt
<span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">columns</span> <span style="color: #0000FF;">AS</span> c
    <span style="color: #0000FF;">ON</span> tt.<span style="color: #202020;">type_table_object_id</span> <span style="color: #808080;">=</span> c.<span style="color: #FF00FF;">object_id</span>
<span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">types</span> <span style="color: #0000FF;">As</span> t
    <span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">system_type_id</span> <span style="color: #808080;">=</span> t.<span style="color: #202020;">system_type_id</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">table_type_name      column_name     data_type
-------------------- --------------- ---------------
orderTable           orderDate       date
orderDetailTable     lineItem        int
orderTable           customer        varchar
orderDetailTable     product         varchar
&nbsp;
(4 row(s) affected)</pre></div></div>

<p>And last, but certainly not least, how do we see if any procs are currently using the table types?  SQL Server 2008 makes this easy for us with the sys.dm_sql_referencing_entities DMV.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> referencing_schema_name, referencing_entity_name, referencing_id
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_sql_referencing_entities</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'dbo.orderTable'</span>, <span style="color: #FF0000;">'TYPE'</span><span style="color: #808080;">&#41;</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">referencing_schema_name referencing_entity_name referencing_id
----------------------- ----------------------- --------------
dbo                     insert_orderTVP_sp      949578421
&nbsp;
(1 row(s) affected)</pre></div></div>

<p>If you're wondering how to implement SQL Server TVP's in your .NET code, well... I can't tell you how to do it, but I can point you to a place that can.  Stephen Forte has <a href="http://www.stephenforte.net/PermaLink,guid,07dfeb00-d0b0-47dd-9761-3b4c9f160277.aspx" target="_blank">a post that explains how easy it is to do</a>.</p>
<p>So now that you have a better understanding of how to work with TVP's, why don't you go implement one in your environment and see how for yourself just how awesome it is?  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Oh, and Happy Halloween!</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/10/metadata-for-table-valued-parameters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rename Database Objects En Masse</title>
		<link>http://sqlfool.com/2010/09/rename-database-objects-en-masse/</link>
		<comments>http://sqlfool.com/2010/09/rename-database-objects-en-masse/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 15:55:22 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1375</guid>
		<description><![CDATA[Ever need to rename all objects in a database? Yeah, okay... it doesn't happen very often, but when it does, it can be time consuming. This recently came up as something I needed to do. When you consider all the tables involved and you add in defaults, indexes, and foreign keys, well... you can imagine [...]]]></description>
			<content:encoded><![CDATA[<p>Ever need to rename all objects in a database?  Yeah, okay... it doesn't happen very often, but when it does, it can be time consuming.  This recently came up as something I needed to do.  When you consider all the tables involved and you add in defaults, indexes, and foreign keys, well... you can imagine how the number of objects adds up quickly.  After doing a few tedious renames, it occurred to me that I could write a script to generate the rename scripts for me.  Work smarter, not harder, right?  For anyone interested, here's the script.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Select</span> name
    , <span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#93;</span> 
    , <span style="color: #0000FF;">Case</span> 
        <span style="color: #0000FF;">When</span> <span style="color: #808080;">&#91;</span>type<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'U'</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Execute sp_rename N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'old_'</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span>
        <span style="color: #0000FF;">When</span> <span style="color: #808080;">&#91;</span>type<span style="color: #808080;">&#93;</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'D'</span>, <span style="color: #FF0000;">'PK'</span>, <span style="color: #FF0000;">'F'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">Then</span> 
            <span style="color: #FF0000;">'Execute sp_rename N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'old_'</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'OBJECT'</span><span style="color: #FF0000;">''</span>
        <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'renameScript'</span>
    , <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> parent_object_id <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Then</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Else</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'sortOrder'</span>
<span style="color: #0000FF;">From</span> sys.<span style="color: #202020;">objects</span>
<span style="color: #0000FF;">Where</span> <span style="color: #808080;">&#91;</span>type<span style="color: #808080;">&#93;</span> In <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'D'</span>, <span style="color: #FF0000;">'PK'</span>, <span style="color: #FF0000;">'U'</span>, <span style="color: #FF0000;">'F'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">Union</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">Select</span> i.<span style="color: #202020;">name</span>
    , o.<span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#93;</span>
    , <span style="color: #FF0000;">'Execute sp_rename N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> o.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span> <span style="color: #808080;">+</span> i.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'old_'</span> <span style="color: #808080;">+</span> i.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'INDEX'</span><span style="color: #FF0000;">''</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'renameScript'</span>
    , <span style="color: #000;">0</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'sortOrder'</span>
<span style="color: #0000FF;">From</span> sys.<span style="color: #202020;">indexes</span> <span style="color: #0000FF;">As</span> i
<span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">objects</span> <span style="color: #0000FF;">As</span> o
    <span style="color: #0000FF;">On</span> i.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> o.<span style="color: #FF00FF;">object_id</span>
<span style="color: #0000FF;">Where</span> i.<span style="color: #202020;">is_primary_key</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #008080;">-- exclude PKs, we take care of that above</span>
    <span style="color: #808080;">AND</span> i.<span style="color: #202020;">type</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> <span style="color: #008080;">-- exclude heaps</span>
    <span style="color: #808080;">AND</span> o.<span style="color: #202020;">type</span> Not In <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'S'</span>, <span style="color: #FF0000;">'IT'</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- exclude system &amp; internal tables</span>
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> sortOrder;</pre></div></div>

<p>Be forewarned that I only tested this on a couple of databases, but it seemed to run without problem for tables, indexes, primary keys, defaults, and foreign keys.  The sortOrder column is there only to ensure that table renames are performed last.  Otherwise, your index renames would fail.  This will only run on SQL Server 2005 or 2008.  If you have any problems with the script, please let me know.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/09/rename-database-objects-en-masse/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Check VLF Counts</title>
		<link>http://sqlfool.com/2010/06/check-vlf-counts/</link>
		<comments>http://sqlfool.com/2010/06/check-vlf-counts/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 16:03:40 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tuning]]></category>
		<category><![CDATA[undocumented commands]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1350</guid>
		<description><![CDATA[Today I stumbled across a database with 87,302 VLF's. Yes, that's right... 87 THOUSAND. Most of our databases have a few dozen VLF's, but this was an old database that had grown to 1.5 TB and had the default autogrowth settings left in tact. How did we discover this? During a routine reboot of the [...]]]></description>
			<content:encoded><![CDATA[<p>Today I stumbled across a database with 87,302 VLF's.  Yes, that's right... 87 THOUSAND.  Most of our databases have a few dozen VLF's, but this was an old database that had grown to 1.5 TB and had the default autogrowth settings left in tact.  How did we discover this?  During a routine reboot of the server, this database took 30 minutes to recover, but there were no error messages or status messages in the log.  </p>
<p>Now, this blog post is not about VLF's or why you should keep the number of VLF's to a small, manageable number -- although I hear under 50 is a good rule of thumb.  No, the purpose of this blog post is to share a little script I wrote to check the number of VLF's each database uses:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> #stage<span style="color: #808080;">&#40;</span>
    FileID      <span style="color: #0000FF;">int</span>
  , FileSize    <span style="color: #0000FF;">bigint</span>
  , StartOffset <span style="color: #0000FF;">bigint</span>
  , FSeqNo      <span style="color: #0000FF;">bigint</span>
  , <span style="color: #808080;">&#91;</span>Status<span style="color: #808080;">&#93;</span>    <span style="color: #0000FF;">bigint</span>
  , Parity      <span style="color: #0000FF;">bigint</span>
  , CreateLSN   <span style="color: #0000FF;">numeric</span><span style="color: #808080;">&#40;</span><span style="color: #000;">38</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> #results<span style="color: #808080;">&#40;</span>
    Database_Name   sysname
  , VLF_count       <span style="color: #0000FF;">int</span> 
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">Exec</span> sp_msforeachdb N<span style="color: #FF0000;">'Use ?; 
            Insert Into #stage 
            Exec sp_executeSQL N'</span><span style="color: #FF0000;">'DBCC LogInfo(?)'</span><span style="color: #FF0000;">'; 
&nbsp;
            Insert Into #results 
            Select DB_Name(), Count(*) 
            From #stage; 
&nbsp;
            Truncate Table #stage;'</span>
&nbsp;
<span style="color: #0000FF;">Select</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">From</span> #results
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> VLF_count <span style="color: #0000FF;">Desc</span>;
&nbsp;
<span style="color: #0000FF;">Drop</span> <span style="color: #0000FF;">Table</span> #stage;
<span style="color: #0000FF;">Drop</span> <span style="color: #0000FF;">Table</span> #results;</pre></div></div>

<p>This script is low-impact and is safe to run on large, production databases during business hours.  However, just be aware that it's using some undocumented commands.  </p>
<p>For more information on VLF's, check out these excellent articles:</p>
<ul>
<li><a href="http://www.sqlskills.com/BLOGS/KIMBERLY/post/Transaction-Log-VLFs-too-many-or-too-few.aspx" target="_blank">Transaction Log VLF's - Too Many or Too Few?</a></li>
<li><a href="http://technet.microsoft.com/en-us/magazine/2009.02.logging.aspx#id0060025" target="_blank">Understanding Logging &#038; Recovery</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/06/check-vlf-counts/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Filtered Indexes Work-Around</title>
		<link>http://sqlfool.com/2010/02/filtered-indexes-work-around/</link>
		<comments>http://sqlfool.com/2010/02/filtered-indexes-work-around/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 16:00:14 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[2008]]></category>
		<category><![CDATA[filtered indexes]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1307</guid>
		<description><![CDATA[Recently, I needed to create a stored procedure that queried a rather large table. The table has a filtered index on a date column, and it covers the query. However, the Query Optimizer was not using the index, which was increasing the execution time (not to mention IO!) by at least 10x. This wasn't the [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I needed to create a stored procedure that queried a rather large table.  The table has a filtered index on a date column, and it covers the query.  However, the Query Optimizer was not using the index, which was increasing the execution time (not to mention IO!) by at least 10x.  This wasn't the first time I've had the Optimizer fail to use a filtered index.  Normally when this happens, I use a table hint to force the filtered index -- after I verify that it is indeed faster, of course.  However, since this was a stored procedure, I was receiving the following error message whenever I tried to execute the proc:</p>
<p><span style="color: #ff0000;">Query processor could not produce a query plan because of the hints defined in this query. Resubmit the query without specifying any hints and without using SET FORCEPLAN.</span></p>
<p>SQL Server would not allow me to execute the stored procedure using the filtered index hint.  If I removed the hint, it executed, but it used a different, non-covering and far more expensive index.  For those of you not familiar with this issue, allow me to illustrate the problem.</p>
<p>First, create a table to play with and populate it with some bogus data:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> dbo.<span style="color: #202020;">filteredIndexTest</span>
<span style="color: #808080;">&#40;</span>
      myID   <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">Identity</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">3</span><span style="color: #808080;">&#41;</span>
    , myDate <span style="color: #0000FF;">smalldatetime</span> 
    , myData <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
&nbsp;
    <span style="color: #0000FF;">Constraint</span> PK_filteredIndexTest
        <span style="color: #0000FF;">Primary</span> <span style="color: #0000FF;">Key</span> <span style="color: #0000FF;">Clustered</span><span style="color: #808080;">&#40;</span>myID<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Declare</span> @<span style="color: #0000FF;">date</span> <span style="color: #0000FF;">smalldatetime</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'2010-01-01'</span>;
&nbsp;
<span style="color: #0000FF;">While</span> @<span style="color: #0000FF;">date</span> <span style="color: #808080;">&lt;</span> <span style="color: #FF0000;">'2010-02-01'</span>
<span style="color: #0000FF;">Begin</span>
&nbsp;
    <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> dbo.<span style="color: #202020;">filteredIndexTest</span>
    <span style="color: #808080;">&#40;</span>
          myDate
        , myData
    <span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">Select</span> @<span style="color: #0000FF;">date</span>
        , <span style="color: #FF0000;">'Date: '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">Convert</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>, @<span style="color: #0000FF;">date</span>, <span style="color: #000;">102</span><span style="color: #808080;">&#41;</span>;
&nbsp;
    <span style="color: #0000FF;">Set</span> @<span style="color: #0000FF;">date</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">DateAdd</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">minute</span>, <span style="color: #000;">1</span>, @<span style="color: #0000FF;">date</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">End</span>;
&nbsp;
<span style="color: #0000FF;">Select</span> <span style="color: #FF00FF;">Count</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">From</span> dbo.<span style="color: #202020;">filteredIndexTest</span>;</pre></div></div>

<p>It looks like this will generate 44,640 rows of test data... plenty enough for our purposes.  Now, let's create our filtered index and write a query that will use it:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">NonClustered</span> <span style="color: #0000FF;">Index</span> IX_filteredIndexTest_1
    <span style="color: #0000FF;">On</span> dbo.<span style="color: #202020;">filteredIndexTest</span><span style="color: #808080;">&#40;</span>myDate<span style="color: #808080;">&#41;</span>
    Include <span style="color: #808080;">&#40;</span>myData<span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">Where</span> myDate <span style="color: #808080;">&gt;=</span> <span style="color: #FF0000;">'2010-01-27'</span>;
&nbsp;
<span style="color: #0000FF;">Select</span> <span style="color: #0000FF;">Distinct</span> myData
<span style="color: #0000FF;">From</span> dbo.<span style="color: #202020;">filteredIndexTest</span>
<span style="color: #0000FF;">Where</span> myDate <span style="color: #808080;">&gt;=</span> <span style="color: #FF0000;">'2010-01-28'</span>;</pre></div></div>

<p>If you look at the execution plan for this query, you'll notice that the Optimizer is using the filtered index.  Perfect!  Now let's parameterize it.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Declare</span> @myDate1 <span style="color: #0000FF;">smalldatetime</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'2010-01-28'</span>;
&nbsp;
<span style="color: #0000FF;">Select</span> <span style="color: #0000FF;">Distinct</span> myData
<span style="color: #0000FF;">From</span> dbo.<span style="color: #202020;">filteredIndexTest</span>
<span style="color: #0000FF;">Where</span> myDate <span style="color: #808080;">&gt;=</span> @myDate1;</pre></div></div>

<p>Uh oh.  Looking at the execution plan, we see that SQL Server is no longer using the filtered index.  Instead, it's scanning the clustered index!  Why is this?  There's actually a good explanation for it.  The reason is that I could, in theory, pass a date to my parameter that fell outside of the filtered date range.  If that's the case, then SQL Server could not utilize the filtered index.  Personally, I think it's a bug and SQL Server should identify whether or not a filtered index could be used based on the actual value submitted, but... that's a whole other blog post.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   </p>
<p>So what can we do?  Well, dynamic SQL may be able to help us out in this case.  Let's give it a go.  First, let's try parameterized dynamic SQL.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Declare</span> @mySQL1 <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2000</span><span style="color: #808080;">&#41;</span>
    , @myParam <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2000</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'@p_myDate2 smalldatetime'</span>
    , @myDate2 <span style="color: #0000FF;">smalldatetime</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'2010-01-28'</span>;
&nbsp;
<span style="color: #0000FF;">Set</span> @mySQL1 <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Select Distinct myData
              From dbo.filteredIndexTest
              Where myDate &gt;= @p_myDate2'</span>;
&nbsp;
<span style="color: #0000FF;">Execute</span> <span style="color: #AF0000;">sp_executeSQL</span> @mySQL1, @myParam, @p_myDate2 <span style="color: #808080;">=</span> @myDate2;</pre></div></div>

<p>Looking at the execution plan, we see we're still scanning on the clustered index.  This is because the parameterized dynamic SQL resolves to be the exact same query as the one above it.  Let's try unparameterized SQL instead:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Declare</span> @mySQL2 <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2000</span><span style="color: #808080;">&#41;</span>
    , @myDate3 <span style="color: #0000FF;">smalldatetime</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'2010-01-28'</span>;
&nbsp;
<span style="color: #0000FF;">Set</span> @mySQL2 <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Select Distinct myData
              From dbo.filteredIndexTest
              Where myDate &gt;= '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>@myDate3 <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span>;
&nbsp;
<span style="color: #0000FF;">Execute</span> <span style="color: #AF0000;">sp_executeSQL</span> @mySQL2;
&nbsp;
<span style="color: #008080;">-- Drop Table dbo.filteredIndexTest;</span></pre></div></div>

<p>Voila!  We have a seek on our filtered index.  Why?  Because the statement resolves to be identical to our first query, where we hard-coded the date value in the WHERE clause.</p>
<p>Now, I want to stress this fact: you should always, ALWAYS use parameterized dynamic SQL whenever possible.  Not only is it safer, but it's also faster, because it can reuse cached plans.  But sometimes you just cannot accomplish the same tasks with it.  This is one of those times.  If you do end up needing to use unparameterized dynamic SQL as a work-around, please make sure you're validating your input, especially if you're interfacing with any sort of external source.</p>
<p>There's an even easier work-around for this problem that <a href="http://twitter.com/CrappyCodingGuy ">Dave</a> (<a href="http://www.crappycoding.com" target="_blank">http://www.crappycoding.com</a>) shared with me: <a href="http://msdn.microsoft.com/en-us/library/ms181714.aspx" target="_blank">recompile</a>.</p>
<p>Adding "Option (Recompile)" to the end of your statements will force the Optimizer to re-evaluate which index will best meet the needs of your query every time the statement is executed.  More importantly, it evaluates the plan based on the actual values passed to the parameter... just like in our hard-coded and dynamic SQL examples.  Let's see it in action:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @myDate4 <span style="color: #0000FF;">SMALLDATETIME</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'2010-01-28'</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> myData
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">filteredIndexTest</span> 
<span style="color: #0000FF;">WHERE</span> myDate <span style="color: #808080;">&gt;=</span> @myDate4
<span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span>RECOMPILE<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @myDate5 <span style="color: #0000FF;">SMALLDATETIME</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'2010-01-20'</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> myData
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">filteredIndexTest</span> 
<span style="color: #0000FF;">WHERE</span> myDate <span style="color: #808080;">&gt;=</span> @myDate5
<span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span>RECOMPILE<span style="color: #808080;">&#41;</span>;</pre></div></div>

<p>If we look at the execution plans for the 2 queries above, we see that the first query seeks on the filtered index, and the second query scans on the clustered index.  This is because the second query cannot be satisfied with the filtered index because we initially limited our index to dates greater than or equal to 1/27/2010.  </p>
<p>There are, of course, trade-offs associated with each approach, so use whichever one best meets your needs.  Do you have another work-around for this issue?  If so, please let me know.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Update:  </p>
<p>Alex Kuznetsov (<a href="http://www.simple-talk.com/author/alex-kuznetsov/">http://www.simple-talk.com/author/alex-kuznetsov/</a>) shared this method too:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @myDate1 <span style="color: #0000FF;">SMALLDATETIME</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'2010-01-28'</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> myData
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">filteredIndexTest</span>
<span style="color: #0000FF;">WHERE</span> myDate <span style="color: #808080;">=</span> @myDate1
<span style="color: #808080;">AND</span> myDate <span style="color: #808080;">&gt;=</span> <span style="color: #FF0000;">'2010-01-27'</span>;</pre></div></div>

<p>Like the other examples, this will result in an index seek on the filtered index.  Basically, by explicitly declaring the start date of your filter, you're letting the Optimizer know that the filtered index can satisfy the request, regardless of the parameter value passed.  Thanks for the tip, Alex!  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/02/filtered-indexes-work-around/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

