<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2enclosuresfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Intermediate SQL</title>
	
	<link>http://intermediatesql.com</link>
	<description>Color Coded SQL, UNIX and Database Essays</description>
	<lastBuildDate>Sat, 16 Feb 2013 19:07:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/IntermediateSql" /><feedburner:info uri="intermediatesql" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><media:copyright>(c) Maxym Kharchenko</media:copyright><item>
		<title>RMOUG 2013 Training Days: SQL Top-N and Pagination pattern</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/rd8tEcZK_Ik/</link>
		<comments>http://intermediatesql.com/sql/rmoug-2013-training-days-sql-top-n-and-pagination-pattern/#comments</comments>
		<pubDate>Sat, 16 Feb 2013 19:07:38 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Conferences]]></category>
		<category><![CDATA[ORACLE]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=785</guid>
		<description><![CDATA[I just returned from RMOUG 2013 training days in Denver, CO and I have to say that I&#8217;m impressed! This was an incredibly well organized and well run event with lots of great speakers and great presentations. As a former board member of Tampa&#8217;s ORACLE users group, I know how hard it is to make [...]]]></description>
				<content:encoded><![CDATA[<p>I just returned from <a href="http://www.rmoug.org/training/training-days-2013/"><span class="caps">RMOUG</span> 2013 training days</a> in Denver, CO and I have to say that I&#8217;m impressed! </p>

<p>This was an incredibly well organized and well run event with lots of great speakers and great presentations. As a former board member of Tampa&#8217;s <span class="caps">ORACLE </span>users group, I know how hard it is to make an event like this happen, especially at this scale. Great job, @DbaKevlar and the rest! </p>

<p>Thanks to everyone who attended <a href="http://intermediatesql.com/wp-content/uploads/2013/02/Kharchenko_SQL-Pagination-Pattern-R.pptx">my presentation</a> at <span class="caps">RMOUG</span> 2013 and if you did not have a chance to catch it, I&#8217;ll be presenting it again at <a href="http://collaborate13.ioug.org/"><span class="caps">IOUG</span> Collaborate 2013</a>.</p>

<p>Cheers!</p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/rd8tEcZK_Ik" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/sql/rmoug-2013-training-days-sql-top-n-and-pagination-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<enclosure url="http://intermediatesql.com/wp-content/uploads/2013/02/Kharchenko_SQL-Pagination-Pattern-R.pptx" length="228210" type="application/vnd.openxmlformats-officedocument.presentationml.presentation" /><media:content url="http://intermediatesql.com/wp-content/uploads/2013/02/Kharchenko_SQL-Pagination-Pattern-R.pptx" fileSize="228210" type="application/vnd.openxmlformats-officedocument.presentationml.presentation" /><feedburner:origLink>http://intermediatesql.com/sql/rmoug-2013-training-days-sql-top-n-and-pagination-pattern/</feedburner:origLink></item>
		<item>
		<title>Read inconsistent queries in ORACLE database ? Sure, why not!</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/6A2rVG0dmrY/</link>
		<comments>http://intermediatesql.com/oracle/read-inconsistent-queries-in-oracle-database-sure-why-not/#comments</comments>
		<pubDate>Fri, 01 Jun 2012 17:04:26 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[ORACLE]]></category>
		<category><![CDATA[Consistency]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Weird]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=759</guid>
		<description><![CDATA[Read consistency is one of the coolest features of ORACLE database. In a nutshell, it means that your queries (at least, logically) do not pay attention to the outside world. I.e. your tables may be hit with thousands of changes per second, but your query will ignore all the hoopla and will always see only [...]]]></description>
				<content:encoded><![CDATA[<p><strong>Read consistency</strong> is one of the coolest features of <span class="caps">ORACLE </span>database. </p>

<p>In a nutshell, it means that <em>your queries</em> (at least, logically) <em>do not pay attention to the outside world</em>. I.e. your tables may be hit with thousands of changes per second, but your query will ignore all the hoopla and will always see only the data that existed when the query started.</p>

<p><span id="more-759"></span><br />
Read consistency is quite addictive, because in <span class="caps">ORACLE </span>it is so trivial. Once you figure out that you don&#8217;t need to do much to get it (i.e. deal with locks or synchronize with other users), running queries and expecting consistent results every time quickly becomes a second nature. </p>

<p>And &#8230; it&#8217;s quite easy to to fall into a trap of thinking that working any other way for a query is impossible.</p>

<p>My job today is to show that this expectation is not entirely correct <img src='http://intermediatesql.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<span id="What_does_it_mean_to_be_read_consistent"><h3>What does it mean to be read consistent</h3></span>

<p>So, what exactly does it mean for a query to be read consistent ? </p>

<p>I&#8217;m going to explore this point using a slightly convoluted (and specially crafted) example, that will query this simple table:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t <span style="color: #66cc66;">&#40;</span>order_id <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
commit;<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t;<br />
<br />
&nbsp; ORDER_ID<br />
<span style="color: #808080; font-style: italic;">----------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span></div></div>

<p>With this slightly complicated query:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WITH</span> int_g <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> level l<span style="color: #66cc66;">,</span> sleep<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> s <span style="color: #993333; font-weight: bold;">FROM</span> dual <span style="color: #993333; font-weight: bold;">CONNECT</span> <span style="color: #993333; font-weight: bold;">BY</span> level <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">5</span><br />
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #808080; font-style: italic;">/*+ ordered no_merge use_nl(t) */</span> int_g<span style="color: #66cc66;">.</span>l<span style="color: #66cc66;">,</span> int_g<span style="color: #66cc66;">.</span>s<span style="color: #66cc66;">,</span> t<span style="color: #66cc66;">.</span>order_id<br />
<span style="color: #993333; font-weight: bold;">FROM</span> int_g<span style="color: #66cc66;">,</span> t<br />
<span style="color: #66cc66;">/</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; ORDER_ID<br />
<span style="color: #808080; font-style: italic;">---------- ---------- ----------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">2</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">3</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">4</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">5</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span></div></div>

<p>Because I&#8217;m using nested loops the query above implements this fairly simple algorithm during its execution:</p>

<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; FOR I <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #ff4500;">1</span>..<span style="color: #ff4500;">5</span>:<br />
&nbsp; &nbsp; SELECT ...<br />
&nbsp; &nbsp; <span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></div></div>

<p>sleep(1) function here is a simple wrap around dbms_lock.sleep:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> sleep <span style="color: #66cc66;">&#40;</span>seconds <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">NUMBER</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">NUMBER</span> <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; sys<span style="color: #66cc66;">.</span>dbms_lock<span style="color: #66cc66;">.</span>sleep<span style="color: #66cc66;">&#40;</span>seconds<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <span style="color: #993333; font-weight: bold;">RETURN</span> seconds;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #66cc66;">/</span></div></div>

<p>that causes the query to sleep for 1 second. </p>

<p>In other words, I&#8217;m probing inner table: T 5 times with 1 second delay between each try.</p>

<p>To make matters interesting, once the above query starts, I&#8217;m going to also start changing data in T from a separate session with this simple script:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">..</span> <span style="color: #cc66cc;">10</span> loop<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t <span style="color: #66cc66;">&#40;</span>order_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; commit;<br />
&nbsp; &nbsp; dbms_lock<span style="color: #66cc66;">.</span>sleep<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <span style="color: #993333; font-weight: bold;">END</span> loop;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #66cc66;">/</span></div></div>

<p>The bottom line is: each probe in the query should deal with a slightly different data in the table and we can now see how <span class="caps">ORACLE </span>reacts to that. </p>

<p>As most people would expect, the result of the query, even with concurrent updates still looks like this:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;S &nbsp; ORDER_ID<br />
<span style="color: #808080; font-style: italic;">---------- ---------- ----------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">2</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">3</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">4</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">5</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span></div></div>

<p>In other words, the original query did <span class="caps">NOT </span>see any data changes coming from session 2. We queried table T 5 times over 5 seconds interval. Every time the data in T was slightly different, but our query always saw the same image, as it existed when query started. This is how read consistency rules the day.</p>

<span id="How_to_be_read_inconsistent"><h3>How to be read inconsistent</h3></span>

<p>Still, if this is  what you&#8217;ve come to expect from <span class="caps">ALL ORACLE </span>queries, prepare to be surprised. </p>

<p>Let&#8217;s slightly modify our query, replacing our regular table with one of <span class="caps">ORACLE </span>dictionary views &#8211; v$session. In a nutshell, we&#8217;ll query our session status as we move along in our query.</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WITH</span> int_g <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> level l<span style="color: #66cc66;">,</span> sleep<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> s <span style="color: #993333; font-weight: bold;">FROM</span> dual<br />
&nbsp; <span style="color: #993333; font-weight: bold;">CONNECT</span> <span style="color: #993333; font-weight: bold;">BY</span> level <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">5</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #808080; font-style: italic;">/*+ ordered no_merge use_nl(se) */</span><br />
&nbsp; se<span style="color: #66cc66;">.</span>event<span style="color: #66cc66;">,</span> se<span style="color: #66cc66;">.</span>wait_time_micro<span style="color: #66cc66;">,</span> int_g<span style="color: #66cc66;">.</span>s<br />
<span style="color: #993333; font-weight: bold;">FROM</span> int_g<span style="color: #66cc66;">,</span> v$session se<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> sid<span style="color: #66cc66;">=</span>sys_context<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'userenv'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'sid'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">/</span><br />
<br />
EVENT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WAIT_TIME_MICRO &nbsp; S<br />
<span style="color: #808080; font-style: italic;">-------------------- --------------- ---</span><br />
PL<span style="color: #66cc66;">/</span><span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #993333; font-weight: bold;">LOCK</span> timer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>000<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">760</span> &nbsp; <span style="color: #cc66cc;">1</span><br />
PL<span style="color: #66cc66;">/</span><span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #993333; font-weight: bold;">LOCK</span> timer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>000<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">610</span> &nbsp; <span style="color: #cc66cc;">1</span><br />
PL<span style="color: #66cc66;">/</span><span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #993333; font-weight: bold;">LOCK</span> timer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>000<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">765</span> &nbsp; <span style="color: #cc66cc;">1</span><br />
PL<span style="color: #66cc66;">/</span><span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #993333; font-weight: bold;">LOCK</span> timer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>000<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">755</span> &nbsp; <span style="color: #cc66cc;">1</span><br />
PL<span style="color: #66cc66;">/</span><span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #993333; font-weight: bold;">LOCK</span> timer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>000<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">688</span> &nbsp; <span style="color: #cc66cc;">1</span></div></div>

<p>Surprise! Look at the &#8220;wait times&#8221; &#8211; all of them are different. </p>

<p>We still query underlying table (v$session in this case) <a href='http://intermediatesql.com/wp-content/uploads/2012/06/sys_qry_plan_inconsistent.txt'>5 times in a nested loop</a>, but now <em>every single time we look at the table we see different results!</em> It&#8217;s almost like read consistency is not in the picture here. </p>

<p>Well, the truth is: </p>

<div id='stb-box-4522' class='stb-warning_box' style="background-image: url(Null); "></p>

<p><strong>When it comes to v$ views read consistency is <span class="caps">REALLY </span>not in the picture</strong></div>

<p>v$ views are mostly based on x$ objects, which, although look like tables to us and are even called (&#8220;fixed&#8221;) tables are something else entirely: x$ objects are kernel memory arrays that are exposed to us (for our convenience) through a table interface. And being arrays, they do <span class="caps">NOT </span>have read consistency!</p>

<p>To prove the point even further I&#8217;m going to slightly modify this example by changing order of tables in the <span class="caps">FROM </span>clause:</p>

<p>With <span class="caps">SQL </span>being declarative language, this shouldn&#8217;t affect the results, right ? Well, see for yourself:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WITH</span> int_g <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> level l<span style="color: #66cc66;">,</span> sleep<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> s <span style="color: #993333; font-weight: bold;">FROM</span> dual<br />
&nbsp; <span style="color: #993333; font-weight: bold;">CONNECT</span> <span style="color: #993333; font-weight: bold;">BY</span> level <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">5</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #808080; font-style: italic;">/*+ ordered no_merge use_nl(se) */</span><br />
&nbsp; se<span style="color: #66cc66;">.</span>event<span style="color: #66cc66;">,</span> se<span style="color: #66cc66;">.</span>wait_time_micro<span style="color: #66cc66;">,</span> int_g<span style="color: #66cc66;">.</span>s<br />
<span style="color: #993333; font-weight: bold;">FROM</span> v$session se<span style="color: #66cc66;">,</span> int_g <span style="color: #808080; font-style: italic;">-- &lt;-- ORDER IS REVERSED</span><br />
<span style="color: #993333; font-weight: bold;">WHERE</span> sid<span style="color: #66cc66;">=</span>sys_context<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'userenv'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'sid'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">/</span><br />
<br />
EVENT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WAIT_TIME_MICRO &nbsp; S<br />
<span style="color: #808080; font-style: italic;">------------------------- --------------- ---</span><br />
<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">*</span>Net message <span style="color: #993333; font-weight: bold;">TO</span> client &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">3</span> &nbsp; <span style="color: #cc66cc;">1</span><br />
<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">*</span>Net message <span style="color: #993333; font-weight: bold;">TO</span> client &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">3</span> &nbsp; <span style="color: #cc66cc;">1</span><br />
<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">*</span>Net message <span style="color: #993333; font-weight: bold;">TO</span> client &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">3</span> &nbsp; <span style="color: #cc66cc;">1</span><br />
<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">*</span>Net message <span style="color: #993333; font-weight: bold;">TO</span> client &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">3</span> &nbsp; <span style="color: #cc66cc;">1</span><br />
<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">*</span>Net message <span style="color: #993333; font-weight: bold;">TO</span> client &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">3</span> &nbsp; <span style="color: #cc66cc;">1</span></div></div>

<p>The picture is completely different! Since we <a href='http://intermediatesql.com/wp-content/uploads/2012/06/sys_qry_plan_consistent.sql_.txt'>start with v$session this time</a>, we read it only once and, of course, that means that wait times are the same everywhere (I guess, everything becomes consistent if you have one sample <img src='http://intermediatesql.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  )</p>

<p>And there you have it folks! Read consistency is <span class="caps">NOT </span>a natural property of any <span class="caps">ORACLE </span>query. It requires work to enforce and sometimes <span class="caps">ORACLE </span>chooses not to do it.</p>

<p>This is cool, but how we make it useful ? <br />
In the next article, I&#8217;ll talk about precisely that.</p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/6A2rVG0dmrY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/oracle/read-inconsistent-queries-in-oracle-database-sure-why-not/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://intermediatesql.com/oracle/read-inconsistent-queries-in-oracle-database-sure-why-not/</feedburner:origLink></item>
		<item>
		<title>Scrap the SCP. How to copy data fast using pigz and nc</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/M_5n7nUBLi0/</link>
		<comments>http://intermediatesql.com/linux/scrap-the-scp-how-to-copy-data-fast-using-pigz-and-nc/#comments</comments>
		<pubDate>Wed, 16 May 2012 17:02:33 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Remote copy]]></category>
		<category><![CDATA[Speed]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=728</guid>
		<description><![CDATA[Have you ever heard that the speed of the system is determined by its slowest component ? I am made painfully aware of that every time I do data migrations. I.e. it doesn&#8217;t matter if you have 64 core systems with 100+ Gb of memory on either end if the majority of time is spent [...]]]></description>
				<content:encoded><![CDATA[<p>Have you ever heard that the speed of the system is determined by its <em>slowest</em> component ? I am made painfully aware of that every time I do data migrations. </p>

<p>I.e. it doesn&#8217;t matter if you have 64 core systems with 100+ Gb of memory on either end if the majority of time is spent waiting for data to trickle across a slow 1 Gb network link. </p>

<p>Watching data trickle for hours, while the rest of the system is doing nothing is a pretty frustrating experience. But limitations breed creativity &#8230; so lately, I&#8217;ve been experimenting with several different copy techniques to see if there is any way transfer speed can be improved, perhaps using some of the idle capacity to speed things up. </p>

<p><span id="more-728"></span></p>

<p>Here is the short summary of my experiments (transferring 16Gb <span class="caps">ORACLE </span>data file across the <span class="caps">WAN</span>), which I summarized as a <em>&#8220;speed and effect comparison&#8221;</em> table. You can judge for yourself (with all the usual caveats that your results will depend on your system configuration and will probably vary yada yada yada) &#8230;</p>

<table class="MsoNormalTable" style="margin: auto auto auto -1.15pt; width: 433pt; border-collapse: collapse; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 0in 0in 0in;" width="577" border="0" cellspacing="0" cellpadding="0">
<tbody><br />
<tr style="height: 22.5pt; mso-yfti-irow: 0; mso-yfti-firstrow: yes;">
<td style="padding-bottom: 0in; padding-left: 5.4pt; width: 86pt; padding-right: 5.4pt; background: #9bbb59; height: 22.5pt; padding-top: 0in; border: windowtext 1pt solid;" valign="bottom" width="115">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><strong><span style="color: white;"><span style="font-size: small;"><span style="font-family: Calibri;">Method </span></span></span></strong></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 84pt; padding-right: 5.4pt; background: #9bbb59; height: 22.5pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" width="112">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><strong><span style="color: white;"><span style="font-size: small;"><span style="font-family: Calibri;">Transfer Time </span></span></span></strong></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; background: #9bbb59; height: 22.5pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><strong><span style="color: white;"><span style="font-size: small;"><span style="font-family: Calibri;">Network Capacity Used </span></span></span></strong></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 61pt; padding-right: 5.4pt; background: #9bbb59; height: 22.5pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" width="81">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><strong><span style="color: white;"><span style="font-size: small;"><span style="font-family: Calibri;"><span class="caps">CPU</span> Used </span></span></span></strong></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; background: #9bbb59; height: 22.5pt; border-top: windowtext 1pt solid; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><strong><span style="color: white;"><span style="font-size: small;"><span style="font-family: Calibri;">Effective Rate </span></span></span></strong></p>
</td>
</tr>
<tr style="height: 15pt; mso-yfti-irow: 1;">
<td style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 86pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="115">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">scp </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 84pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="112">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">4 min 50 seconds </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 55 Mb per second </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 61pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="81">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 5% </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 55 Mb per second </span></span></span></p>
</td>
</tr>
<tr style="height: 15pt; mso-yfti-irow: 2;">
<td style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 86pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="115">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">bbcp </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 84pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="112">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">2 min 27 seconds </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 108 Mb per second </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 61pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="81">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 2% </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 108 Mb per second </span></span></span></p>
</td>
</tr>
<tr style="height: 15pt; mso-yfti-irow: 3;">
<td style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 86pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="115">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">ncp / gzip </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 84pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="112">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">2 min 1 second </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 10 Mb per second </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 61pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="81">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 15% </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 132 Mb per second </span></span></span></p>
</td>
</tr>
<tr style="height: 15pt; mso-yfti-irow: 4;">
<td style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 86pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="115">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">ncp / pigz </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 84pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="112">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">30 seconds </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 20 Mb per second </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 61pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="81">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 50% </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; background-color: transparent; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 533 Mb per second </span></span></span></p>
</td>
</tr>
<tr style="height: 15pt; mso-yfti-irow: 5; mso-yfti-lastrow: yes;">
<td style="border-bottom: windowtext 1pt solid; border-left: windowtext 1pt solid; padding-bottom: 0in; padding-left: 5.4pt; width: 86pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="115">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">ncp / pigz (parallel degree limited) </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 84pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="112">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">1 min 15 second </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 15 Mb per second </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 61pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="81">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 20% </span></span></span></p>
</td>
<td style="border-bottom: windowtext 1pt solid; border-left: #f0f0f0; padding-bottom: 0in; padding-left: 5.4pt; width: 101pt; padding-right: 5.4pt; background: #eaf1dd; height: 15pt; border-top: #f0f0f0; border-right: windowtext 1pt solid; padding-top: 0in;" valign="bottom" nowrap="nowrap" width="135">
<p class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black;"><span style="font-size: small;"><span style="font-family: Calibri;">~ 214 Mb per second </span></span></span></p>
</td>
</tr>
</tbody><br />
</table>


<p>And here is the <strong>longer explanation</strong> if you are really interested <img src='http://intermediatesql.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<span id="Copying_data_using_SCP"><h3>Copying data using <span class="caps">SCP</span></h3></span>

<p>Traditionally, many people are using <strong>scp</strong> to copy files between systems, something like:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">scp</span> <span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb<span style="color: #000000; font-weight: bold;">/</span>data_file-<span style="color: #000000;">1</span>.dbf remote_host:<span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb</div></div>

<p>The problem is &#8211; scp is <span class="caps">NOT </span>very fast. I.e. regular speeds, achieved by scp are in the range of ~ 20-50 Mbs per second. To put it in perspective, it takes from ~ 4 to 13 minutes to copy a 16 Gb file between systems. Multiple it by, say, 8 files and now you are wasting 0.5 to 1.5 hours for the simple copy.</p>

<p>Which begs the question &#8211;  how can we do better ?</p>

<span id="Why_SCP_is_slow_by_default"><h3>Why <span class="caps">SCP </span>is slow by default</h3></span>

<p>The first observation with scp is that even at the top of the range the transfer speeds are <span class="caps">NOT </span>approaching the true <span class="caps">NIC </span>capacity (which, for 1 Gb <span class="caps">NIC </span>is slightly more than 100 Mbs per second).</p>

<p><a href="http://intermediatesql.com/wp-content/uploads/2012/05/XFER-scp.jpg"><img src="http://intermediatesql.com/wp-content/uploads/2012/05/XFER-scp.jpg" alt="" title="XFER-scp" width="982" height="179" class="alignnone size-full wp-image-736" /></a></p>

<p>So, we should do much better if we are able to <em>&#8220;fill the pipe&#8221;</em> completely.</p>

<span id="Filling_the_pipe:_Remote_copy_with_BBCP"><h3>Filling the pipe: Remote copy with <span class="caps">BBCP</span></h3></span>

<p>&#8220;Filling the pipe&#8221; is precisely what <a href="http://www.slac.stanford.edu/~abh/bbcp/">bbcp</a> command does &#8211; it opens multiple network streams and transfers a file in parallel, using most of the network capacity in the process. In my tests, bbcp consistently outperformed scp, reaching speeds of ~ 100-115 Mbs per second and cutting transfer time by the factor of 2.</p>

<p><a href="http://intermediatesql.com/wp-content/uploads/2012/05/XFER-bbcp.jpg"><img src="http://intermediatesql.com/wp-content/uploads/2012/05/XFER-bbcp.jpg" alt="" title="XFER-bbcp" width="983" height="180" class="alignnone size-full wp-image-737" /></a></p>

<p>There are, however, two problems with bbcp.</p>

<p>First of all, its default syntax is pretty scary. I.e. in my example, it looked like this:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&gt;</span> bbcp <span style="color: #660033;">-P</span> <span style="color: #000000;">10</span> <span style="color: #660033;">-f</span> <span style="color: #660033;">-T</span> <span style="color: #ff0000;">'ssh -x -a %I -l %U %H bbcp'</span> \<br />
<span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb<span style="color: #000000; font-weight: bold;">/</span>data_file-<span style="color: #000000;">1</span>.dbf remote_host:<span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb<span style="color: #000000; font-weight: bold;">/</span>data_file-<span style="color: #000000;">1</span>.dbf</div></div>

<p>But more importantly, using that much network for copy is dangerous as it does not leave much bandwidth for anything else on the host (i.e. regular <span class="caps">ORACLE </span>connections by apps). </p>

<p>Plus, it may affect other <em>unrelated</em> hosts if you happen to have multiple machines using the same network path and a slightly oversubscribed network.</p>

<p>In other words, bbcp should be used only if you do not care whether database on the box is accessible and also do not share the host/rack/routers with anybody else.</p>

<p>To be fair, you can use bbcp options to limit how much bandwidth it is using. But if you do that, the copy speed essentially reverts back to scp as it directly correlates to how much data you are pushing over the wire.</p>

<p>Bottom line, bbcp = not good, if your system is <em>actually used</em> !</p>

<p>Is there another alternative ? </p>

<span id="The_magic_bullet:_Compression"><h3>The magic bullet: Compression</h3></span>

<p>Yes. Apparently, <strong><span class="caps">ORACLE </span>data files are pretty compressible</strong>. We can gzip them on the source, transfer 5-10x times less data over the wire (yes, that seems to be the average compression rates, sometimes rates are even better) and unpack them on destination.</p>

<p>The problem, however is that instead of running a simple scp command, we need to run 3 commands on 2 separate systems: </p>

<ul>
	<li>Source: gzip</li>
	<li>Source/Target: transfer, i.e. scp</li>
	<li>Target: unzip</li>
</ul>

<p>which is a bit too complex if we just want to copy a bunch of files.</p>

<span id="Network_streaming"><h3>Network streaming</h3></span>

<p>Fortunately, this technique can be simplified and generalized by using <em>network streaming tools</em>.</p>

<p>Here is an example of copying the same file using gzip and <a href="http://www.catonmat.net/blog/unix-utilities-netcat/">netcat</a>. We still need to run 2 commands, but they are pretty simple:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># SOURCE: </span><br />
<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cf</span> - <span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb<span style="color: #000000; font-weight: bold;">/</span>data_file-<span style="color: #000000;">1</span>.dbf <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">gzip</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">|</span> nc <span style="color: #660033;">-l</span> <span style="color: #000000;">8888</span><br />
<br />
<span style="color: #666666; font-style: italic;"># TARGET:</span><br />
<span style="color: #000000; font-weight: bold;">&gt;</span> nc <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #7a0874; font-weight: bold;">source</span> host<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">8888</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">gzip</span> <span style="color: #660033;">-d</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> xf - <span style="color: #660033;">-C</span> <span style="color: #000000; font-weight: bold;">/</span></div></div>

<p><strong>nc</strong> here is a network streamer that sends data over to the wire on the sending end (port: 8888) and reads the data from the wire on the receiving. </p>

<p>I ran many such copies and every single time <strong>md5sum</strong> confirmed that data files were transferred correctly. Moreover, when something breaks (such as when a certain <span class="caps">DBA </span>would run Ctrl+C on either end), this event is very visible &#8211; you will recognize that an error has occurred and you need to re-transfer.</p>

<p>In most of my tests this combination of commands was even faster than bbcp (giving me an additional 15-25 % improvement), but, more importantly, it utilized only ~ 1/4 of the available network bandwidth, usually putting ~ 20-30 Mbps over the wire (even scp puts 60).</p>

<p><a href="http://intermediatesql.com/wp-content/uploads/2012/05/XFER-ncp.jpg"><img src="http://intermediatesql.com/wp-content/uploads/2012/05/XFER-ncp.jpg" alt="" title="XFER-ncp" width="983" height="179" class="alignnone size-full wp-image-734" /></a></p>

<span id="And_finally_parallelism"><h3>And, finally, parallelism</h3></span>

<p>But we are still not done. Transfer speeds can be improved further if we are willing to use a bit of <span class="caps">CPU </span>on the source host. </p>

<p>As you might know, gzip is a <em>sequential single threaded</em> application, but we also have a parallel zip, named rather expressively as <strong>&#8220;pigz&#8221;</strong>:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># SOURCE: </span><br />
<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cf</span> - <span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb<span style="color: #000000; font-weight: bold;">/</span>data_file-<span style="color: #000000;">1</span>.dbf <span style="color: #000000; font-weight: bold;">|</span> pigz <span style="color: #000000; font-weight: bold;">|</span> nc <span style="color: #660033;">-l</span> <span style="color: #000000;">8888</span><br />
<br />
<span style="color: #666666; font-style: italic;"># TARGET:</span><br />
<span style="color: #000000; font-weight: bold;">&gt;</span> nc <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #7a0874; font-weight: bold;">source</span> host<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">8888</span> <span style="color: #000000; font-weight: bold;">|</span> pigz <span style="color: #660033;">-d</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> xf - <span style="color: #660033;">-C</span> <span style="color: #000000; font-weight: bold;">/</span></div></div>

<p><a href="http://zlib.net/pigz/">Pigz</a> is a essentially a gzip, but can use multiple parallel streams to compress/decompress the data. If we replace gzip with pigz, we can achieve fantastical speeds and cut our transfer time again by the factor of ~ 2-10, comparing to scp.</p>

<span id="A_few_notes_and_observations"><h3>A few notes and observations</h3></span>

<span id="Bbcp_compression"><h4>Bbcp compression</h4></span>

<p>If you <em>can</em> fill network pipe completely (i.e. you are the only user in a system), the question naturally becomes: <strong>&#8220;Can we combine compression and multistream transfer for even faster speeds ?&#8221;</strong>.</p>

<p>As it happens, bbcp command has <em>&#8220;compress me&#8221;</em> option for input streams, so it seems a natural candidate here &#8230; However as hard as I tried I couldn&#8217;t make it work properly. In all of my tests, when bbcp compression was turned on, there was definite improvement in network utilization, but the transfer itself was dead slow &#8230; much slower than that of the original scp. If anybody knows how to use bbcp compression efficiently, I&#8217;ll appreciate the learning experience.</p>

<p>Still, the rather straightforward workaround is to still use tar/pigz/nc and <em>just run several copies in parallel</em>.</p>

<span id="Monitoring_transfer_progress_with_nc"><h4>Monitoring transfer progress with nc</h4></span>

<p>Pigz/nc transfer might be significantly faster, but, it might not be the easiest to monitor. While scp has a nice progress bar, pigz/nc just gives you a blank screen for the entire duration of the transfer. Fortunately, it is very easy to correct if you drop in a <a href="http://www.catonmat.net/blog/unix-utilities-pipe-viewer/">pipe viewer tool</a> within pigz/nc pipe.</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cf</span> - <span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb<span style="color: #000000; font-weight: bold;">/</span>data_file-<span style="color: #000000;">1</span>.dbf <span style="color: #000000; font-weight: bold;">|</span> \<br />
&nbsp; pv <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #660033;">-sb</span> <span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb<span style="color: #000000; font-weight: bold;">/</span>data_file-<span style="color: #000000;">1</span>.dbf <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{s += $1} END {print s}'</span><span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">|</span> \<br />
&nbsp; pigz &nbsp;<span style="color: #000000; font-weight: bold;">|</span> nc <span style="color: #660033;">-l</span> <span style="color: #000000;">8888</span></div></div>

<p>which should give you a nice progress bar, quite similar to scp:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">1.12GB <span style="color: #000000;">0</span>:00:<span style="color: #000000;">15</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>86.5MB<span style="color: #000000; font-weight: bold;">/</span>s<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>===<span style="color: #000000; font-weight: bold;">&gt;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #7a0874; font-weight: bold;">&#93;</span> &nbsp;<span style="color: #000000;">7</span><span style="color: #000000; font-weight: bold;">%</span> ETA <span style="color: #000000;">0</span>:03:<span style="color: #000000;">18</span></div></div>

<span id="Using_one_command_for_copy_instead_of_two"><h4>Using one command for copy, instead of two</h4></span>

<p>While source/transfer commands are not too complex to master, there are still two commands that you need to run. To make things easier, it makes sense to <a href="http://intermediatesql.com/wp-content/uploads/2012/05/ncp.txt">script them together</a> to remove another advantage of scp:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&gt;</span> ncp<span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000; font-weight: bold;">/</span>u02<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>mydb<span style="color: #000000; font-weight: bold;">/</span>data_file-<span style="color: #000000;">1</span>.dbf remote_host</div></div>

<span id="tarpigznc_transfer_is_not_secure"><h4>tar/pigz/nc transfer is not secure</h4></span>

<p>Finally, there is one advantage that scp still holds: its transfer is <em>secure</em> while pigz/nc transfers data in clear text. So, if you are using unsecured networks, this option is probably not for you. </p>

<p>Cheers,<br />
Maxym Kharchenko</p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/M_5n7nUBLi0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/linux/scrap-the-scp-how-to-copy-data-fast-using-pigz-and-nc/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<feedburner:origLink>http://intermediatesql.com/linux/scrap-the-scp-how-to-copy-data-fast-using-pigz-and-nc/</feedburner:origLink></item>
		<item>
		<title>IOUG Collaborate 2012: The art of database sharding</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/9GzzEYNVm8s/</link>
		<comments>http://intermediatesql.com/sharding/ioug-collaborate-2012-the-art-of-database-sharding/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 05:31:03 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[Sharding]]></category>
		<category><![CDATA[Distributed Computing]]></category>
		<category><![CDATA[ORACLE]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=712</guid>
		<description><![CDATA[Thanks everyone, who attended my presentation about database sharding at IOUG Collaborate. That was a lot of fun! Sharding is an awesome technique and I&#8217;ll blog separately on it, but for now see my presentation and white paper. Cheers]]></description>
				<content:encoded><![CDATA[<p>Thanks everyone, who attended my presentation about database sharding at <a href="http://events.ioug.org/p/cm/ld/fid=28"><span class="caps">IOUG</span> Collaborate</a>. That was a lot of fun!</p>

<p>Sharding is an awesome technique and I&#8217;ll blog separately on it, but for now see my <a href="http://intermediatesql.com/wp-content/uploads/2012/04/2012_369_Kharchenko_ppr_0423.pptx">presentation</a> and <a href='http://intermediatesql.com/wp-content/uploads/2012/04/2012_369_Kharchenko_ppr.doc'>white paper</a>.</p>

<p>Cheers</p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/9GzzEYNVm8s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/sharding/ioug-collaborate-2012-the-art-of-database-sharding/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<enclosure url="http://intermediatesql.com/wp-content/uploads/2012/04/2012_369_Kharchenko_ppr_0423.pptx" length="662698" type="application/vnd.openxmlformats-officedocument.presentationml.presentation" /><media:content url="http://intermediatesql.com/wp-content/uploads/2012/04/2012_369_Kharchenko_ppr_0423.pptx" fileSize="662698" type="application/vnd.openxmlformats-officedocument.presentationml.presentation" /><feedburner:origLink>http://intermediatesql.com/sharding/ioug-collaborate-2012-the-art-of-database-sharding/</feedburner:origLink></item>
		<item>
		<title>How to find if your SQL is using SPM baseline</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/qxzYgT5mnj0/</link>
		<comments>http://intermediatesql.com/oracle/how-to-find-if-your-sql-is-using-spm-baseline/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 23:55:24 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[ORACLE]]></category>
		<category><![CDATA[11g]]></category>
		<category><![CDATA[SPM]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Stability]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=673</guid>
		<description><![CDATA[I&#8217;ve always wondered how many of my SQLs are NOT using baselines. Of course, when you run DBMS_XPLAN.DISPLAY_CURSOR for the statement, it can put a nicely formatted note for you, something like: Note ----- &#160; &#160;- SQL plan baseline &#34;SQL_PLAN_01yu884fpund494ecae5c&#34; used FOR this statement But can you find it globally for ALL SQLs in the [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve always wondered how many of my <span class="caps">SQL</span>s are <span class="caps">NOT </span>using baselines.</p>

<p>Of course, when you run <strong><span class="caps">DBMS</span>_XPLAN.DISPLAY_CURSOR</strong> for the statement, it can put a nicely formatted note for you, something like:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Note <br />
<span style="color: #808080; font-style: italic;">----- </span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">-</span> <span style="color: #993333; font-weight: bold;">SQL</span> plan baseline <span style="color: #ff0000;">&quot;SQL_PLAN_01yu884fpund494ecae5c&quot;</span> used <span style="color: #993333; font-weight: bold;">FOR</span> this statement</div></div>

<p>But can you find it globally for <span class="caps">ALL SQL</span>s in the shared pool ?</p>

<p><span id="more-673"></span></p>

<p>It turns out that the answer was staring right in my face: <strong>v$sql</strong> has a <strong><span class="caps">SQL</span>_PLAN_BASELINE</strong> column, which along with <strong><span class="caps">EXACT</span>_MATCHING_SIGNATURE</strong> is <em>a key</em> into <strong>dba_sql_plan_baselines</strong> view.</p>

<p>If <strong>v$sql.SQL_PLAN_BASELINE</strong> is empty, then the baseline is <span class="caps">NOT </span>used, otherwise the column contains baseline plan name being used at the moment.</p>

<p>Anyway, here is a useful script to find out if your particular cursor is using plan(s) from baseline or not:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> b<span style="color: #66cc66;">.</span>sql_handle<span style="color: #66cc66;">,</span> b<span style="color: #66cc66;">.</span>plan_name<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>child_number<span style="color: #66cc66;">,</span> <br />
&nbsp; s<span style="color: #66cc66;">.</span>plan_hash_value<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>executions<br />
<span style="color: #993333; font-weight: bold;">FROM</span> v$sql s<span style="color: #66cc66;">,</span> dba_sql_plan_baselines b<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> s<span style="color: #66cc66;">.</span>exact_matching_signature <span style="color: #66cc66;">=</span> b<span style="color: #66cc66;">.</span>signature<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> s<span style="color: #66cc66;">.</span>sql_plan_baseline <span style="color: #66cc66;">=</span> b<span style="color: #66cc66;">.</span>plan_name<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> s<span style="color: #66cc66;">.</span>sql_id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'&amp;SQL_ID'</span><br />
<span style="color: #66cc66;">/</span></div></div>

<p>Cheers!</p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/qxzYgT5mnj0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/oracle/how-to-find-if-your-sql-is-using-spm-baseline/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://intermediatesql.com/oracle/how-to-find-if-your-sql-is-using-spm-baseline/</feedburner:origLink></item>
		<item>
		<title>How to add a hint to ORACLE query without touching its text</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/A-eVSRwYcXw/</link>
		<comments>http://intermediatesql.com/oracle/how-to-add-a-hint-to-oracle-query-without-touching-its-text/#comments</comments>
		<pubDate>Sat, 18 Jun 2011 16:24:25 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[ORACLE]]></category>
		<category><![CDATA[Optimizer]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Profile]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=674</guid>
		<description><![CDATA[If you&#8217;ve been a DBA long enough, you&#8217;ve probably seen multiple cases where certain SQL queries just refuse to cooperate. I.e. you KNOW that the query is supposed to use index IDX1, but ORACLE stubbornly decides to take IDX2. Rats! The usual suspect here is bad statistics and thus the usual advise is: start re-collecting [...]]]></description>
				<content:encoded><![CDATA[<p>If you&#8217;ve been a <span class="caps">DBA </span>long enough, you&#8217;ve probably seen multiple cases where certain <span class="caps">SQL </span>queries just refuse to cooperate.</p>

<p>I.e. you <span class="caps">KNOW </span>that the query is supposed to use index <strong><span class="caps">IDX1</span></strong>, but <span class="caps">ORACLE </span>stubbornly decides to take <strong><span class="caps">IDX2</span></strong>. Rats!</p>

<p>The usual suspect here is bad statistics and thus the usual advise is: start re-collecting them. But while this might help, the success is far from guaranteed! Moreover, with a <em>&#8216;real production&#8217;</em> data statistics collection could take hours (days?) and you already have people screaming about slow performance &#8230; In other words, you need to fix the problem <span class="caps">NOW </span>and waiting until stats collection <em>maybe</em> fixes the problem a few hours down the road is just not an option!</p>

<p><span id="more-674"></span></p>

<p>Well, if the problem is that a bad index is chosen &#8211; the fastest way to fix it is to hint the query to chose a good one.</p>

<p>However, there might be some problems with this approach too. Adding <span class="caps">SQL </span>hint is still a <em>&#8220;source code change&#8221;</em>. And in many cases, the source code is either not accessible or access is so complicated that hinting your query can take weeks &#8230; Yikes!</p>

<p>Fortunately, there is a a way to attach <span class="caps">SQL </span>hints directly in the database itself without all that hassle &#8230; It&#8217;s not fully supported, but it works &#8230; and it can save your butt if you need to &#8220;fix&#8221; things quickly &#8230;</p>

<p>Here is what you need to do:</p>

<span id="Step_1:_Find_your_query_block_name"><h1>Step 1: Find your query block name</h1></span>

<p>Reason being, <span class="caps">ORACLE </span>needs to know where in the query to apply the hint.</p>

<p>Assuming, you know your query <span class="caps">SQL</span>_ID:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">define SQL_ID<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'your sql id'</span><br />
define CHILD_NO<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> verify off<br />
<br />
colu operation format A20<br />
colu options format A25<br />
colu object_name format A20<br />
colu object_alias format A20<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> operation<span style="color: #66cc66;">,</span>options<span style="color: #66cc66;">,</span>object_name<span style="color: #66cc66;">,</span>object_alias<br />
<span style="color: #993333; font-weight: bold;">FROM</span> v$sql_plan<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> sql_id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'&amp;SQL_ID'</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> child_number<span style="color: #66cc66;">=</span>&amp;CHILD_NO<br />
<span style="color: #66cc66;">/</span></div></div>

<p>You should get something like:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">OPERATION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OPTIONS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OBJECT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OBJECT_ALIAS<br />
<span style="color: #808080; font-style: italic;">-------------------- ------------------------- -------------------- --------------------</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> STATEMENT<br />
<span style="color: #993333; font-weight: bold;">TABLE</span> ACCESS &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">INDEX</span> ROWID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;T &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;T@SEL$1<br />
<span style="color: #993333; font-weight: bold;">INDEX</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RANGE SCAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;T_D_IDX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;T@SEL$1</div></div>

<p>where @SEL1 is your query block name.</p>

<span id="Step_2:_8220Construct8221_extended_hint"><h1>Step 2: &#8220;Construct&#8221; extended hint</h1></span>

<p>Hints can be attached to your query in the form of a <span class="caps">SQL </span>profile.</p>

<p>However, <span class="caps">ORACLE </span>is very peculiar about profile hint syntax and, generally, requires extended form of hints that list table and query block names.</p>

<p>Simple hints (that you are probably used to) will not do!</p>

<p>I.e. to be recognized, this common hint:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">INDEX</span><span style="color: #66cc66;">&#40;</span>t t_d_idx<span style="color: #66cc66;">&#41;</span></div></div>

<p>will need to be transformed into its <em>&#8220;extended&#8221;</em> form:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">INDEX</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #ff0000;">&quot;SEL$1&quot;</span> T@<span style="color: #ff0000;">&quot;SEL$1&quot;</span> <span style="color: #ff0000;">&quot;T_D_IDX&quot;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>If you are unsure what exactly <span class="caps">ORACLE </span>expects in &#8220;extended&#8221; hints, you can use <a href="http://intermediatesql.com/wp-content/uploads/2011/06/s_hints.txt">this script</a> to see extended hints for any <span class="caps">SQL </span>that exists in the shared pool.</p>

<span id="Step_3:_Create_SQL_profile"><h1>Step 3: Create <span class="caps">SQL </span>profile</h1></span>

<p>This is the final step and it&#8217;s the one that is <span class="caps">NOT </span>&#8220;officially&#8221; supported.</p>

<p>Even though <span class="caps">SQL </span>profiles are basically just a bunch of hints that attach to your statement, <span class="caps">ORACLE </span>originally designed them as a way to &#8220;fix&#8221; bad cardinality estimations for &#8220;special&#8221; data.</p>

<p>Thus, the only &#8220;supported&#8221; way to create profiles is to have <span class="caps">ORACLE </span>run <span class="caps">SQL </span>tuning job on your query, i.e.:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dbms_sqltune<span style="color: #66cc66;">.</span>create_tuning_task<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span>;<br />
dbms_sqltune<span style="color: #66cc66;">.</span>execute_tuning_task<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span>;<br />
etc <span style="color: #66cc66;">...</span></div></div>

<p>which may create profile at the end if it helps the query (no guarantees, though!)</p>

<p>Plus, the only hints that profiles can officially have are: <strong><span class="caps">OPT</span>_ESTIMATE</strong> (&#8220;extended&#8221; form of good old <span class="caps">CARDINALITY </span>hints).</p>

<p>But if you look closely at <span class="caps">DBMS</span>_SQLTUNE package, you will find undocumented <span class="caps">IMPORT</span>_SQL_PROFILE procedure that allows &#8220;free form&#8221; SQL Profiles to be created.</p>

<p>Like this:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span><br />
clsql_text <span style="color: #993333; font-weight: bold;">CLOB</span>;<br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> sql_fulltext <span style="color: #993333; font-weight: bold;">INTO</span> clsql_text <span style="color: #993333; font-weight: bold;">FROM</span> v$sqlarea <span style="color: #993333; font-weight: bold;">WHERE</span> sql_id <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&amp;SQL_ID'</span>;<br />
<br />
DBMS_SQLTUNE<span style="color: #66cc66;">.</span>IMPORT_SQL_PROFILE<span style="color: #66cc66;">&#40;</span><br />
&nbsp; sql_text <span style="color: #66cc66;">=&gt;</span> clsql_text<span style="color: #66cc66;">,</span><br />
&nbsp; profile <span style="color: #66cc66;">=&gt;</span> sqlprof_attr<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'INDEX(@&quot;SEL$1&quot; T@&quot;SEL$1&quot; &quot;T_D_IDX&quot;)'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; name <span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">'PROFILE_&amp;SQL_ID'</span><span style="color: #66cc66;">,</span><br />
&nbsp; force_match <span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">TRUE</span><br />
<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #66cc66;">/</span></div></div>

<p><strong>force_match=TRUE</strong> means that <span class="caps">SQL </span>profile will be used for all similar statements, regardless of the value of literals (if <span class="caps">SQL </span>has any).</p>

<span id="Step_4:_Verify_that_the_plan_has_changed"><h1>Step 4: Verify that the plan has changed</h1></span>

<p>At this point the problem should be resolved. If you look at the new execution plan, you should see message like:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Note<br />
<span style="color: #808080; font-style: italic;">-----</span><br />
<span style="color: #66cc66;">-</span> <span style="color: #993333; font-weight: bold;">SQL</span> profile <span style="color: #ff0000;">&quot;PROFILE_0vt5t1thw4rzx&quot;</span> used <span style="color: #993333; font-weight: bold;">FOR</span> this statement</div></div>

<p>and execution plan should change.</p>

<p>If it hasn&#8217;t, either you made a typo in the hint definition or there is something else that prevents change in plans, i.e. current <span class="caps">SPM </span>baseline.</p>

<p>Correcting typos is easy: just drop existing profile and repeat step 3 with the new hint.</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">EXEC</span> dbms_sqltune<span style="color: #66cc66;">.</span>drop_sql_profile<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'PROFILE_&amp;SQL_ID'</span><span style="color: #66cc66;">&#41;</span>;</div></div>

<p>Good luck!</p>

<p><span class="caps">P.S.</span> You can also follow this <a href="http://intermediatesql.com/wp-content/uploads/2011/06/add_hint_example.txt">complete, albeit artificial example</a>.</p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/A-eVSRwYcXw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/oracle/how-to-add-a-hint-to-oracle-query-without-touching-its-text/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://intermediatesql.com/oracle/how-to-add-a-hint-to-oracle-query-without-touching-its-text/</feedburner:origLink></item>
		<item>
		<title>Smile: I don’t always drink beer …</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/bnIcCPrzEgc/</link>
		<comments>http://intermediatesql.com/uncategorized/smile-i-dont-always-drink-beer/#comments</comments>
		<pubDate>Mon, 09 May 2011 17:55:30 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[joke]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=655</guid>
		<description><![CDATA[Remember the ever annoying (but hilarious) Dos Equis guy commercials about &#8220;the most interesting man in the world&#8230;&#8221; ? Well, I&#8217;ve heard a good one today: Thanks, Arup. Stay thirsty my friends]]></description>
				<content:encoded><![CDATA[<p>Remember the ever annoying (but hilarious) <em>Dos Equis guy</em> commercials about <a href="http://www.youtube.com/watch?v=L-4zfsy6rsM&amp;feature=related">&#8220;the most interesting man in the world&#8230;&#8221;</a> ?</p>

<p>Well, I&#8217;ve heard a good one today:</p>

<div id='stb-box-5283' class='stb-info_box' style="background-image: url(none); min-height: 20px; padding-left: 5px; "><br />
   &#8230; I don&#8217;t always test my software &#8230; but when I do, I test it in <span class="caps">PROD </span>!<br/><br />
</div>

<p>Thanks, Arup. Stay thirsty my friends <img src='http://intermediatesql.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/bnIcCPrzEgc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/uncategorized/smile-i-dont-always-drink-beer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://intermediatesql.com/uncategorized/smile-i-dont-always-drink-beer/</feedburner:origLink></item>
		<item>
		<title>How to find SPM baseline by sql_id</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/t_6H6_J7z3g/</link>
		<comments>http://intermediatesql.com/oracle/how-to-find-spm-baseline-by-sql_id/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 23:27:10 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[ORACLE]]></category>
		<category><![CDATA[11g]]></category>
		<category><![CDATA[Optimizer]]></category>
		<category><![CDATA[SPM]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=639</guid>
		<description><![CDATA[When you start working with SQL Plan baselines, one of the annoying things that you might find is that the main &#8220;baseline&#8221; dictionary view dba_sql_plan_baselines does not have sql_id column. SQL&#62; @DESC dba_sql_plan_baselines &#160;Name &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;NULL? &#160; &#160;TYPE [...]]]></description>
				<content:encoded><![CDATA[<p>When you start working with <span class="caps">SQL</span> Plan baselines, one of the annoying things that you might find is that the main &#8220;baseline&#8221; dictionary view <strong>dba_sql_plan_baselines</strong> does not have <strong>sql_id</strong> column.</p>

<p><span id="more-639"></span></p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SQL<span style="color: #66cc66;">&gt;</span> @<span style="color: #993333; font-weight: bold;">DESC</span> dba_sql_plan_baselines<br />
&nbsp;Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span>? &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">TYPE</span><br />
&nbsp;<span style="color: #808080; font-style: italic;">----------------------------------------- -------- ----------------------------</span><br />
&nbsp;SIGNATURE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">NUMBER</span><br />
&nbsp;SQL_HANDLE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp;SQL_TEXT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">CLOB</span><br />
&nbsp;PLAN_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp;CREATOR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp;ORIGIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp;<span style="color: #66cc66;">...</span></div></div>

<p>There are all kinds of identifiers: <span class="caps">SQL</span>_HANDLE, <span class="caps">PLAN</span>_NAME, <span class="caps">SIGNATURE, </span>but if you look at them, nothing resembles <span class="caps">SQL</span>_ID.</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SQL<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> signature<span style="color: #66cc66;">,</span> sql_handle<span style="color: #66cc66;">,</span> plan_name <br />
&nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> dba_sql_plan_baselines <span style="color: #993333; font-weight: bold;">WHERE</span> rownum <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">3</span>;<br />
<br />
&nbsp;SIGNATURE SQL_HANDLE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PLAN_NAME<br />
<span style="color: #808080; font-style: italic;">---------- ------------------------------ ------------------------------</span><br />
9<span style="color: #66cc66;">.</span>1707E<span style="color: #66cc66;">+</span>15 SYS_SQL_002094bafbab9fd3 &nbsp; &nbsp; &nbsp; SYS_SQL_PLAN_fbab9fd35f783afa<br />
9<span style="color: #66cc66;">.</span>1707E<span style="color: #66cc66;">+</span>15 SYS_SQL_002094bafbab9fd3 &nbsp; &nbsp; &nbsp; SYS_SQL_PLAN_fbab9fd3bcdf6d3e<br />
1<span style="color: #66cc66;">.</span>2199E<span style="color: #66cc66;">+</span>16 SYS_SQL_002b56dca9b266b3 &nbsp; &nbsp; &nbsp; SYS_SQL_PLAN_a9b266b3a3910e56</div></div>

<p>And yet, almost everywhere <span class="caps">ORACLE </span>seems to be making <span class="caps">SQL</span>_ID as the primary <span class="caps">SQL </span>identifier. </p>

<p>Of course, you can always use <span class="caps">SQL</span>_TEXT to join to v$sql, but this seems to be a bit drastic. </p>

<p>Fortunately, there is a better way. </p>

<p>Starting from <span class="caps">ORACLE</span> 10g v$sql has 2 additional columns: <strong>exact_matching_signature</strong> and <strong>force_matching_signature</strong> and it turns out they exactly match <strong>dba_sql_plan_baseline.signature</strong>, so finding out baselines that &#8220;attach&#8221; to your particular statement becomes quite easy:</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> sql_handle<span style="color: #66cc66;">,</span> plan_name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> dba_sql_plan_baselines<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> signature <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> exact_matching_signature <span style="color: #993333; font-weight: bold;">FROM</span> v$sql <span style="color: #993333; font-weight: bold;">WHERE</span> sql_id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'&amp;SQL_ID'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">/</span></div></div>

<p>Why did <span class="caps">ORACLE </span>create a separate <span class="caps">SQL </span>identifier here ? </p>

<p>My guess is &#8211; <span class="caps">SQL</span>_IDs and <span class="caps">SPM </span>baselines are not exactly the same thing and do not have 1-1 mapping between them.</p>

<ul>
	<li><span class="caps">SQL</span>_ID is just another representation of <span class="caps">SQL </span>text (you can read about it <a href="http://blog.tanelpoder.com/2009/02/22/sql_id-is-just-a-fancy-representation-of-hash-value/"> in Tanel Poder&#8217;s blog</a>)</li>

	<li>While baseline is about <span class="caps">SQL </span><em>plan</em> (group of plans, really) and technically the same group of plans can be attached to many &#8220;distinct&#8221; SQLs.

</ul>

<p>Cheers!</p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/t_6H6_J7z3g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/oracle/how-to-find-spm-baseline-by-sql_id/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://intermediatesql.com/oracle/how-to-find-spm-baseline-by-sql_id/</feedburner:origLink></item>
		<item>
		<title>How to freeze ORACLE database by strace</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/2Hd3fWPINO8/</link>
		<comments>http://intermediatesql.com/linux/how-to-freeze-oracle-database-by-strace/#comments</comments>
		<pubDate>Fri, 18 Feb 2011 17:50:00 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[11g]]></category>
		<category><![CDATA[Problem]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=627</guid>
		<description><![CDATA[Sometimes when you trace things, you can discover some really interesting (and unexpected) stuff. For example, here is a simple way to &#8220;freeze&#8221; your ORACLE database, which I &#8220;discovered&#8221; while tracing system calls in LGWR process (ORACLE 11.2.0.2 on Linux 2.6.18 &#215;64). Here goes &#8230; To freeze your ORACLE database start with the following unassuming [...]]]></description>
				<content:encoded><![CDATA[<p>Sometimes when you trace things, you can discover some really interesting (and unexpected) stuff.</p>

<p>For example, here is a simple way to &#8220;freeze&#8221; your <span class="caps">ORACLE </span>database, which I &#8220;discovered&#8221; while tracing system calls in <strong><span class="caps">LGWR</span></strong> process (ORACLE 11.2.0.2 on Linux 2.6.18 &#215;64).</p>

<p><span id="more-627"></span></p>

<p>Here goes &#8230; To freeze your <span class="caps">ORACLE </span>database start with the following unassuming strace command on your <span class="caps">LGWR</span>:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># Start STRACE for your LGWR</span><br />
Linux<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">strace</span> <span style="color: #660033;">-p</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>pgrep <span style="color: #660033;">-fx</span> ora_lgwr_<span style="color: #007800;">$ORACLE_SID</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #660033;">-o</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>l.out <span style="color: #660033;">-T</span> <span style="color: #000000; font-weight: bold;">&amp;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000;">2402</span><br />
Process <span style="color: #000000;">19998</span> attached - interrupt to quit</div></div>

<p>At this point, nothing bad happens yet, trace data is flowing into /tmp/l.out and the database is, in general, happy. </p>

<p>Weirdness starts when you detach (or kill) your <strong>strace</strong> process.</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Linux<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #000000; font-weight: bold;">%</span>1<br />
Process <span style="color: #000000;">19998</span> detached</div></div>

<p>At the first glance, nothing seems to be wrong, the database is still running, sessions are connected and executing queries etc. </p>

<p>But when you try to <span class="caps">COMMIT </span>(or execute <span class="caps">DDL </span>which commits implicitly)</p>

<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SQL<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t <span style="color: #66cc66;">&#40;</span>n <span style="color: #993333; font-weight: bold;">NUMBER</span><span style="color: #66cc66;">&#41;</span>;</div></div>

<p>Your command will wait &#8230; and wait &#8230; and wait &#8230;</p>

<p><strong>v$session</strong> will show that your session (along with all others trying to do commits) is waiting for <strong>log file sync</strong> wait event. But why ?</p>

<p>A quick look at <span class="caps">LGWR </span>process state clarifies the issue:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Linux<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>pgrep <span style="color: #660033;">-fx</span> ora_lgwr_<span style="color: #007800;">$ORACLE_SID</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
&nbsp; PID TTY &nbsp; &nbsp; &nbsp;STAT &nbsp; TIME COMMAND<br />
<span style="color: #000000;">19998</span> ? &nbsp; &nbsp; &nbsp; &nbsp;Ts &nbsp; &nbsp; <span style="color: #000000;">0</span>:00 ora_lgwr_test11</div></div>

<p>According to ps manual, <strong>T state</strong> means that the process is <em>&#8220;Stopped, either by a job control signal or because it is being traced&#8221;</em> (the latter is a lie because tracing has just stopped). And, unfortunately in this case, unless something happens, <span class="caps">LGWR </span>will be in this state forever stalling all database activity.</p>

<p>So, the question becomes, how can we &#8220;unstop&#8221; the <span class="caps">LGWR </span>?</p>

<p>Surprisingly, the most straightforward method is to <strong>attach strace to <span class="caps">LGWR </span>again</strong>. This unfreezes the database and let transactions flow. Unfortunately, that also means that trace information is also collected, which generally slows things down.</p>

<p>To avoid it, <strong>a better way is to send <span class="caps">LGWR </span>(POSIX) <span class="caps">SIGCONT</span></strong> signal to nudge it into &#8220;ready to run&#8221; state.</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Linux<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-SIGCONT</span> <span style="color: #000000;">19998</span><br />
Linux<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>pgrep <span style="color: #660033;">-fx</span> ora_lgwr_<span style="color: #800000;">${ORACLE_SID}</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
&nbsp; PID TTY &nbsp; &nbsp; &nbsp;STAT &nbsp; TIME COMMAND<br />
<span style="color: #000000;">19998</span> ? &nbsp; &nbsp; &nbsp; &nbsp;Ss &nbsp; &nbsp; <span style="color: #000000;">0</span>:00 ora_lgwr_test11</div></div>

<p>which solves the issue.</p>

<p>I&#8217;ve seen it on a couple of my systems and it &#8220;works&#8221; with both 11.1.0.7 and 11.2.0.2 databases. This seems like a bug and I do not know whether it only applies on my particular strace/Linux/ORACLE version(s), but, well, sometimes you just have to deal with the problems like that &#8230;</p>

<p>Cheers!</p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/2Hd3fWPINO8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/linux/how-to-freeze-oracle-database-by-strace/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://intermediatesql.com/linux/how-to-freeze-oracle-database-by-strace/</feedburner:origLink></item>
		<item>
		<title>ora_cpu.pl tool for Linux</title>
		<link>http://feedproxy.google.com/~r/IntermediateSql/~3/0bX9hAgD6ig/</link>
		<comments>http://intermediatesql.com/linux/ora_cpu-pl-tool-for-linux/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 03:09:02 +0000</pubDate>
		<dc:creator>Maxym Kharchenko</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[AIX]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://intermediatesql.com/?p=608</guid>
		<description><![CDATA[A very brief update. One of the smaller (and nicer) part of ORA_MEM package is ora_cpu.pl utility that shows graphically what active db processes are doing at the moment &#8230; be it running SQL, accessing db object or waiting for something. As an added benefit, it also shows some important OS statistics, such as process [...]]]></description>
				<content:encoded><![CDATA[<p>A very brief update. </p>

<p>One of the smaller (and nicer) part of <strong><span class="caps">ORA</span>_MEM</strong> package is <strong>ora_cpu.pl</strong> utility that shows graphically what active db processes are doing at the moment &#8230; be it running <span class="caps">SQL, </span>accessing db object or waiting for something. </p>

<p>As an added benefit, it also shows some important OS statistics, such as process state or &#8216;spot&#8217; <span class="caps">CPU </span>utilization for your database sessions. </p>

<p><span id="more-608"></span></p>

<p>Previously, this tool was only available for <span class="caps">AIX, </span>but now I ported it to Linux as well.</p>

<p>If you run it as:</p>

<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">UNIX<span style="color: #000000; font-weight: bold;">&gt;</span> ora_cpu.pl <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&lt;</span>db connection with SELECT ANY DICTIONARY privilege<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #660033;">-a</span> <span style="color: #000000;">5</span></div></div>

<p>You will see a <em>color coded</em> picture like the one below (click on it to see the picture full scale):</p>

<p><a href="http://intermediatesql.com/wp-content/uploads/2011/02/ora_cpu.jpg"><img src="http://intermediatesql.com/wp-content/uploads/2011/02/ora_cpu-1024x908.jpg" alt="ora_cpu at work" title="ora_cpu at work" width="550" height="487" class="alignnone size-large wp-image-610" /></a></p>

<p>Alternatively, run it with -h option to see what else the tool can do.</p>

<p>Linux version of <strong>ora_cpu.pl</strong> is now available at the <span class="caps">TOOLS </span>section of the site, or you can use <a href='http://intermediatesql.com/wp-content/uploads/2011/02/ora_cpu.linux_.tar.gz'>this</a> direct link to download.</p>

<p>Give it a try and Enjoy <img src='http://intermediatesql.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p><img src="http://feeds.feedburner.com/~r/IntermediateSql/~4/0bX9hAgD6ig" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://intermediatesql.com/linux/ora_cpu-pl-tool-for-linux/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<enclosure url="http://intermediatesql.com/wp-content/uploads/2011/02/ora_cpu.linux_.tar.gz" length="16293" type="application/x-tar" /><media:content url="http://intermediatesql.com/wp-content/uploads/2011/02/ora_cpu.linux_.tar.gz" fileSize="16293" type="application/x-tar" /><feedburner:origLink>http://intermediatesql.com/linux/ora_cpu-pl-tool-for-linux/</feedburner:origLink></item>
	<copyright>(c) Maxym Kharchenko</copyright><media:credit role="author">Maxym Kharchenko</media:credit><media:rating>nonadult</media:rating></channel>
</rss>
