<?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; monitor</title>
	<atom:link href="http://sqlfool.com/tag/monitor/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com</link>
	<description>Self-Professed SQL Scripting Junkie!</description>
	<lastBuildDate>Wed, 02 May 2012 21:25:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<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&#8217;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&#8217;ve written the following script, which examines metadata values using sys.partitions.  This method isn&#8217;t as accurate as running SELECT COUNT(*) FROM, but it&#8217;s much faster.  Keep in mind, since it&#8217;s just looking at row counts, it&#8217;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&#8217;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>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&#8217;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&#8217;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&#8217;d share the script.  I would like to warn that this script is *not* perfect&#8230; namely, it doesn&#8217;t handle negative integer values very elegantly.  It also doesn&#8217;t know if you started your seed at zero, approached your max positive limit, then reseeded to the negative limit (see my &#8220;quick and dirty fix&#8221; 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>6</slash:comments>
		</item>
		<item>
		<title>Monitoring Process for Performance Counters</title>
		<link>http://sqlfool.com/2009/09/monitoring-process-for-performance-counters/</link>
		<comments>http://sqlfool.com/2009/09/monitoring-process-for-performance-counters/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 17:19:12 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[monitor]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1161</guid>
		<description><![CDATA[Recently I needed to create a process to monitor performance counters over a short period of time. We were going to implement a change and we wanted to compare performance before and after to see if there was any impact. To do this, I first created a couple of tables. One table is used to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I needed to create a process to monitor performance counters over a short period of time.  We were going to implement a change and we wanted to compare performance before and after to see if there was any impact.  </p>
<p>To do this, I first created a couple of tables.  One table is used to actually store the monitored values.  The second table is used for configuration; you insert only the counters you want to monitor.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Create the table to store our logged perfmon counters */</span>
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> dbo.<span style="color: #202020;">dba_perfCounterMonitor</span>
<span style="color: #808080;">&#40;</span>
      capture_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
    , captureDate   <span style="color: #0000FF;">smalldatetime</span>       Not Null
    , objectName    <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>       Not Null
    , counterName   <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>       Not Null
    , instanceName  <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>       Not Null
    , <span style="color: #0000FF;">value</span>         <span style="color: #0000FF;">float</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span>            Not Null
    , valueType     <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>        Not Null
&nbsp;
    <span style="color: #0000FF;">Constraint</span> PK_dba_perfCounterMonitor
        <span style="color: #0000FF;">Primary</span> <span style="color: #0000FF;">Key</span> <span style="color: #0000FF;">Clustered</span><span style="color: #808080;">&#40;</span>capture_id<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Create the table that controls which counters we're going to monitor */</span>
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> dbo.<span style="color: #202020;">dba_perfCounterMonitorConfig</span>
<span style="color: #808080;">&#40;</span>
      objectName    <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   Not Null
    , counterName   <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   Not Null
    , instanceName  <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   Null
<span style="color: #808080;">&#41;</span>;</pre></div></div>

<p>If you leave the instanceName NULL in the config table, it&#8217;ll monitor all instances.  Now we&#8217;re going to insert some sample performance counters into the config table.  The counters you&#8217;re interested in can, and likely will, vary.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Insert some perfmon counters to be monitored */</span>
<span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> dbo.<span style="color: #202020;">dba_perfCounterMonitorConfig</span>
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:Buffer Manager'</span>, <span style="color: #FF0000;">'Page Life Expectancy'</span>, Null <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:Locks'</span>, <span style="color: #FF0000;">'Lock Requests/sec'</span>, Null <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:Locks'</span>, <span style="color: #FF0000;">'Lock Waits/sec'</span>, Null <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:Locks'</span>, <span style="color: #FF0000;">'Lock Wait Time (ms)'</span>, Null <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:Buffer Manager'</span>, <span style="color: #FF0000;">'Page reads/sec'</span>, Null <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:Buffer Manager'</span>, <span style="color: #FF0000;">'Page writes/sec'</span>, Null <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:Buffer Manager'</span>, <span style="color: #FF0000;">'Buffer cache hit ratio'</span>, Null <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:Databases'</span>, <span style="color: #FF0000;">'Transactions/sec'</span>, <span style="color: #FF0000;">'AdventureWorks'</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'SQLServer:General Statistics'</span>, <span style="color: #FF0000;">'Processes blocked'</span>, Null;</pre></div></div>

<p>Now let&#8217;s create our proc.  This proc will run for a specified time period and will *average* the counters over that time.  I personally take snapshots every 15 seconds for 4 minutes; I have a scheduled task that runs this every 5 minutes.  It&#8217;s not perfect, but it gives me a good idea of what&#8217;s happening on the server.</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;">Procedure</span> dbo.<span style="color: #202020;">dba_perfCounterMonitor_sp</span>
&nbsp;
        <span style="color: #008080;">/* Declare Parameters */</span>
          @samplePeriod    <span style="color: #0000FF;">int</span>      <span style="color: #808080;">=</span>  <span style="color: #000;">240</span>  <span style="color: #008080;">/* how long to sample, in seconds */</span>
        , @sampleRate      <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:15'</span>  <span style="color: #008080;">/* how frequently to sample, in seconds */</span>
        , @displayResults  <span style="color: #0000FF;">bit</span>      <span style="color: #808080;">=</span>  <span style="color: #000;">0</span>  <span style="color: #008080;">/* display the results when done */</span>
<span style="color: #0000FF;">As</span>
<span style="color: #008080;">/*********************************************************************************
    Name:       dba_perfCounterMonitor_sp
&nbsp;
    Author:     Michelle Ufford, http://sqlfool.com
&nbsp;
    Purpose:    Monitors performance counters.  Uses the dba_perfCounterMonitorConfig
                table to manage which perf counters to monitor.  
&nbsp;
                @samplePeriod - specifies how long the process will try to monitor
                                performance counters; in seconds.
&nbsp;
                @sampleRate - how long inbetween samples; in seconds.
&nbsp;
                The average values over sample period is then logged to the
                dba_perfCounterMonitor table.
&nbsp;
    Notes:      There are 3 basic types of performance counter calculations:
&nbsp;
                Value/Base: these calculations require 2 counters. The value 
                            counter (cntr_type = 537003264) has to be divided 
                            by the base counter (cntr_type = 1073939712).
&nbsp;
                Per Second: these counters are store cumulative values; the
                            value must be compared at 2 different times to
                            calculate the difference (cntr_type = 537003264).
&nbsp;
                Point In Time:  these counters show what the value of the
                                counter is at the current point-in-time 
                                (cntr_type = 65792).  No calculation is 
                                necessary to derive the value.
&nbsp;
    Called by:  DBA
&nbsp;
    Date        User    Description
    ----------------------------------------------------------------------------
    2009-09-04  MFU     Initial Release
*********************************************************************************
    Exec dbo.dba_perfCounterMonitor_sp
          @samplePeriod     = 60
        , @sampleRate       = '00:00:01'
        , @displayResults   = 1;
*********************************************************************************/</span>
&nbsp;
<span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Set</span> XACT_Abort <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Set</span> Ansi_Padding <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Set</span> Ansi_Warnings <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Set</span> ArithAbort <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Set</span> Concat_Null_Yields_Null <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Set</span> Numeric_RoundAbort <span style="color: #0000FF;">Off</span>;
&nbsp;
<span style="color: #0000FF;">Begin</span>
&nbsp;
    <span style="color: #008080;">/* Declare Variables */</span>
    <span style="color: #0000FF;">Declare</span> @startTime <span style="color: #0000FF;">datetime</span>
        , @endTime <span style="color: #0000FF;">datetime</span>
        , @iteration <span style="color: #0000FF;">int</span>;
&nbsp;
    <span style="color: #0000FF;">Select</span> @startTime <span style="color: #808080;">=</span> <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>
        , @iteration <span style="color: #808080;">=</span> <span style="color: #000;">1</span>;
&nbsp;
    <span style="color: #0000FF;">Declare</span> @samples <span style="color: #0000FF;">Table</span>
    <span style="color: #808080;">&#40;</span>
          iteration     <span style="color: #0000FF;">int</span>             Not Null
        , objectName    <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   Not Null
        , counterName   <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   Not Null
        , instanceName  <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   Not Null
        , cntr_value    <span style="color: #0000FF;">float</span>           Not Null
        , base_value    <span style="color: #0000FF;">float</span>           Null
        , cntr_type     <span style="color: #0000FF;">bigint</span>          Not Null
    <span style="color: #808080;">&#41;</span>;
&nbsp;
    <span style="color: #0000FF;">Begin</span> Try
&nbsp;
        <span style="color: #008080;">/* Start a new transaction */</span>
        <span style="color: #0000FF;">Begin</span> <span style="color: #0000FF;">Transaction</span>;
&nbsp;
        <span style="color: #008080;">/* Grab all of our counters */</span>
        <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> @samples
        <span style="color: #0000FF;">Select</span> @iteration
            , <span style="color: #FF00FF;">RTrim</span><span style="color: #808080;">&#40;</span>dopc.<span style="color: #FF00FF;">object_name</span><span style="color: #808080;">&#41;</span>
            , <span style="color: #FF00FF;">RTrim</span><span style="color: #808080;">&#40;</span>dopc.<span style="color: #202020;">counter_name</span><span style="color: #808080;">&#41;</span>
            , <span style="color: #FF00FF;">RTrim</span><span style="color: #808080;">&#40;</span>dopc.<span style="color: #202020;">instance_name</span><span style="color: #808080;">&#41;</span>
            , <span style="color: #FF00FF;">RTrim</span><span style="color: #808080;">&#40;</span>dopc.<span style="color: #202020;">cntr_value</span><span style="color: #808080;">&#41;</span>
            , <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Select</span> cntr_value <span style="color: #0000FF;">From</span> sys.<span style="color: #202020;">dm_os_performance_counters</span> <span style="color: #0000FF;">As</span> dopc1
                <span style="color: #0000FF;">Where</span> dopc1.<span style="color: #FF00FF;">object_name</span> <span style="color: #808080;">=</span> pcml.<span style="color: #202020;">objectName</span>
                And dopc1.<span style="color: #202020;">counter_name</span> <span style="color: #808080;">=</span> pcml.<span style="color: #202020;">counterName</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' base'</span>
                And dopc1.<span style="color: #202020;">instance_name</span> <span style="color: #808080;">=</span> IsNull<span style="color: #808080;">&#40;</span>pcml.<span style="color: #202020;">instanceName</span>, dopc.<span style="color: #202020;">instance_name</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
            , dopc.<span style="color: #202020;">cntr_type</span>
        <span style="color: #0000FF;">From</span> sys.<span style="color: #202020;">dm_os_performance_counters</span> <span style="color: #0000FF;">As</span> dopc
        Join dbo.<span style="color: #202020;">dba_perfCounterMonitorConfig</span> <span style="color: #0000FF;">As</span> pcml
            <span style="color: #0000FF;">On</span> dopc.<span style="color: #FF00FF;">object_name</span> <span style="color: #808080;">=</span> pcml.<span style="color: #202020;">objectName</span>
                And dopc.<span style="color: #202020;">counter_name</span> <span style="color: #808080;">=</span> pcml.<span style="color: #202020;">counterName</span>
                And dopc.<span style="color: #202020;">instance_name</span> <span style="color: #808080;">=</span> IsNull<span style="color: #808080;">&#40;</span>pcml.<span style="color: #202020;">instanceName</span>, dopc.<span style="color: #202020;">instance_name</span><span style="color: #808080;">&#41;</span>;
&nbsp;
        <span style="color: #008080;">/* During our sample period, grab our counter values and store the results */</span>
        <span style="color: #0000FF;">While</span> <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&lt;</span> <span style="color: #FF00FF;">DateAdd</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">second</span>, @samplePeriod, @startTime<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">Begin</span>
&nbsp;
            <span style="color: #0000FF;">Set</span> @iteration <span style="color: #808080;">=</span> @iteration <span style="color: #808080;">+</span> <span style="color: #000;">1</span>;
&nbsp;
            <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> @samples
            <span style="color: #0000FF;">Select</span> @iteration
                , <span style="color: #FF00FF;">RTrim</span><span style="color: #808080;">&#40;</span>dopc.<span style="color: #FF00FF;">object_name</span><span style="color: #808080;">&#41;</span>
                , <span style="color: #FF00FF;">RTrim</span><span style="color: #808080;">&#40;</span>dopc.<span style="color: #202020;">counter_name</span><span style="color: #808080;">&#41;</span>
                , <span style="color: #FF00FF;">RTrim</span><span style="color: #808080;">&#40;</span>dopc.<span style="color: #202020;">instance_name</span><span style="color: #808080;">&#41;</span>
                , dopc.<span style="color: #202020;">cntr_value</span>
                , <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Select</span> cntr_value <span style="color: #0000FF;">From</span> sys.<span style="color: #202020;">dm_os_performance_counters</span> <span style="color: #0000FF;">As</span> dopc1
                    <span style="color: #0000FF;">Where</span> dopc1.<span style="color: #FF00FF;">object_name</span> <span style="color: #808080;">=</span> pcml.<span style="color: #202020;">objectName</span>
                    And dopc1.<span style="color: #202020;">counter_name</span> <span style="color: #808080;">=</span> pcml.<span style="color: #202020;">counterName</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' base'</span>
                    And dopc1.<span style="color: #202020;">instance_name</span> <span style="color: #808080;">=</span> IsNull<span style="color: #808080;">&#40;</span>pcml.<span style="color: #202020;">instanceName</span>, dopc.<span style="color: #202020;">instance_name</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
                , dopc.<span style="color: #202020;">cntr_type</span>
            <span style="color: #0000FF;">From</span> sys.<span style="color: #202020;">dm_os_performance_counters</span> <span style="color: #0000FF;">As</span> dopc
            Join dbo.<span style="color: #202020;">dba_perfCounterMonitorConfig</span> <span style="color: #0000FF;">As</span> pcml
                <span style="color: #0000FF;">On</span> dopc.<span style="color: #FF00FF;">object_name</span> <span style="color: #808080;">=</span> pcml.<span style="color: #202020;">objectName</span>
                    And dopc.<span style="color: #202020;">counter_name</span> <span style="color: #808080;">=</span> pcml.<span style="color: #202020;">counterName</span>
                    And dopc.<span style="color: #202020;">instance_name</span> <span style="color: #808080;">=</span> IsNull<span style="color: #808080;">&#40;</span>pcml.<span style="color: #202020;">instanceName</span>, dopc.<span style="color: #202020;">instance_name</span><span style="color: #808080;">&#41;</span>;
&nbsp;
            <span style="color: #008080;">/* Wait for a small delay */</span>
            <span style="color: #0000FF;">WaitFor</span> Delay @sampleRate;
&nbsp;
        <span style="color: #0000FF;">End</span>;
&nbsp;
        <span style="color: #008080;">/* Grab our end time for calculations */</span>
        <span style="color: #0000FF;">Set</span> @endTime <span style="color: #808080;">=</span> <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
        <span style="color: #008080;">/* Store the average of our point-in-time counters */</span>
        <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> dbo.<span style="color: #202020;">dba_perfCounterMonitor</span> 
        <span style="color: #808080;">&#40;</span>
			  captureDate
			, objectName
			, counterName
			, instanceName
			, <span style="color: #0000FF;">value</span>
			, valueType
		<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">Select</span> @startTime
		    , objectName
		    , counterName
		    , instanceName
		    , <span style="color: #FF00FF;">Avg</span><span style="color: #808080;">&#40;</span>cntr_value<span style="color: #808080;">&#41;</span>
		    , <span style="color: #FF0000;">'value'</span>
		<span style="color: #0000FF;">From</span> @samples
		<span style="color: #0000FF;">Where</span> cntr_type <span style="color: #808080;">=</span> <span style="color: #000;">65792</span>
		<span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">By</span> objectName
		    , counterName
		    , instanceName;
&nbsp;
        <span style="color: #008080;">/* Store the average of the value vs the base for cntr_type = 537003264 */</span>
        <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> dbo.<span style="color: #202020;">dba_perfCounterMonitor</span> 
        <span style="color: #808080;">&#40;</span>
			  captureDate
			, objectName
			, counterName
			, instanceName
			, <span style="color: #0000FF;">value</span>
			, valueType
		<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">Select</span> @startTime
		    , objectName
		    , counterName
		    , instanceName
		    , <span style="color: #FF00FF;">Avg</span><span style="color: #808080;">&#40;</span>cntr_value<span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #FF00FF;">Avg</span><span style="color: #808080;">&#40;</span>IsNull<span style="color: #808080;">&#40;</span>base_value, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
		    , <span style="color: #FF0000;">'percent'</span>
		<span style="color: #0000FF;">From</span> @samples
		<span style="color: #0000FF;">Where</span> cntr_type <span style="color: #808080;">=</span> <span style="color: #000;">537003264</span>
		<span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">By</span> objectName
		    , counterName
		    , instanceName;
&nbsp;
        <span style="color: #008080;">/* Compare the first and last values for our cumulative, per-second counters */</span>
        <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> dbo.<span style="color: #202020;">dba_perfCounterMonitor</span> 
        <span style="color: #808080;">&#40;</span>
			  captureDate
			, objectName
			, counterName
			, instanceName
			, <span style="color: #0000FF;">value</span>
			, valueType
		<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">Select</span> @startTime
		    , objectName
		    , counterName
		    , instanceName
		    , <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">Max</span><span style="color: #808080;">&#40;</span>cntr_value<span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">Min</span><span style="color: #808080;">&#40;</span>cntr_value<span style="color: #808080;">&#41;</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>, @startTime, @endTime<span style="color: #808080;">&#41;</span>
		    , <span style="color: #FF0000;">'value'</span>
		<span style="color: #0000FF;">From</span> @samples
		<span style="color: #0000FF;">Where</span> cntr_type <span style="color: #808080;">=</span> <span style="color: #000;">272696576</span>
        <span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">By</span> objectName
		    , counterName
		    , instanceName;
&nbsp;
        <span style="color: #008080;">/* Should we display the results of our most recent execution?  */</span>
        <span style="color: #0000FF;">If</span> @displayResults <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
            <span style="color: #0000FF;">Select</span> captureDate
                , objectName
                , counterName
                , instanceName
                , <span style="color: #0000FF;">value</span>
                , valueType
            <span style="color: #0000FF;">From</span> dbo.<span style="color: #202020;">dba_perfCounterMonitor</span> <span style="color: #0000FF;">With</span> <span style="color: #808080;">&#40;</span>NoLock<span style="color: #808080;">&#41;</span>
            <span style="color: #0000FF;">Where</span> captureDate <span style="color: #808080;">=</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>@startTime <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">smalldatetime</span><span style="color: #808080;">&#41;</span>
            <span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> objectName
                , counterName
                , instanceName;
&nbsp;
        <span style="color: #008080;">/* If you have an open transaction, commit it */</span>
        <span style="color: #0000FF;">If</span> <span style="color: #FF00FF;">@@TranCount</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span>
            <span style="color: #0000FF;">Commit</span> <span style="color: #0000FF;">Transaction</span>;
&nbsp;
    <span style="color: #0000FF;">End</span> Try
    <span style="color: #0000FF;">Begin</span> Catch
&nbsp;
        <span style="color: #008080;">/* Whoops, there was an error... rollback! */</span>
        <span style="color: #0000FF;">If</span> <span style="color: #FF00FF;">@@TranCount</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span>
            <span style="color: #0000FF;">Rollback</span> <span style="color: #0000FF;">Transaction</span>;
&nbsp;
        <span style="color: #008080;">/* Return an error message and log it */</span>
        <span style="color: #0000FF;">Execute</span> dbo.<span style="color: #202020;">dba_logError_sp</span>;
&nbsp;
    <span style="color: #0000FF;">End</span> Catch;
&nbsp;
    <span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">Off</span>;
    <span style="color: #0000FF;">Return</span> <span style="color: #000;">0</span>;
<span style="color: #0000FF;">End</span>
Go</pre></div></div>

<p>Like I said, it&#8217;s not perfect, but it gets the job done.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Getting an error about dba_logError_sp?  Take a look at my <a href="http://sqlfool.com/2008/12/error-handling-in-t-sql/" target="_blank">error handling proc</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2009/09/monitoring-process-for-performance-counters/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Replication Monitor</title>
		<link>http://sqlfool.com/2008/11/replication-monitor/</link>
		<comments>http://sqlfool.com/2008/11/replication-monitor/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 22:05:16 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[monitor]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=210</guid>
		<description><![CDATA[In my last blog post, I provided a script to view replication latency.  Ian Kirk took the script and ran with it, adding centralized execution and permanent logging. I&#8217;ve tweaked it a little bit further and deployed to production. So far, so good. Here&#8217;s the latest and greatest for those interested: If Object_ID&#40;'dbo.dba_replicationMonitor'&#41; Is Null [...]]]></description>
			<content:encoded><![CDATA[<p>In my last blog post, I provided a <a href="http://sqlfool.com/2008/11/checking-replication-latency-with-t-sql/" target="_blank">script to view replication latency</a>.  <a href="http://ilkirk.wordpress.com/" target="_blank">Ian Kirk</a> took the script and ran with it, adding centralized execution and permanent logging.  I&#8217;ve tweaked it a little bit further and deployed to production.  So far, so good.  </p>
<p>Here&#8217;s the latest and greatest for those interested:</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;">'dbo.dba_replicationMonitor'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">Is</span> Null
<span style="color: #0000FF;">Begin</span>
    <span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> dbo.<span style="color: #202020;">dba_replicationMonitor</span>
    <span style="color: #808080;">&#40;</span> 
          monitor_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
        , monitorDate           <span style="color: #0000FF;">smalldatetime</span>       Not Null 
        , publicationName       sysname             Not Null
        , publicationDB         sysname             Not Null
        , iteration             <span style="color: #0000FF;">int</span>                 Null
        , tracer_id             <span style="color: #0000FF;">int</span>                 Null
        , distributor_latency   <span style="color: #0000FF;">int</span>                 Null
        , subscriber            <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>       Null
        , subscriber_db         <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>       Null
        , subscriber_latency    <span style="color: #0000FF;">int</span>                 Null
        , overall_latency       <span style="color: #0000FF;">int</span>                 Null 
    <span style="color: #808080;">&#41;</span>;
<span style="color: #0000FF;">End</span>;
&nbsp;
<span style="color: #0000FF;">If</span> <span style="color: #FF00FF;">ObjectProperty</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">Object_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'dbo.dba_replicationLatencyMonitor_sp'</span><span style="color: #808080;">&#41;</span>, N<span style="color: #FF0000;">'IsProcedure'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #0000FF;">Begin</span>
    <span style="color: #0000FF;">Drop</span> <span style="color: #0000FF;">Procedure</span> dbo.<span style="color: #202020;">dba_replicationLatencyMonitor_sp</span>;
    <span style="color: #0000FF;">Print</span> <span style="color: #FF0000;">'Procedure dba_replicationLatencyMonitor_sp dropped'</span>;
<span style="color: #0000FF;">End</span>;
Go
&nbsp;
<span style="color: #0000FF;">Set</span> Quoted_Identifier <span style="color: #0000FF;">On</span>
Go
<span style="color: #0000FF;">Set</span> ANSI_Nulls <span style="color: #0000FF;">On</span>
Go
&nbsp;
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Procedure</span> dbo.<span style="color: #202020;">dba_replicationLatencyMonitor_sp</span>
&nbsp;
        <span style="color: #008080;">/* Declare Parameters */</span>
          @publicationToTest    sysname        <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'yourPublicationName'</span>
        , @publicationDB        sysname        <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'yourPublicationDB'</span>
        , @replicationDelay     <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;">=</span> N<span style="color: #FF0000;">'00:00:30'</span>
        , @iterations           <span style="color: #0000FF;">int</span>            <span style="color: #808080;">=</span> <span style="color: #000;">5</span>
        , @iterationDelay       <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;">=</span> N<span style="color: #FF0000;">'00:00:30'</span>
        , @displayResults       <span style="color: #0000FF;">bit</span>            <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
        , @deleteTokens         <span style="color: #0000FF;">bit</span>            <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #0000FF;">As</span>
<span style="color: #008080;">/*********************************************************************************
    Name:       dba_replicationLatencyMonitor_sp
&nbsp;
    Author:     Michelle F. Ufford
&nbsp;
    Purpose:    Retrieves the amount of replication latency in seconds
&nbsp;
    Notes:      Default settings will run 1 test every minute for 5 minutes.
&nbsp;
                @publicationToTest = defaults to yourPublicationName publication
&nbsp;
                @publicationDB = the database that is the source for the publication.
				    The tracer procs are found in the publishing DB.
&nbsp;
                @replicationDelay = how long to wait for the token to replicate;
                    probably should not set to anything less than 10 (in seconds)
&nbsp;
                @iterations = how many tokens you want to test
&nbsp;
                @iterationDelay = how long to wait between sending test tokens
                    (in seconds)
&nbsp;
                @displayResults = print results to screen when complete
&nbsp;
                @deleteTokens = whether you want to retain tokens when done
&nbsp;
    Called by:  DBA
&nbsp;
    Date        Initials    Description
    ----------------------------------------------------------------------------
    2008-11-20   MFU        Initial Release
    2008-11-24	 ILK        Tweaked to allow for centralized execution 
                            Replaced temp table with permanent table.
    2008-11-25   MFU        More tweaking, added publication data to 
                            dba_replicationMonitor, fixed NULL latency data,
                            moved dba_replicationMonitor creation out of proc
*********************************************************************************
    Exec dbo.dba_replicationLatencyMonitor_sp
          @publicationToTest    = N'myTestPublication'
        , @publicationDB        = N'sandbox_publisher'
        , @replicationDelay     = N'00:00:05'
        , @iterations           = 1
        , @iterationDelay       = N'00:00:05'
        , @displayResults       = 1
        , @deleteTokens         = 1;
*********************************************************************************/</span>
&nbsp;
<span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Set</span> XACT_Abort <span style="color: #0000FF;">On</span>;
&nbsp;
<span style="color: #0000FF;">Begin</span>
&nbsp;
    <span style="color: #008080;">/* Declare Variables */</span>
    <span style="color: #0000FF;">Declare</span> @currentIteration   <span style="color: #0000FF;">int</span>
          , @tokenID            <span style="color: #0000FF;">bigint</span>
          , @currentDateTime    <span style="color: #0000FF;">smalldatetime</span>
          , @sqlStatement       <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">200</span><span style="color: #808080;">&#41;</span>
          , @parmDefinition		<span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">500</span><span style="color: #808080;">&#41;</span>;
&nbsp;
    <span style="color: #0000FF;">Declare</span> @tokenResults <span style="color: #0000FF;">Table</span>
    <span style="color: #808080;">&#40;</span> 
          iteration             <span style="color: #0000FF;">int</span>             Null
        , tracer_id             <span style="color: #0000FF;">int</span>             Null
        , distributor_latency   <span style="color: #0000FF;">int</span>             Null
        , subscriber            <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>   Null
        , subscriber_db         <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>   Null
        , subscriber_latency    <span style="color: #0000FF;">int</span>             Null
        , overall_latency       <span style="color: #0000FF;">int</span>             Null 
    <span style="color: #808080;">&#41;</span>;
&nbsp;
    <span style="color: #008080;">/* Initialize our variables */</span>
    <span style="color: #0000FF;">Select</span> @currentIteration <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
         , @currentDateTime  <span style="color: #808080;">=</span> <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
    <span style="color: #0000FF;">While</span> @currentIteration <span style="color: #808080;">&lt;</span> @iterations
    <span style="color: #0000FF;">Begin</span>
&nbsp;
		<span style="color: #008080;">/* Prepare the stored procedure execution string */</span>
		<span style="color: #0000FF;">Set</span> @sqlStatement <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'Execute '</span> <span style="color: #808080;">+</span> @publicationDB <span style="color: #808080;">+</span> N<span style="color: #FF0000;">'.sys.sp_postTracerToken '</span> <span style="color: #808080;">+</span> 
							N<span style="color: #FF0000;">'@publication = @VARpublicationToTest , '</span> <span style="color: #808080;">+</span>
							N<span style="color: #FF0000;">'@tracer_token_id = @VARtokenID OutPut;'</span>
&nbsp;
		<span style="color: #008080;">/* Define the parameters used by the sp_ExecuteSQL later */</span>
		<span style="color: #0000FF;">Set</span> @parmDefinition <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'@VARpublicationToTest sysname, '</span> <span style="color: #808080;">+</span>
			N<span style="color: #FF0000;">'@VARtokenID bigint OutPut'</span>;
&nbsp;
        <span style="color: #008080;">/* Insert a new tracer token in the publication database */</span>
        <span style="color: #0000FF;">Execute</span> <span style="color: #AF0000;">sp_executesql</span> 
              @sqlStatement
            , @parmDefinition
            , @VARpublicationToTest <span style="color: #808080;">=</span> @publicationToTest
            , @VARtokenID <span style="color: #808080;">=</span> @TokenID <span style="color: #0000FF;">OutPut</span>;
&nbsp;
        <span style="color: #008080;">/* Give a few seconds to allow the record to reach the subscriber */</span>
        <span style="color: #0000FF;">WaitFor</span> Delay @replicationDelay;
&nbsp;
        <span style="color: #008080;">/* Prepare our statement to retrieve tracer token data */</span>
        <span style="color: #0000FF;">Select</span> @sqlStatement <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Execute '</span> <span style="color: #808080;">+</span> @publicationDB <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.sys.sp_helpTracerTokenHistory '</span> <span style="color: #808080;">+</span>
                    N<span style="color: #FF0000;">'@publication = @VARpublicationToTest , '</span> <span style="color: #808080;">+</span>
                    N<span style="color: #FF0000;">'@tracer_id = @VARtokenID'</span>
            , @parmDefinition <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'@VARpublicationToTest sysname, '</span> <span style="color: #808080;">+</span>
                    N<span style="color: #FF0000;">'@VARtokenID bigint'</span>;
&nbsp;
        <span style="color: #008080;">/* Store our results for retrieval later */</span>
        <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> @tokenResults
        <span style="color: #808080;">&#40;</span>
            distributor_latency
          , subscriber
          , subscriber_db
          , subscriber_latency
          , overall_latency
        <span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">Execute</span> <span style="color: #AF0000;">sp_executesql</span> 
              @sqlStatement
            , @parmDefinition
            , @VARpublicationToTest <span style="color: #808080;">=</span> @publicationToTest
            , @VARtokenID <span style="color: #808080;">=</span> @TokenID;
&nbsp;
        <span style="color: #008080;">/* Assign the iteration and token id to the results for easier investigation */</span>
        <span style="color: #0000FF;">Update</span> @tokenResults
        <span style="color: #0000FF;">Set</span> iteration <span style="color: #808080;">=</span> @currentIteration <span style="color: #808080;">+</span> <span style="color: #000;">1</span>
          , tracer_id <span style="color: #808080;">=</span> @tokenID
        <span style="color: #0000FF;">Where</span> iteration <span style="color: #0000FF;">Is</span> Null;
&nbsp;
        <span style="color: #008080;">/* Wait for the specified time period before creating another token */</span>
        <span style="color: #0000FF;">WaitFor</span> Delay @iterationDelay;
&nbsp;
        <span style="color: #008080;">/* Avoid endless looping... :) */</span>
        <span style="color: #0000FF;">Set</span> @currentIteration <span style="color: #808080;">=</span> @currentIteration <span style="color: #808080;">+</span> <span style="color: #000;">1</span>;
&nbsp;
    <span style="color: #0000FF;">End</span>;
&nbsp;
    <span style="color: #008080;">/* Display our results */</span>
    <span style="color: #0000FF;">If</span> @displayResults <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    <span style="color: #0000FF;">Begin</span>
        <span style="color: #0000FF;">Select</span> 
              iteration
            , tracer_id
            , IsNull<span style="color: #808080;">&#40;</span>distributor_latency, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'distributor_latency'</span>
            , subscriber
            , subscriber_db
            , IsNull<span style="color: #808080;">&#40;</span>subscriber_latency, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'subscriber_latency'</span>
            , IsNull<span style="color: #808080;">&#40;</span>overall_latency, 
                IsNull<span style="color: #808080;">&#40;</span>distributor_latency, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> IsNull<span style="color: #808080;">&#40;</span>subscriber_latency, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
                <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'overall_latency'</span>
        <span style="color: #0000FF;">From</span> @tokenResults;
    <span style="color: #0000FF;">End</span>;
&nbsp;
    <span style="color: #008080;">/* Store our results */</span>
    <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> dbo.<span style="color: #202020;">dba_replicationMonitor</span>
    <span style="color: #808080;">&#40;</span>
          monitorDate
        , publicationName
        , publicationDB
        , iteration
        , tracer_id
        , distributor_latency
        , subscriber
        , subscriber_db
        , subscriber_latency
        , overall_latency
    <span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">Select</span> 
          @currentDateTime
        , @publicationToTest
        , @publicationDB
        , iteration
        , tracer_id
        , IsNull<span style="color: #808080;">&#40;</span>distributor_latency, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
        , subscriber
        , subscriber_db
        , IsNull<span style="color: #808080;">&#40;</span>subscriber_latency, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
        , IsNull<span style="color: #808080;">&#40;</span>overall_latency, 
            IsNull<span style="color: #808080;">&#40;</span>distributor_latency, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> IsNull<span style="color: #808080;">&#40;</span>subscriber_latency, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">From</span> @tokenResults;
&nbsp;
    <span style="color: #008080;">/* Delete the tracer tokens if requested */</span>
    <span style="color: #0000FF;">If</span> @deleteTokens <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 <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Execute '</span> <span style="color: #808080;">+</span> @publicationDB <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.sys.sp_deleteTracerTokenHistory '</span> <span style="color: #808080;">+</span>
                    N<span style="color: #FF0000;">'@publication = @VARpublicationToTest , '</span> <span style="color: #808080;">+</span>
                    N<span style="color: #FF0000;">'@cutoff_date = @VARcurrentDateTime'</span>
            , @parmDefinition <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'@VARpublicationToTest sysname, '</span> <span style="color: #808080;">+</span>
                    N<span style="color: #FF0000;">'@VARcurrentDateTime datetime'</span>;
&nbsp;
        <span style="color: #0000FF;">Execute</span> <span style="color: #AF0000;">sp_executesql</span> 
              @sqlStatement
            , @parmDefinition
            , @VARpublicationToTest <span style="color: #808080;">=</span> @publicationToTest
            , @VARcurrentDateTime <span style="color: #808080;">=</span> @currentDateTime;
&nbsp;
    <span style="color: #0000FF;">End</span>;
&nbsp;
    <span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">Off</span>;
    <span style="color: #0000FF;">Return</span> <span style="color: #000;">0</span>;
<span style="color: #0000FF;">End</span>
Go
&nbsp;
<span style="color: #0000FF;">Set</span> Quoted_Identifier <span style="color: #0000FF;">Off</span>;
Go
<span style="color: #0000FF;">Set</span> ANSI_Nulls <span style="color: #0000FF;">On</span>;
Go</pre></div></div>

<p>&nbsp;</p>
<p>Note: All of my stored procedures have standardized error handling that I remove before posting to avoid confusion; you may want to implement your own error handling.</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2008/11/replication-monitor/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Checking Replication Latency with T-SQL</title>
		<link>http://sqlfool.com/2008/11/checking-replication-latency-with-t-sql/</link>
		<comments>http://sqlfool.com/2008/11/checking-replication-latency-with-t-sql/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 20:43:44 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[monitor]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=204</guid>
		<description><![CDATA[This post may only appeal to small subset of DBA&#8217;s. This particular script was born of a need to view replication latency in production. I had to investigate whether the replication latency issues we were experiencing in production were occurring on the publisher (us) or the subscriber (them), and address accordingly. &#8220;Why just not use [...]]]></description>
			<content:encoded><![CDATA[<p>This post may only appeal to small subset of DBA&#8217;s.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>This particular script was born of a need to view replication latency in production.  I had to investigate whether the replication latency issues we were experiencing in production were occurring on the publisher (us) or the subscriber (them), and address accordingly.  &#8220;Why just not use Replication Monitor?&#8221;, you might ask.  Good question!  I would definitely use Replication Monitor if it were available to me; however, I do not have the necessary permissions to the Distributor.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  </p>
<p>Fortunately, SQL 2005 provides us with some tools to help: <a href="http://msdn.microsoft.com/en-us/library/ms176091.aspx" target="_blank">sp_postTracerToken</a> and <a href="http://msdn.microsoft.com/en-us/library/ms187349(SQL.90).aspx" target="_blank">sp_helpTracerTokenHistory</a>.  This allows us to access the same Tracer Tokens feature that is in Replication Monitor. </p>
<br />
<img src="http://sqlfool.com/blogImages/20081120/ReplicationLatency_Tracer.JPG" alt="Replication Monitor - Tracer Tokens" width="400"/>
<p>&nbsp;</p>
<p>So that&#8217;s the backstory, and here&#8217;s the script I came up with.  For those interested, it&#8217;d be pretty easy to modify this script to use a permanent table and regularly log replication latency.</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;">Procedure</span> dbo.<span style="color: #202020;">dba_replicationLatencyGet_sp</span>
&nbsp;
        <span style="color: #008080;">/* Declare Parameters */</span>
          @publicationToTest sysname        <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'yourPublicationName'</span>
        , @replicationDelay  <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;">=</span> N<span style="color: #FF0000;">'00:00:30'</span>
        , @iterations        <span style="color: #0000FF;">int</span>            <span style="color: #808080;">=</span> <span style="color: #000;">5</span>
        , @iterationDelay    <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;">=</span> N<span style="color: #FF0000;">'00:00:30'</span>
        , @deleteTokens      <span style="color: #0000FF;">bit</span>            <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
        , @deleteTempTable   <span style="color: #0000FF;">bit</span>            <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #0000FF;">As</span>
<span style="color: #008080;">/*********************************************************************************
    Name:       dba_replicationLatencyGet_sp
&nbsp;
    Author:     Michelle F. Ufford
&nbsp;
    Purpose:    Retrieves the amount of replication latency in seconds
&nbsp;
    Notes:      Default settings will run 1 test every minute for 5 minutes.
&nbsp;
                @publicationToTest = change the default to your publication
&nbsp;
                @replicationDelay = how long to wait for the token to replicate;
                    probably should not set to anything less than 10 (in seconds)
&nbsp;
                @iterations = how many tokens you want to test
&nbsp;
                @iterationDelay = how long to wait between sending test tokens
                    (in seconds)
&nbsp;
                @deleteTokens = whether you want to retain tokens when done
&nbsp;
                @deleteTempTable = whether or not to retain the temporary table
                    when done.  Data stored to ##tokenResults; set @deleteTempTable 
                    flag to 0 if you do not want to delete when done.
&nbsp;
    Called by:  DBA
&nbsp;
    Date        Initials    Description
    ----------------------------------------------------------------------------
    2008-11-20   MFU        Initial Release
*********************************************************************************
    Exec dbo.dba_replicationLatencyGet_sp
          @publicationToTest    = N'yourPublicationName'
        , @replicationDelay     = N'00:00:05'
        , @iterations           = 1
        , @iterationDelay       = N'00:00:05'
        , @deleteTokens         = 1
        , @deleteTempTable      = 1;
*********************************************************************************/</span>
&nbsp;
<span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">On</span>;
<span style="color: #0000FF;">Set</span> XACT_Abort <span style="color: #0000FF;">On</span>;
&nbsp;
<span style="color: #0000FF;">Begin</span>
&nbsp;
    <span style="color: #008080;">/* Declare Variables */</span>
    <span style="color: #0000FF;">Declare</span> @currentIteration   <span style="color: #0000FF;">int</span>
          , @tokenID            <span style="color: #0000FF;">bigint</span>
          , @currentDateTime    <span style="color: #0000FF;">smalldatetime</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.dbo.##tokenResults'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">Is</span> Null
    <span style="color: #0000FF;">Begin</span>
        <span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> ##tokenResults
                        <span style="color: #808080;">&#40;</span> iteration           <span style="color: #0000FF;">int</span>             Null
                        , tracer_id           <span style="color: #0000FF;">int</span>             Null
                        , distributor_latency <span style="color: #0000FF;">int</span>             Null
                        , subscriber          <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>   Null
                        , subscriber_db       <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>   Null
                        , subscriber_latency  <span style="color: #0000FF;">int</span>             Null
                        , overall_latency     <span style="color: #0000FF;">int</span>             Null <span style="color: #808080;">&#41;</span>;
    <span style="color: #0000FF;">End</span>;
&nbsp;
    <span style="color: #008080;">/* Initialize our variables */</span>
    <span style="color: #0000FF;">Select</span> @currentIteration <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
         , @currentDateTime  <span style="color: #808080;">=</span> <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
    <span style="color: #0000FF;">While</span> @currentIteration <span style="color: #808080;">&lt;</span> @iterations
    <span style="color: #0000FF;">Begin</span>
&nbsp;
        <span style="color: #008080;">/* Insert a new tracer token in the publication database */</span>
        <span style="color: #0000FF;">Execute</span> sys.<span style="color: #202020;">sp_postTracerToken</span> 
          @publication <span style="color: #808080;">=</span> @publicationToTest,
          @tracer_token_id <span style="color: #808080;">=</span> @tokenID <span style="color: #0000FF;">OutPut</span>;
&nbsp;
        <span style="color: #008080;">/* Give a few seconds to allow the record to reach the subscriber */</span>
        <span style="color: #0000FF;">WaitFor</span> Delay @replicationDelay;
&nbsp;
        <span style="color: #008080;">/* Store our results in a temp table for retrieval later */</span>
        <span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> ##tokenResults
        <span style="color: #808080;">&#40;</span>
            distributor_latency
          , subscriber
          , subscriber_db
          , subscriber_latency
          , overall_latency
        <span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">Execute</span> sys.<span style="color: #202020;">sp_helpTracerTokenHistory</span> @publicationToTest, @tokenID;
&nbsp;
        <span style="color: #008080;">/* Assign the iteration and token id to the results for easier investigation */</span>
        <span style="color: #0000FF;">Update</span> ##tokenResults
        <span style="color: #0000FF;">Set</span> iteration <span style="color: #808080;">=</span> @currentIteration <span style="color: #808080;">+</span> <span style="color: #000;">1</span>
          , tracer_id <span style="color: #808080;">=</span> @tokenID
        <span style="color: #0000FF;">Where</span> iteration <span style="color: #0000FF;">Is</span> Null;
&nbsp;
        <span style="color: #008080;">/* Wait for the specified time period before creating another token */</span>
        <span style="color: #0000FF;">WaitFor</span> Delay @iterationDelay;
&nbsp;
        <span style="color: #008080;">/* Avoid endless looping... :) */</span>
        <span style="color: #0000FF;">Set</span> @currentIteration <span style="color: #808080;">=</span> @currentIteration <span style="color: #808080;">+</span> <span style="color: #000;">1</span>;
&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> ##tokenResults;
&nbsp;
    <span style="color: #0000FF;">If</span> @deleteTempTable <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    <span style="color: #0000FF;">Begin</span>
        <span style="color: #0000FF;">Drop</span> <span style="color: #0000FF;">Table</span> ##tokenResults;
    <span style="color: #0000FF;">End</span>;
&nbsp;
    <span style="color: #0000FF;">If</span> @deleteTokens <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    <span style="color: #0000FF;">Begin</span>
       <span style="color: #0000FF;">Execute</span> sp_deleteTracerTokenHistory @publication <span style="color: #808080;">=</span> @publicationToTest, @cutoff_date <span style="color: #808080;">=</span> @currentDateTime;
    <span style="color: #0000FF;">End</span>;
&nbsp;
    <span style="color: #0000FF;">Set</span> <span style="color: #0000FF;">NoCount</span> <span style="color: #0000FF;">Off</span>;
    <span style="color: #0000FF;">Return</span> <span style="color: #000;">0</span>;
<span style="color: #0000FF;">End</span>
Go</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2008/11/checking-replication-latency-with-t-sql/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

