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

<channel>
	<title>OS与Oracle &#187; Tunning</title>
	<atom:link href="http://www.os2ora.com/tag/tunning/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.os2ora.com</link>
	<description>专注于现实世界Oracle数据库的高性能，高可扩展性与新一代数据库Exadata架构</description>
	<lastBuildDate>Fri, 16 Jul 2010 02:55:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>cardinality feedback</title>
		<link>http://www.os2ora.com/cardinality-feedback/</link>
		<comments>http://www.os2ora.com/cardinality-feedback/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 02:55:51 +0000</pubDate>
		<dc:creator>Kaya</dc:creator>
				<category><![CDATA[数据库性能调优]]></category>
		<category><![CDATA[11G]]></category>
		<category><![CDATA[cardinality feedback]]></category>
		<category><![CDATA[Tunning]]></category>

		<guid isPermaLink="false">http://www.os2ora.com/cardinality-feedback/</guid>
		<description><![CDATA[到了10g的时代，cardinality feedback这个词正在变得越来越流行。我想，开始导致这个词流行的或许不是来自Oracle官方的推广，而是来自Wolfgang Breitling在Hotsos Symposium 2006上的一个演讲。
到了11g，Oracle有个new feature就叫做cardinality feedback...]]></description>
			<content:encoded><![CDATA[<p><span class="Apple-style-span" style="word-spacing: 0px; font: medium simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(85,85,85); font-family: verdana, &#39;BitStream vera Sans&#39;, helvetica, sans-serif; text-align: left">Kaya 发表于<span class="Apple-converted-space">&#160;</span><a style="font-weight: bold; color: rgb(102,102,102); text-decoration: underline" href="http://www.os2ora.com/">os2ora.com</a></span></span></p>
<p><span class="Apple-style-span" style="word-spacing: 0px; font: medium simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 12px; color: rgb(85,85,85); font-family: verdana, &#39;BitStream vera Sans&#39;, helvetica, sans-serif; text-align: left">到了10g的时代，cardinality feedback这个词正在变得越来越流行。我想，开始导致这个词流行的或许不是来自Oracle官方的推广，而是来自Wolfgang Breitling在<a href="https://portal.hotsos.com/events/SYM06">Hotsos Symposium 2006</a>上的一个演讲: </span></span>Tuning by Cardinality Feedback: Method and Examples. 从作者的网站有下面的信息:</p>
<blockquote><p><b>Tuning by Cardinality Feedback</b></p>
<p>Tuning by cardinality feedback looks at discrepancies between estimated and real row source cardinalities of an execution plan and attempts to find ways to correct the CBO’s error in estimation and trusting it to find a better plan based on the corrected, more accurate estimates.</p>
<p>Faced with an underperforming SQL, the question the TCF method is trying to answer is not      <br />&#160;&#160;&#160; What would be a better access plan?       <br />But instead       <br />&#160;&#160;&#160; Why is this plan, which the CBO chose as optimal, performing so poorly?</p>
<p>Once the answer to that question is found, the next goal is to find a way to remedy the cause for the miscalculation, but ultimately get out of the way and let the CBO do its job again.</p>
<p>The presentation was given at the 2006 Hotsos Symposium on Oracle® System Performance March 5–9, 2006 in Dallas, Texas and at CBO Days June 21-22 in Zurich, Switzerland.</p>
<p><a href="http://www.centrexcc.com/Tuning%20by%20Cardinality%20Feedback.pdf">Paper</a>&#160; <a href="http://www.centrexcc.com/Tuning%20by%20Cardinality%20Feedback.ppt.pdf">Presentation</a></p>
</blockquote>
<p>这种方法，也是我们平常调优SQL的最主要方法。虽然以前没有专门介绍过，不过有一些文章还是偶尔提及的，例如这篇: <a href="http://www.os2ora.com/fantastic-11gr2-sql-monitor-report/">11gR2出色的SQL Monitor Report</a>, 提到了用SQL Monitor Report查看actual rows与estimate rows的方法。</p>
<p>网上的另一个介绍cardinality feedback的演讲可以参考这一篇，<a href="http://jonathanlewis.wordpress.com/2009/05/11/cardinality-feedback/">http://jonathanlewis.wordpress.com/2009/05/11/cardinality-feedback/</a>,作者是<span class="Apple-style-span" style="word-spacing: 0px; font: medium simsun; text-transform: none; color: rgb(0,0,0); text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span class="Apple-style-span" style="font-size: 13px; line-height: 21px; font-family: verdana, tahoma, arial, sans-serif"><strong><em>Michelle Deng，</em></strong><strong><em>Sanofi Aventis.</em></strong><font color="#555555" size="2">注意这里已经是2009年的事情了。</font></span></span></p>
<p>看看Oracle内部开发人员的演讲吧，这是VLDB 2008上的一个演讲.</p>
<p>Allison W. Lee, Mohamed Zaït: Closing the query processing loop in Oracle 11g: <a href="http://www.vldb.org/pvldb/1/1454178.pdf">Paper</a>, <a href="https://www.se.auckland.ac.nz/conferences/VLDB2008resources/presentations/papers/I14.ppt">Presentation</a></p>
<p>这俨然成为了11g的一个new feature，如何你手头有Oracle 11g，你甚至现在就可以演练一下。</p>
<p>下面是一个用到了cardinality feedback的执行计划，注意最后一行: cardinality feedback used for this statement</p>
<p>&#160;</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">---------------------------------------------------------------------------------------------------------------</span>
<span style="color: #66cc66;">|</span> Id  <span style="color: #66cc66;">|</span> Operation                      <span style="color: #66cc66;">|</span> Name                         <span style="color: #66cc66;">|</span> Rows  <span style="color: #66cc66;">|</span> Bytes <span style="color: #66cc66;">|</span> Cost <span style="color: #66cc66;">&#40;</span>%CPU<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span> Time     <span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">---------------------------------------------------------------------------------------------------------------</span>
<span style="color: #66cc66;">|</span>   <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">SELECT</span> STATEMENT               <span style="color: #66cc66;">|</span>                              <span style="color: #66cc66;">|</span>       <span style="color: #66cc66;">|</span>       <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">98089</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span>          <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>   <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span>  SORT <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>                 <span style="color: #66cc66;">|</span>                              <span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">97</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">98089</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span> 00:<span style="color: #cc66cc;">19</span>:<span style="color: #cc66cc;">38</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|*</span>  <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>   HASH <span style="color: #993333; font-weight: bold;">JOIN</span>                    <span style="color: #66cc66;">|</span>                              <span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">97</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">98088</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span> 00:<span style="color: #cc66cc;">19</span>:<span style="color: #cc66cc;">38</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>   <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span>    <span style="color: #993333; font-weight: bold;">VIEW</span>                        <span style="color: #66cc66;">|</span> VW_NSO_1                     <span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">36</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">47108</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span> 00:09:<span style="color: #cc66cc;">26</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>   <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>     SORT <span style="color: #993333; font-weight: bold;">UNIQUE</span>                <span style="color: #66cc66;">|</span>                              <span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>   <span style="color: #cc66cc;">148</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">47108</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">51</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span> 00:09:<span style="color: #cc66cc;">26</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>   <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span>      UNION<span style="color: #66cc66;">-</span><span style="color: #993333; font-weight: bold;">ALL</span>                 <span style="color: #66cc66;">|</span>                              <span style="color: #66cc66;">|</span>       <span style="color: #66cc66;">|</span>       <span style="color: #66cc66;">|</span>            <span style="color: #66cc66;">|</span>          <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|*</span>  <span style="color: #cc66cc;">6</span> <span style="color: #66cc66;">|</span>       <span style="color: #993333; font-weight: bold;">TABLE</span> ACCESS STORAGE <span style="color: #993333; font-weight: bold;">FULL</span><span style="color: #66cc66;">|</span> TA                           <span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">62</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">23549</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span> 00:04:<span style="color: #cc66cc;">43</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|*</span>  <span style="color: #cc66cc;">7</span> <span style="color: #66cc66;">|</span>       <span style="color: #993333; font-weight: bold;">TABLE</span> ACCESS STORAGE <span style="color: #993333; font-weight: bold;">FULL</span><span style="color: #66cc66;">|</span> TA                           <span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">86</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">23558</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span> 00:04:<span style="color: #cc66cc;">43</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|*</span>  <span style="color: #cc66cc;">8</span> <span style="color: #66cc66;">|</span>    <span style="color: #993333; font-weight: bold;">TABLE</span> ACCESS STORAGE <span style="color: #993333; font-weight: bold;">FULL</span>   <span style="color: #66cc66;">|</span> TB                           <span style="color: #66cc66;">|</span>   469K<span style="color: #66cc66;">|</span>    39M<span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">50978</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">|</span> 00:<span style="color: #cc66cc;">10</span>:<span style="color: #cc66cc;">12</span> <span style="color: #66cc66;">|</span>
<span style="color: #808080; font-style: italic;">---------------------------------------------------------------------------------------------------------------</span>
&nbsp;
<span style="color: #66cc66;">....</span>
&nbsp;
Note
<span style="color: #808080; font-style: italic;">-----</span>
   <span style="color: #66cc66;">-</span> cardinality feedback used <span style="color: #993333; font-weight: bold;">FOR</span> this statement</pre></div></div>

<p>可以google下”<a href="http://www.google.com/search?q=%2B%22cardinality+feedback+used+for+this+statement%22+%2Boracle">cardinality feedback</a>”。会有人其它人的演练报告。</p>
<p>想想吧，以后的DBA，回顾这段历史，想想以前的人们如何用手工的方法用cardinality feedback 进行调优，是不是会和我们现在想着过去人们如何用手工方法进行space management, memory management的场景类似？ evolving, evolving, evolving…</p>
]]></content:encoded>
			<wfw:commentRss>http://www.os2ora.com/cardinality-feedback/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>利用SQL Monitor Report对SQL进行诊断与调优</title>
		<link>http://www.os2ora.com/use-sql-monitor-report-to-tune-and-diagnose-sql/</link>
		<comments>http://www.os2ora.com/use-sql-monitor-report-to-tune-and-diagnose-sql/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 13:38:51 +0000</pubDate>
		<dc:creator>Kaya</dc:creator>
				<category><![CDATA[Exadata]]></category>
		<category><![CDATA[Oracle管理与维护]]></category>
		<category><![CDATA[数据库性能调优]]></category>
		<category><![CDATA[Hash Join]]></category>
		<category><![CDATA[Hash Join Buffered]]></category>
		<category><![CDATA[SQL Monitor Report]]></category>
		<category><![CDATA[Tunning]]></category>
		<category><![CDATA[诊断]]></category>

		<guid isPermaLink="false">http://www.os2ora.com/use-sql-monitor-report-to-tune-and-diagnose-sql/</guid>
		<description><![CDATA[本文要分析的是下面这个SQL，执行了半个多钟头还没返回结果。
Wait events 是了解Oracle运行状态的一个重要途径。对于某个具体的SQL，SQL Monitor Report提供了drill down的方式得到这个具体SQL在运行中的wait events的分布情况，下面是SQL Monitor Report的相应图形。
这里最突出的等待事件是enq: TS - contention，这是关于临时segment的等待事件，这可能是一般的表空间的争用(例如并行直接路径加载数据)，也可能是临时表空间的争用(例如为了hash join或者sort)。那么这时临时表空间的增长状态是怎么样子的呢？SQL Monitor Report提供了这方面的信息......]]></description>
			<content:encoded><![CDATA[<p>Kaya 发表于 <a href="http://www.os2ora.com/">os2ora.com</a>&#160;</p>
<p>本文要分析的是下面这个SQL，执行了半个多钟头还没返回结果。</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #808080; font-style: italic;">/*+ APPEND */</span>
<span style="color: #993333; font-weight: bold;">INTO</span>
 T_D
<span style="color: #993333; font-weight: bold;">SELECT</span>
 <span style="color: #66cc66;">*</span>
<span style="color: #993333; font-weight: bold;">FROM</span>
 T_A a
<span style="color: #66cc66;">,</span>T_B b
<span style="color: #66cc66;">,</span>T_C c
<span style="color: #993333; font-weight: bold;">WHERE</span> a<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> b<span style="color: #66cc66;">.</span>id
<span style="color: #993333; font-weight: bold;">AND</span>   b<span style="color: #66cc66;">.</span>number <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>number
;</pre></div></div>

<p>&#160;</p>
<p>Wait events 是了解Oracle运行状态的一个重要途径。对于某个具体的SQL，SQL Monitor Report提供了drill down的方式得到这个具体SQL在运行中的wait events的分布情况，下面是SQL Monitor Report的相应图形。</p>
<p><a href="http://www.os2ora.com/wp-content/uploads/2010/01/image8.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="216" alt="image" src="http://www.os2ora.com/wp-content/uploads/2010/01/image_thumb8.png" width="814" border="0" /></a> </p>
<p>这里最突出的等待事件是enq: TS &#8211; contention，这是关于临时segment的等待事件，这可能是一般的表空间的争用(例如并行直接路径加载数据)，也可能是临时表空间的争用(例如为了hash join或者sort)。那么这时临时表空间的增长状态是怎么样子的呢？SQL Monitor Report提供了这方面的信息。</p>
<p><a href="http://www.os2ora.com/wp-content/uploads/2010/01/image9.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="156" alt="image" src="http://www.os2ora.com/wp-content/uploads/2010/01/image_thumb9.png" width="804" border="0" /></a> </p>
<p>可以看出，temp space从开始的3G慢慢增长到20G，整个过程2800 seconds，如果算下速度的话17GB/2800=6MB/s。这刚好与上面的等待事件 enq: TS &#8211; contention相吻合。</p>
<p>这是一个直接加载数据的例子，在这个例子中，最后表的大小是30GB左右。可以怀疑，这些临时表空间的分配是为了存放最后放入目标表的数据。</p>
<p>这于这个等待事件enq: TS &#8211; contention发生在这个SQL执行过程中的哪一步呢？SQL Monitor Report也给出了答案，在执行计划那一页：</p>
</p>
<p><a href="http://www.os2ora.com/wp-content/uploads/2010/01/image10.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="323" alt="image" src="http://www.os2ora.com/wp-content/uploads/2010/01/image_thumb10.png" width="804" border="0" /></a> </p>
</p>
<p>注意上图鼠标位置，标志着位于HASH JOIN BUFFERED这个步骤的等待事件enq: TS – contention占了这个SQL所有等待活动的97%！</p>
<p>问题到这里已经基本明了了：</p>
<p>1. 优化器采用Hash Join Buffered的方式返回三个表Join的结果，当为这些结果集分配临时表的空间时，碰到了严重的竞争。</p>
<p>2. 由于分配速度极其缓慢，导致了整个SQL超过97%的时间花在了这个等待事件上，通过去掉这个等待事件，这个SQL应该能提升上百倍的速度。</p>
<p>解决这个问题的根本在于加速临时表空间的回收速度，不过，也有workaround的办法，那就是预先分配足够的临时表空间，避免回收临时表空间时出现TS &#8211; contention竞争。</p>
<p>另外一种思路，则在于减少对临时表空间的利用，在这里，为什么要用到这么多临时表空间在于Oracle采用Hash Join Buffered而不是采用Hash Join，这意味着Oracle会先对Join的结果集进行buffer，等到所有结果ready之后再写到目标表，另一种思路当然是让结果直接写到目标表中，也就是实现并行的DML插入操作。方法很简单：在运行这个SQL之前加上</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> session enable parallel dml;</pre></div></div>

<p>即可，在这种情形下，Oracle不需要再为结果集分配临时表空间，同时，由于采用并于DML操作，整个SQL的运行时间会得到很大的提速。下面是相应的执行计划。</p>
<p><a href="http://www.os2ora.com/wp-content/uploads/2010/01/image11.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="323" alt="image" src="http://www.os2ora.com/wp-content/uploads/2010/01/image_thumb11.png" width="804" border="0" /></a> </p>
<p>可以看出，Hash Join Buffered变成了Hash Join，同时，等待事件enq: TS contention已然消失，换成了”喜闻乐见”的ON CPU。同时，整个SQL在小于30s的时间内完成了。</p>
<p>不可否认，Hash Join Buffered是这里极其隐蔽，可能不小心就被忽略了，以为上面两个执行计划就是LOAD AS SELECT的位置从第2行搬到了第4行，其实背后发生的事情远没有这么简单。以后有机会再对这个话题进行展开吧。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.os2ora.com/use-sql-monitor-report-to-tune-and-diagnose-sql/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
