<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Holiday Quiz</title>
	<atom:link href="http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/</link>
	<description>Just another Oracle weblog</description>
	<lastBuildDate>Mon, 20 May 2013 01:44:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Short Sorts &#171; Oracle Scratchpad</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35111</link>
		<dc:creator><![CDATA[Short Sorts &#171; Oracle Scratchpad]]></dc:creator>
		<pubDate>Mon, 28 Dec 2009 18:31:27 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35111</guid>
		<description><![CDATA[[...] sorting, trace files &#8212; Jonathan Lewis @ 6:29 pm UTC Dec 28,2009 Tags: sorting  I posted a little holiday quiz &#8211; timed to appear just before midnight (GMT) on 24th December &#8211; that asked about the [...]]]></description>
		<content:encoded><![CDATA[<p>[...] sorting, trace files &#8212; Jonathan Lewis @ 6:29 pm UTC Dec 28,2009 Tags: sorting  I posted a little holiday quiz &#8211; timed to appear just before midnight (GMT) on 24th December &#8211; that asked about the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35110</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Mon, 28 Dec 2009 16:37:42 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35110</guid>
		<description><![CDATA[I think that Randolf has a point here - there must be more variables to consider. What if table T1 is range partitioned on the SORTCODE column? I put together a test case that shows that with range partitioning, only 12 rows will be sorted for the second SQL statement (the number varies based on the granularity of the partitioning) - without range partitioning, the second SQL statement sorts 1,000,000 rows.  In either case, DBMS_XPLAN on 11.1.0.7 reports that the second SQL statement uses 2MB of memory, even though the actual amount used will likely be less (possibly close to what my other responses suggest).]]></description>
		<content:encoded><![CDATA[<p>I think that Randolf has a point here &#8211; there must be more variables to consider. What if table T1 is range partitioned on the SORTCODE column? I put together a test case that shows that with range partitioning, only 12 rows will be sorted for the second SQL statement (the number varies based on the granularity of the partitioning) &#8211; without range partitioning, the second SQL statement sorts 1,000,000 rows.  In either case, DBMS_XPLAN on 11.1.0.7 reports that the second SQL statement uses 2MB of memory, even though the actual amount used will likely be less (possibly close to what my other responses suggest).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Randolf Geist</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35108</link>
		<dc:creator><![CDATA[Randolf Geist]]></dc:creator>
		<pubDate>Mon, 28 Dec 2009 13:15:59 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35108</guid>
		<description><![CDATA[Hm, interesting idea. Have you tried that? It would be interesting to see how this MV definition is supposed to look like to give the response directly in 1 gets, in particular for the first query, but also for the second one?]]></description>
		<content:encoded><![CDATA[<p>Hm, interesting idea. Have you tried that? It would be interesting to see how this MV definition is supposed to look like to give the response directly in 1 gets, in particular for the first query, but also for the second one?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lascoltodelvenerdi</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35105</link>
		<dc:creator><![CDATA[lascoltodelvenerdi]]></dc:creator>
		<pubDate>Mon, 28 Dec 2009 10:26:58 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35105</guid>
		<description><![CDATA[Well.

The first query is quite clear: Oracle read all the table, sort all the rows. Memory usage depends on Oracle version as different version of Oracle uses different sort algorithms, as the column is of 6 bytes (I&#039;m assuming 1 char=8 bit) then you have more or less 6*1.000.000, so 6M of RAM, but if some kind of &quot;hash sort&quot; is used, this can be lower.

As stated by many here Oracle (from 9 and up?) can do a &quot;SORT ORDER BY STOPKEY&quot; so in the second case, it will use an array of dimension 10 (the &quot;rownum&quot; we want in the example) and sort/replace only 10 rows (full table scan still apply). Memory used 10*6=60 bytes.

Bye,
Antonio]]></description>
		<content:encoded><![CDATA[<p>Well.</p>
<p>The first query is quite clear: Oracle read all the table, sort all the rows. Memory usage depends on Oracle version as different version of Oracle uses different sort algorithms, as the column is of 6 bytes (I&#8217;m assuming 1 char=8 bit) then you have more or less 6*1.000.000, so 6M of RAM, but if some kind of &#8220;hash sort&#8221; is used, this can be lower.</p>
<p>As stated by many here Oracle (from 9 and up?) can do a &#8220;SORT ORDER BY STOPKEY&#8221; so in the second case, it will use an array of dimension 10 (the &#8220;rownum&#8221; we want in the example) and sort/replace only 10 rows (full table scan still apply). Memory used 10*6=60 bytes.</p>
<p>Bye,<br />
Antonio</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bernard Polarski</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35103</link>
		<dc:creator><![CDATA[Bernard Polarski]]></dc:creator>
		<pubDate>Mon, 28 Dec 2009 09:02:42 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35103</guid>
		<description><![CDATA[We must assume that this is a regular heap table with no tricks, otherwise we could also assume that there is a MV that gives directly the response in 1 gets.]]></description>
		<content:encoded><![CDATA[<p>We must assume that this is a regular heap table with no tricks, otherwise we could also assume that there is a MV that gives directly the response in 1 gets.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Narendra</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35101</link>
		<dc:creator><![CDATA[Narendra]]></dc:creator>
		<pubDate>Sun, 27 Dec 2009 19:48:03 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35101</guid>
		<description><![CDATA[Jonathan,

You are killing me. :)
I just tried to run the test and the results left me &quot;shattered&quot;... Here are the details:
First the spool file:
======================================================
SQL&gt; select * from v$version ;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE	10.2.0.1.0	Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production

SQL&gt; create table t (sortcode varchar2(10)) ;

Table created.

SQL&gt; REM I am trying to populate the table with the specified criteria.
SQL&gt; insert /*+ APPEND */ into t select dbms_random.string(&#039;a&#039;,1)&#124;&#124;ROUND(dbms_random.value(1,420000), 0) from dual connect by level  commit ;

Commit complete.

SQL&gt; select max(cnt) from (select count(*) cnt from t group by sortcode) ;

  MAX(CNT)
----------
         4

SQL&gt; REM Now I will enable the trace as well as check V$MYSTAT for no. of rows sorted
SQL&gt; alter session set events &#039;10046 trace name context forever, level 12&#039; ;

Session altered.

SQL&gt; select b.name, a.value from V$MYSTAT a, V$STATNAME b where a.statistic# = b.statistic# and b.name like &#039;sort%&#039; ;

NAME                                                                  VALUE
---------------------------------------------------------------- ----------
sorts (memory)                                                          204
sorts (disk)                                                              0
sorts (rows)                                                           1159

SQL&gt; REM first query in the quiz

SQL&gt; select sortcode from t order by sortcode ;

-- Output removed

SQL&gt; select b.name, a.value from V$MYSTAT a, V$STATNAME b where a.statistic# = b.statistic# and b.name like &#039;sort%&#039; ;

NAME                                                                  VALUE
---------------------------------------------------------------- ----------
sorts (memory)                                                          205
sorts (disk)                                                              0
sorts (rows)                                                        1001159

SQL&gt; REM now another query
SQL&gt; select sortcode from (select sortcode from t order by sortcode) where rownum  &lt;= 10
;
-- Output removed

SQL&gt; select b.name, a.value from V$MYSTAT a, V$STATNAME b where a.statistic# = b.statistic# and b.name like &#039;sort%&#039; ;

NAME                                                                  VALUE
---------------------------------------------------------------- ----------
sorts (memory)                                                          206
sorts (disk)                                                              0
sorts (rows)                                                        2001159

SQL&gt; spool off
=====================================================

The V$ views show that both the sqls sorted 1 million rows (but is it so?)

So then the trace details:
=====================================================
select sortcode 
from
 t order by sortcode 


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          1          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch    66668      5.70       5.09          0       1768          0     1000000
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total    66670      5.70       5.09          0       1769          0     1000000

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 52  

Rows     Row Source Operation
-------  ---------------------------------------------------
1000000  SORT ORDER BY (cr=1768 pr=0 pw=0 time=9759508 us)
1000000   TABLE ACCESS FULL T (cr=1768 pr=0 pw=0 time=10000070 us)


Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                   66668        0.00          0.32
  SQL*Net message from client                 66668        0.20        127.59
********************************************************************************

select sortcode 
from
 (select sortcode from t order by sortcode) where rownum &lt;= 10 


call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          2          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch        2      0.34       0.34          0       1768          0          10
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total        4      0.34       0.34          0       1770          0          10

Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 52  

Rows     Row Source Operation
-------  ---------------------------------------------------
     10  COUNT STOPKEY (cr=1768 pr=0 pw=0 time=343142 us)
     10   VIEW  (cr=1768 pr=0 pw=0 time=343058 us)
     10    SORT ORDER BY STOPKEY (cr=1768 pr=0 pw=0 time=343025 us)
1000000     TABLE ACCESS FULL T (cr=1768 pr=0 pw=0 time=10000088 us)


Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       2        0.00          0.00
  SQL*Net message from client                     2        0.00          0.00
=====================================================

The &quot;Row-source Operation&quot; section for second query shows &quot;Sort order by stopkey&quot; step producing only 10 rows. Does that not mean sort took place only on 10 rows?
But then which one is correct? :(

p.s. Sometimes, I think I should believe in &quot;Don Burleson&quot; and &quot;Don Lewis&quot; (from oracle forums) argument that Oracle is software and not a science...
:)]]></description>
		<content:encoded><![CDATA[<p>Jonathan,</p>
<p>You are killing me. :)<br />
I just tried to run the test and the results left me &#8220;shattered&#8221;&#8230; Here are the details:<br />
First the spool file:<br />
======================================================<br />
SQL&gt; select * from v$version ;</p>
<p>BANNER<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 &#8211; Prod<br />
PL/SQL Release 10.2.0.1.0 &#8211; Production<br />
CORE	10.2.0.1.0	Production<br />
TNS for Linux: Version 10.2.0.1.0 &#8211; Production<br />
NLSRTL Version 10.2.0.1.0 &#8211; Production</p>
<p>SQL&gt; create table t (sortcode varchar2(10)) ;</p>
<p>Table created.</p>
<p>SQL&gt; REM I am trying to populate the table with the specified criteria.<br />
SQL&gt; insert /*+ APPEND */ into t select dbms_random.string(&#8216;a&#8217;,1)||ROUND(dbms_random.value(1,420000), 0) from dual connect by level  commit ;</p>
<p>Commit complete.</p>
<p>SQL&gt; select max(cnt) from (select count(*) cnt from t group by sortcode) ;</p>
<p>  MAX(CNT)<br />
&#8212;&#8212;&#8212;-<br />
         4</p>
<p>SQL&gt; REM Now I will enable the trace as well as check V$MYSTAT for no. of rows sorted<br />
SQL&gt; alter session set events &#8217;10046 trace name context forever, level 12&#8242; ;</p>
<p>Session altered.</p>
<p>SQL&gt; select b.name, a.value from V$MYSTAT a, V$STATNAME b where a.statistic# = b.statistic# and b.name like &#8216;sort%&#8217; ;</p>
<p>NAME                                                                  VALUE<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br />
sorts (memory)                                                          204<br />
sorts (disk)                                                              0<br />
sorts (rows)                                                           1159</p>
<p>SQL&gt; REM first query in the quiz</p>
<p>SQL&gt; select sortcode from t order by sortcode ;</p>
<p>&#8211; Output removed</p>
<p>SQL&gt; select b.name, a.value from V$MYSTAT a, V$STATNAME b where a.statistic# = b.statistic# and b.name like &#8216;sort%&#8217; ;</p>
<p>NAME                                                                  VALUE<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br />
sorts (memory)                                                          205<br />
sorts (disk)                                                              0<br />
sorts (rows)                                                        1001159</p>
<p>SQL&gt; REM now another query<br />
SQL&gt; select sortcode from (select sortcode from t order by sortcode) where rownum  &lt;= 10<br />
;<br />
&#8211; Output removed</p>
<p>SQL&gt; select b.name, a.value from V$MYSTAT a, V$STATNAME b where a.statistic# = b.statistic# and b.name like &#8216;sort%&#8217; ;</p>
<p>NAME                                                                  VALUE<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br />
sorts (memory)                                                          206<br />
sorts (disk)                                                              0<br />
sorts (rows)                                                        2001159</p>
<p>SQL&gt; spool off<br />
=====================================================</p>
<p>The V$ views show that both the sqls sorted 1 million rows (but is it so?)</p>
<p>So then the trace details:<br />
=====================================================<br />
select sortcode<br />
from<br />
 t order by sortcode </p>
<p>call     count       cpu    elapsed       disk      query    current        rows<br />
&#8212;&#8212;- &#8212;&#8212;  &#8212;&#8212;&#8211; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-  &#8212;&#8212;&#8212;-<br />
Parse        1      0.00       0.00          0          1          0           0<br />
Execute      1      0.00       0.00          0          0          0           0<br />
Fetch    66668      5.70       5.09          0       1768          0     1000000<br />
&#8212;&#8212;- &#8212;&#8212;  &#8212;&#8212;&#8211; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-  &#8212;&#8212;&#8212;-<br />
total    66670      5.70       5.09          0       1769          0     1000000</p>
<p>Misses in library cache during parse: 1<br />
Optimizer mode: ALL_ROWS<br />
Parsing user id: 52  </p>
<p>Rows     Row Source Operation<br />
&#8212;&#8212;-  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
1000000  SORT ORDER BY (cr=1768 pr=0 pw=0 time=9759508 us)<br />
1000000   TABLE ACCESS FULL T (cr=1768 pr=0 pw=0 time=10000070 us)</p>
<p>Elapsed times include waiting on following events:<br />
  Event waited on                             Times   Max. Wait  Total Waited<br />
  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-   Waited  &#8212;&#8212;&#8212;-  &#8212;&#8212;&#8212;&#8212;<br />
  SQL*Net message to client                   66668        0.00          0.32<br />
  SQL*Net message from client                 66668        0.20        127.59<br />
********************************************************************************</p>
<p>select sortcode<br />
from<br />
 (select sortcode from t order by sortcode) where rownum &lt;= 10 </p>
<p>call     count       cpu    elapsed       disk      query    current        rows<br />
&#8212;&#8212;- &#8212;&#8212;  &#8212;&#8212;&#8211; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-  &#8212;&#8212;&#8212;-<br />
Parse        1      0.00       0.00          0          2          0           0<br />
Execute      1      0.00       0.00          0          0          0           0<br />
Fetch        2      0.34       0.34          0       1768          0          10<br />
&#8212;&#8212;- &#8212;&#8212;  &#8212;&#8212;&#8211; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-  &#8212;&#8212;&#8212;-<br />
total        4      0.34       0.34          0       1770          0          10</p>
<p>Misses in library cache during parse: 1<br />
Optimizer mode: ALL_ROWS<br />
Parsing user id: 52  </p>
<p>Rows     Row Source Operation<br />
&#8212;&#8212;-  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
     10  COUNT STOPKEY (cr=1768 pr=0 pw=0 time=343142 us)<br />
     10   VIEW  (cr=1768 pr=0 pw=0 time=343058 us)<br />
     10    SORT ORDER BY STOPKEY (cr=1768 pr=0 pw=0 time=343025 us)<br />
1000000     TABLE ACCESS FULL T (cr=1768 pr=0 pw=0 time=10000088 us)</p>
<p>Elapsed times include waiting on following events:<br />
  Event waited on                             Times   Max. Wait  Total Waited<br />
  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-   Waited  &#8212;&#8212;&#8212;-  &#8212;&#8212;&#8212;&#8212;<br />
  SQL*Net message to client                       2        0.00          0.00<br />
  SQL*Net message from client                     2        0.00          0.00<br />
=====================================================</p>
<p>The &quot;Row-source Operation&quot; section for second query shows &quot;Sort order by stopkey&quot; step producing only 10 rows. Does that not mean sort took place only on 10 rows?<br />
But then which one is correct? :(</p>
<p>p.s. Sometimes, I think I should believe in &quot;Don Burleson&quot; and &quot;Don Lewis&quot; (from oracle forums) argument that Oracle is software and not a science&#8230;<br />
:)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Randolf Geist</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35098</link>
		<dc:creator><![CDATA[Randolf Geist]]></dc:creator>
		<pubDate>Sat, 26 Dec 2009 20:40:31 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35098</guid>
		<description><![CDATA[Since this is a quiz for the holidays and it is Jonathan who is raising the question, I could imagine there are more subtle things to consider.

E.g. he says &quot;I have a table with one million rows, there are no indexes on the table&quot;, but what if the table was the index (IOT)?

Since SORTCODE &quot;has no nulls&quot; it could be the leading column of a composite primary key - so an IOT is a possible construct to avoid sort operations.

If you use now a suitable NLS_SORT = binary setting for the assumed CHAR(6) column, both statements could go along without any sort operation at all and no memory required for this.

What about an indexed cluster? Again the index is not on the table, but on the cluster... It&#039;s a bit splitting hairs but just throwing out some ideas.

It is correct that with a heap table the second query can use a &quot;SORT ORDER BY STOPKEY&quot; operation that only needs to keep the top N rows as it goes along the unsorted row source and therefore requires a far smaller workarea size for the sort operation.

Randolf

PS: And no, my presentation above doesn&#039;t (directly) cover this particular question]]></description>
		<content:encoded><![CDATA[<p>Since this is a quiz for the holidays and it is Jonathan who is raising the question, I could imagine there are more subtle things to consider.</p>
<p>E.g. he says &#8220;I have a table with one million rows, there are no indexes on the table&#8221;, but what if the table was the index (IOT)?</p>
<p>Since SORTCODE &#8220;has no nulls&#8221; it could be the leading column of a composite primary key &#8211; so an IOT is a possible construct to avoid sort operations.</p>
<p>If you use now a suitable NLS_SORT = binary setting for the assumed CHAR(6) column, both statements could go along without any sort operation at all and no memory required for this.</p>
<p>What about an indexed cluster? Again the index is not on the table, but on the cluster&#8230; It&#8217;s a bit splitting hairs but just throwing out some ideas.</p>
<p>It is correct that with a heap table the second query can use a &#8220;SORT ORDER BY STOPKEY&#8221; operation that only needs to keep the top N rows as it goes along the unsorted row source and therefore requires a far smaller workarea size for the sort operation.</p>
<p>Randolf</p>
<p>PS: And no, my presentation above doesn&#8217;t (directly) cover this particular question</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Asif Momen</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35093</link>
		<dc:creator><![CDATA[Asif Momen]]></dc:creator>
		<pubDate>Sat, 26 Dec 2009 08:11:19 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35093</guid>
		<description><![CDATA[Here are my two cents:

1) How many rows are sorted in each of these two queries

Query-1 will sort all 1M rows while the query-2 will sort ONLY 10 rows. Both the queries will read all 1M rows but query-2 will be able to limit number of rows to sort by using &quot;SORT ORDER BY STOPKEY&quot; step.


Rows     Row Source Operation
-------  ---------------------------------------------------
     10  COUNT STOPKEY (cr=1645 pr=701 pw=0 time=537221 us)
     10   VIEW  (cr=1645 pr=701 pw=0 time=537190 us)
     10    SORT ORDER BY STOPKEY (cr=1645 pr=701 pw=0 time=537169 us)
1000000     TABLE ACCESS FULL T1 (cr=1645 pr=701 pw=0 time=4000117 us)



2) roughly how much memory would you expect Oracle to use ?

Query-1 uses around 12MB of memory while query-2 uses less than 1 MB. (from v$sesstat view)


Jonathan, this seems not fair. You are enjoying your holidays and making us work on this assignment. ;)]]></description>
		<content:encoded><![CDATA[<p>Here are my two cents:</p>
<p>1) How many rows are sorted in each of these two queries</p>
<p>Query-1 will sort all 1M rows while the query-2 will sort ONLY 10 rows. Both the queries will read all 1M rows but query-2 will be able to limit number of rows to sort by using &#8220;SORT ORDER BY STOPKEY&#8221; step.</p>
<p>Rows     Row Source Operation<br />
&#8212;&#8212;-  &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
     10  COUNT STOPKEY (cr=1645 pr=701 pw=0 time=537221 us)<br />
     10   VIEW  (cr=1645 pr=701 pw=0 time=537190 us)<br />
     10    SORT ORDER BY STOPKEY (cr=1645 pr=701 pw=0 time=537169 us)<br />
1000000     TABLE ACCESS FULL T1 (cr=1645 pr=701 pw=0 time=4000117 us)</p>
<p>2) roughly how much memory would you expect Oracle to use ?</p>
<p>Query-1 uses around 12MB of memory while query-2 uses less than 1 MB. (from v$sesstat view)</p>
<p>Jonathan, this seems not fair. You are enjoying your holidays and making us work on this assignment. ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damir Vadas</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35088</link>
		<dc:creator><![CDATA[Damir Vadas]]></dc:creator>
		<pubDate>Fri, 25 Dec 2009 17:47:01 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35088</guid>
		<description><![CDATA[Johnattan,
This is explained in 
http://www.sqltools-plusplus.org:7676/media/FIRST_ROWS_n%20presentation%20UKOUG%202009.pdf

Damir Vadas
http://damir-vadas.blogspot.com]]></description>
		<content:encoded><![CDATA[<p>Johnattan,<br />
This is explained in<br />
<a href="http://www.sqltools-plusplus.org:7676/media/FIRST_ROWS_n%20presentation%20UKOUG%202009.pdf" rel="nofollow">http://www.sqltools-plusplus.org:7676/media/FIRST_ROWS_n%20presentation%20UKOUG%202009.pdf</a></p>
<p>Damir Vadas<br />
<a href="http://damir-vadas.blogspot.com" rel="nofollow">http://damir-vadas.blogspot.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Santosh Upadhyay</title>
		<link>http://jonathanlewis.wordpress.com/2009/12/24/holiday-quiz/#comment-35086</link>
		<dc:creator><![CDATA[Santosh Upadhyay]]></dc:creator>
		<pubDate>Fri, 25 Dec 2009 17:18:38 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=2724#comment-35086</guid>
		<description><![CDATA[I think....

In the first case, all the 1 million rows will be sorted.

In the second case, the sort area will contain  10 rows sorted. Each row in the table will be traversed but only 10 rows will be sorted at a time, with the new row included or discarded,depending on the sortcode value.

Not sure about the exact calucualtion of memory needed but case 1 will take much . In case 2, sorting 10 rows in RAM will be far less.]]></description>
		<content:encoded><![CDATA[<p>I think&#8230;.</p>
<p>In the first case, all the 1 million rows will be sorted.</p>
<p>In the second case, the sort area will contain  10 rows sorted. Each row in the table will be traversed but only 10 rows will be sorted at a time, with the new row included or discarded,depending on the sortcode value.</p>
<p>Not sure about the exact calucualtion of memory needed but case 1 will take much . In case 2, sorting 10 rows in RAM will be far less.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
