<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: A TOP Query	</title>
	<atom:link href="https://lobsterpot.com.au/blog/2013/03/11/a-top-query/feed/" rel="self" type="application/rss+xml" />
	<link>https://lobsterpot.com.au/blog/2013/03/11/a-top-query/</link>
	<description></description>
	<lastBuildDate>Wed, 14 May 2014 04:06:57 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2013/03/11/a-top-query/#comment-2226</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Wed, 14 May 2014 04:06:57 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3215#comment-2226</guid>

					<description><![CDATA[Hi Nick,
Yes, that method works very well for finding gaps too.
Rob]]></description>
			<content:encoded><![CDATA[<p>Hi Nick,<br />
Yes, that method works very well for finding gaps too.<br />
Rob</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Nick		</title>
		<link>https://lobsterpot.com.au/blog/2013/03/11/a-top-query/#comment-2225</link>

		<dc:creator><![CDATA[Nick]]></dc:creator>
		<pubDate>Tue, 13 May 2014 15:34:31 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3215#comment-2225</guid>

					<description><![CDATA[Hi Rob,
yes, you&#039;re right of course.
Though one thing is to identify the extents (upper and lower limit) of gaps, another is to identify the actual missing keys within the gaps, which is what mine does.
I gave it a try with ROW_NUMBER, but before I got it working I suddenly also remembered the new LAG and LEAD functions. This is what I got working:
;with cte as
(
	select 
	lag(key) over(order by key) as previous_key,
	key as current_key
	from tbl
)
select
previous_key as gap_start, 
current_key as gap_end
from cte
where current_key-previous_key &#062; 1
order by current_key]]></description>
			<content:encoded><![CDATA[<p>Hi Rob,<br />
yes, you&#8217;re right of course.<br />
Though one thing is to identify the extents (upper and lower limit) of gaps, another is to identify the actual missing keys within the gaps, which is what mine does.<br />
I gave it a try with ROW_NUMBER, but before I got it working I suddenly also remembered the new LAG and LEAD functions. This is what I got working:<br />
;with cte as<br />
(<br />
	select<br />
	lag(key) over(order by key) as previous_key,<br />
	key as current_key<br />
	from tbl<br />
)<br />
select<br />
previous_key as gap_start,<br />
current_key as gap_end<br />
from cte<br />
where current_key-previous_key &gt; 1<br />
order by current_key</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2013/03/11/a-top-query/#comment-2224</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Tue, 13 May 2014 03:56:45 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3215#comment-2224</guid>

					<description><![CDATA[Hi Nick,
Using ROW_NUMBER() is a better option for generating a table of numbers, as it realises it can stop if you only need a few dozen rows. Your version doesn&#039;t get to leverage that. But actually, to find gaps, you don&#039;t even need a table of numbers. Just use ROW_NUMBER(), and look for the times when the difference between the values with gaps and the rownum increases (indicating that things have been missed).
Hope this helps,
Rob]]></description>
			<content:encoded><![CDATA[<p>Hi Nick,<br />
Using ROW_NUMBER() is a better option for generating a table of numbers, as it realises it can stop if you only need a few dozen rows. Your version doesn&#8217;t get to leverage that. But actually, to find gaps, you don&#8217;t even need a table of numbers. Just use ROW_NUMBER(), and look for the times when the difference between the values with gaps and the rownum increases (indicating that things have been missed).<br />
Hope this helps,<br />
Rob</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Nick		</title>
		<link>https://lobsterpot.com.au/blog/2013/03/11/a-top-query/#comment-2223</link>

		<dc:creator><![CDATA[Nick]]></dc:creator>
		<pubDate>Mon, 12 May 2014 18:45:48 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3215#comment-2223</guid>

					<description><![CDATA[This is my absolute favourite sequence generator. Employed here to find gaps in a primary key.
No idea about the plan though...
;with 
digits (i) as(
	select 1 as i 
	union all select 2 
	union all select 3 
	union all select 4 
	union all select 5 
	union all select 6 
	union all select 7 
	union all select 8 
	union all select 9 
	union all select 0
),
sequence (i) as (
	select D1.i 
	+ (10*D2.i)
	+ (100*D3.i)
	+ (1000*D4.i)
	+ (10000*D5.i) 
	+ (100000*D6.i) 
	from digits as D1,
	digits as D2,
	digits as D3,
	digits as D4,
	digits as D5,
	digits as D6 
)
select 
	(select top 1 admcdt from adm where admkey &#060; i order by admkey desc) as before, 
	i as &#039;missing adm&#039;, 
	(select top 1 admcdt from adm where admkey &#062; i order by admkey asc) as after
from sequence
where i between 1 and (select max(admkey) from adm)
and not exists (select 1 from adm where admkey=i)
order by i
All credit to -- &lt;a href=&quot;http://drsql.spaces.live.com/blog/cns&quot; rel=&quot;nofollow ugc&quot;&gt;http://drsql.spaces.live.com/blog/cns&lt;/a&gt;!80677FB08B3162E4!1232.entry]]></description>
			<content:encoded><![CDATA[<p>This is my absolute favourite sequence generator. Employed here to find gaps in a primary key.<br />
No idea about the plan though&#8230;<br />
;with<br />
digits (i) as(<br />
	select 1 as i<br />
	union all select 2<br />
	union all select 3<br />
	union all select 4<br />
	union all select 5<br />
	union all select 6<br />
	union all select 7<br />
	union all select 8<br />
	union all select 9<br />
	union all select 0<br />
),<br />
sequence (i) as (<br />
	select D1.i<br />
	+ (10*D2.i)<br />
	+ (100*D3.i)<br />
	+ (1000*D4.i)<br />
	+ (10000*D5.i)<br />
	+ (100000*D6.i)<br />
	from digits as D1,<br />
	digits as D2,<br />
	digits as D3,<br />
	digits as D4,<br />
	digits as D5,<br />
	digits as D6<br />
)<br />
select<br />
	(select top 1 admcdt from adm where admkey &lt; i order by admkey desc) as before,<br />
	i as &#8216;missing adm&#8217;,<br />
	(select top 1 admcdt from adm where admkey &gt; i order by admkey asc) as after<br />
from sequence<br />
where i between 1 and (select max(admkey) from adm)<br />
and not exists (select 1 from adm where admkey=i)<br />
order by i<br />
All credit to &#8212; <a href="http://drsql.spaces.live.com/blog/cns" rel="nofollow ugc">http://drsql.spaces.live.com/blog/cns</a>!80677FB08B3162E4!1232.entry</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2013/03/11/a-top-query/#comment-2222</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Tue, 12 Mar 2013 02:05:18 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3215#comment-2222</guid>

					<description><![CDATA[All the scripts here are from SQL 2012. I would suggest that 4199 should be applied by default on new servers, yes. By all means do some testing though.
&lt;a href=&quot;http://support.microsoft.com/kb/974006&quot; rel=&quot;nofollow ugc&quot;&gt;http://support.microsoft.com/kb/974006&lt;/a&gt; says:
&#034;Starting with Microsoft SQL Server 2000 Service Pack 3 (SP3), the SQL Server query processor team adopted a policy that any hotfix that could potentially affect the execution plan of a query must be controlled by a trace flag. Except for fixes to bugs that can cause incorrect results or corruption, these hotfixes are turned off by default, and a trace flag is required to enable the fix. This policy change helps avoid unexpected changes to the execution plan that may occur when a hotfix or a security update is installed.&#034;
So if something in the QO has been deemed worthy of a hotfix, you still need to turn on a traceflag to get the benefit of that fix. 4199 is how you turn these things on. I&#039;d be surprised if you found a situation where 4199 was detrimental.]]></description>
			<content:encoded><![CDATA[<p>All the scripts here are from SQL 2012. I would suggest that 4199 should be applied by default on new servers, yes. By all means do some testing though.<br />
<a href="http://support.microsoft.com/kb/974006" rel="nofollow ugc">http://support.microsoft.com/kb/974006</a> says:<br />
&quot;Starting with Microsoft SQL Server 2000 Service Pack 3 (SP3), the SQL Server query processor team adopted a policy that any hotfix that could potentially affect the execution plan of a query must be controlled by a trace flag. Except for fixes to bugs that can cause incorrect results or corruption, these hotfixes are turned off by default, and a trace flag is required to enable the fix. This policy change helps avoid unexpected changes to the execution plan that may occur when a hotfix or a security update is installed.&quot;<br />
So if something in the QO has been deemed worthy of a hotfix, you still need to turn on a traceflag to get the benefit of that fix. 4199 is how you turn these things on. I&#8217;d be surprised if you found a situation where 4199 was detrimental.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: tobi		</title>
		<link>https://lobsterpot.com.au/blog/2013/03/11/a-top-query/#comment-2221</link>

		<dc:creator><![CDATA[tobi]]></dc:creator>
		<pubDate>Tue, 12 Mar 2013 01:57:25 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.lobsterpot.com.au/?p=3215#comment-2221</guid>

					<description><![CDATA[So what&#039;s the recommendation regarding that trace flag? Apply it by default on new servers?
Is it meaningful to 2012? the article does not list anything regarding 2012.]]></description>
			<content:encoded><![CDATA[<p>So what&#8217;s the recommendation regarding that trace flag? Apply it by default on new servers?<br />
Is it meaningful to 2012? the article does not list anything regarding 2012.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
