<?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/"
	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>Lczinkoc's Blog</title>
	<atom:link href="http://sqlplsql.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlplsql.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Thu, 27 Oct 2011 12:45:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sqlplsql.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Lczinkoc's Blog</title>
		<link>http://sqlplsql.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sqlplsql.wordpress.com/osd.xml" title="Lczinkoc&#039;s Blog" />
	<atom:link rel='hub' href='http://sqlplsql.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Correlated subqueries and Query Transformations</title>
		<link>http://sqlplsql.wordpress.com/2011/10/27/correlated-subqueries-and-query-transformations-2/</link>
		<comments>http://sqlplsql.wordpress.com/2011/10/27/correlated-subqueries-and-query-transformations-2/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 12:45:17 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=481</guid>
		<description><![CDATA[Many times ask me my students when we have – at least &#8211; two solutions for a particular problem, which is the best queries ? The correct answer is typically complex, and started “ It depends on ….” or “let’s see the environment…”. Let’s see the following simple example: We would like to select those [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=481&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Many times ask me my students when we have – at least &#8211; two solutions for a particular problem, which is the best queries ?<br />
The correct answer is typically complex, and started “ It depends on ….” or “let’s see the environment…”.<br />
Let’s see the following simple example:<br />
We would like to select those employees whose salaries are greater than their department’s average salary!<br />
1. First solution can be:</p>
<p><strong>SELECT /* well-paid people */ e.last_name, e.department_id, e.salary</strong><br />
<strong>FROM employees e</strong><br />
<strong> WHERE e.salary&gt; (SELECT AVG(salary) FROM EMPLOYEES</strong><br />
<strong>WHERE department_id=e.department_id);</strong></p>
<p>This is correlated subquery and we can ask how many times the subquery will be executed?<br />
We will see that the answer is not a number.<br />
2.Second solution can be:<br />
<strong>SELECT /* well-paid people */ e.last_name, e.department_id,</strong><br />
<strong> e.salary ,d.average</strong><br />
<strong> FROM employees e,</strong><br />
<strong> (SELECT department_id, AVG(salary) average FROM employees GROUP BY department_id ) d</strong><br />
<strong> WHERE e.department_id=d.department_id AND e.salary&gt;d.average ;</strong><br />
Now we examine the execution plans (using my sqlid stroed procedure, see an other blog entry .<br />
Let’s the execution plains:<br />
<strong>EXEC sqlid(&#8216;well-paid people&#8217;,'typical&#8217;)</strong></p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/10/elso2.jpg"><img class="alignleft size-full wp-image-504" title="elso" src="http://sqlplsql.files.wordpress.com/2011/10/elso2.jpg?w=450&#038;h=285" alt="" width="450" height="285" /></a></p>
<p>For the second solution, please consider the following figure:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/10/masodik5.jpg"><img class="alignleft size-full wp-image-505" title="masodik" src="http://sqlplsql.files.wordpress.com/2011/10/masodik5.jpg?w=450&#038;h=299" alt="" width="450" height="299" /></a></p>
<p>We can observe – although- the plan hash values are different, actually the content of them are identical!<br />
In other words Oracle executed a query transformation for the second SELECT statement and used the semantically same plan for both quires.<br />
Let’s suppose that we prohibit the query transformation for the first SELECT statement:<br />
<strong>SELECT /*+ NO_QUERY_TRANSFORMATION*/</strong><br />
<strong> e.last_name, e.department_id, e.salary</strong><br />
<strong> FROM employees e</strong><br />
<strong> WHERE e.salary&gt; (SELECT AVG(salary) FROM EMPLOYEES</strong><br />
<strong> WHERE department_id=e.department_id);</strong></p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/10/harmadik.jpg"><img class="alignleft size-full wp-image-501" title="harmadik" src="http://sqlplsql.files.wordpress.com/2011/10/harmadik.jpg?w=450&#038;h=259" alt="" width="450" height="259" /></a></p>
<p>We can realize that this plan is more expensive than previous one.<br />
Be careful with hints!<br />
Sometimes we can demolish the server&#8217;s good plan with our hints!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/481/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/481/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/481/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=481&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2011/10/27/correlated-subqueries-and-query-transformations-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/10/elso2.jpg" medium="image">
			<media:title type="html">elso</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/10/masodik5.jpg" medium="image">
			<media:title type="html">masodik</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/10/harmadik.jpg" medium="image">
			<media:title type="html">harmadik</media:title>
		</media:content>
	</item>
		<item>
		<title>Flashback queries for Data Dictionary</title>
		<link>http://sqlplsql.wordpress.com/2011/08/17/flashback-queries-for-data-dictionary/</link>
		<comments>http://sqlplsql.wordpress.com/2011/08/17/flashback-queries-for-data-dictionary/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 11:00:06 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=459</guid>
		<description><![CDATA[Oracle developers sometimes delete their subprograms, views, tables etc accidentally. From Oracle 10g we have opportunity to salvage them from the undo segment. Note that &#8211; by default – the Oracle does not guarantee the successful salvage in 100%. (See later in this article or the documentation fore more detail) The FLASHBACK statement &#8211; with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=459&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1 align="center"></h1>
<p>Oracle developers sometimes delete their subprograms, views, tables etc accidentally.<br />
From Oracle 10g we have opportunity to salvage them from the undo segment.<br />
Note that &#8211; by default – the Oracle does not guarantee the successful salvage in 100%.<br />
(See later in this article or the documentation fore more detail)</p>
<p>The FLASHBACK statement &#8211; with wide range of  functionalities &#8211; ensures the possibility to restore earlier state of objects or completely recreate them.<br />
Flashback queries appeared in Oracle 9i first.<br />
This is well known by the DBA’s and developers.<br />
Maybe not so popular that we can execute flashback queries against the Data Dictionary.<br />
Average users (ie. Not SYS) can not do it without necessary object privileges.<br />
Note the <strong>FLASHBACK ANY TABLE</strong>  system privilege is not enough for these queries!</p>
<p>Let&#8217;s see an example:<br />
1. HR user created earlier a function, this was the content of the tax.sql:<br />
<strong>CREATE OR REPLACE FUNCTION TAX(P_AMOUNT IN NUMBER)</strong><br />
<strong> RETURN NUMBER</strong><br />
<strong> IS</strong><br />
<strong> M  NUMBER;</strong><br />
<strong> BEGIN</strong><br />
<strong> IF P_AMOUNT &lt;8000 THEN M:=0.08;</strong><br />
<strong> ELSIF P_AMOUNT &lt;18000 THEN M:=0.25;</strong><br />
<strong> ELSE  M:=0.3;</strong><br />
<strong> END IF;</strong><br />
<strong> RETURN P_AMOUNT*M;</strong><br />
<strong> END;</strong><br />
<strong> /</strong></p>
<p>Although HR has the original version of the function, but he modified his function in the database many times  and he did not track these modifications in the text file (in our example tax.sql).<br />
HR user deleted his function accidentally:</p>
<p><strong>DROP FUNCTION TAX;</strong></p>
<p>A couple minutes later he realized that it was a mistake and his version of the function in tax.sql is too old! He asked the DBA to help him.<br />
The DBA connected to the database instance as SYS and issued the following GRANT statement:</p>
<p><strong>GRANT FLASHBACK ON USER_SOURCE TO HR;</strong></p>
<p>The DBA wanted to be proactive and  helpful, so he or she  issued the second  SQL statement:</p>
<p><strong>GRANT FLASHBACK ON USER_VIEWS TO HR;</strong></p>
<p>Now HR will be able to issue a flashback query against the subset (only for USER_SOURCE and USER_VIEWS)  of the Data Dictionary:</p>
<p><strong>SELECT TEXT FROM USER_SOURCE AS OF TIMESTAMP SYSTIMESTAMP- INTERVAL &#8216;&amp;MIN&#8217; MINUTE</strong><br />
<strong> WHERE NAME=&#8217;TAX&#8217;;</strong></p>
<p><strong>TEXT</strong><br />
<strong> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</strong><br />
<strong> FUNCTION TAX(P_AMOUNT IN NUMBER)</strong><br />
<strong> RETURN NUMBER</strong><br />
<strong> IS</strong><br />
<strong> M  NUMBER;</strong><br />
<strong> BEGIN</strong><br />
<strong> IF P_AMOUNT &lt;8000 THEN M:=0.08;</strong><br />
<strong> ELSIF P_AMOUNT &lt;18000 THEN M:=0.25;</strong><br />
<strong> ELSE  M:=0.3;</strong><br />
<strong> END IF;</strong><br />
<strong> RETURN P_AMOUNT*M;</strong><br />
<strong> END;</strong></p>
<p><strong>11 rows selected.</strong></p>
<p>If HR redirects the output of the above query in a spool file, he can recreate  the latest version of his function.<br />
Of course HR can issue the same select against the USER_VIEWS.</p>
<p>This method useful If:</p>
<ol start="1">
<li>The Oracle object is a text type object (subprograms, views, directories, context objects, …)</li>
<li>The DROP statement was not “too old”. This means, if there was a big traffic in the database, many transactions used the undo segment in latest couple of hours or even minutes)</li>
<li>The DBA issued similar statements to this:<br />
<strong>ALTER SYSTEM SET UNDO_RETENTION = 3600;</strong><br />
<strong> ALTER TABLESPACE UNDOTBS1 RETENTION GUARANTEE ;</strong></li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/459/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/459/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=459&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2011/08/17/flashback-queries-for-data-dictionary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>
	</item>
		<item>
		<title>Analyzing Oracle Data Warehouse with Analytic Functions II.</title>
		<link>http://sqlplsql.wordpress.com/2011/04/29/analyzing-oracle-data-warehouse-with-analytic-functions-ii/</link>
		<comments>http://sqlplsql.wordpress.com/2011/04/29/analyzing-oracle-data-warehouse-with-analytic-functions-ii/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 08:29:58 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=402</guid>
		<description><![CDATA[Window and Report Aggregate Family Window aggregate functions operate on an ordered set of rows and for each row they return an aggregate value for a window rooted at that row. The window can be unbounded, partially unbounded or bounded. The size of the window can be specified by physically (eg. With number of rows, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=402&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="center"><strong>Window and Report Aggregate Family</strong></p>
<p>Window aggregate functions operate on an ordered set of rows and for each row they return an aggregate value for a window rooted at that row.<br />
The window can be unbounded, partially unbounded or bounded. The size of the window can be specified by physically (eg. With number of rows, ROWS) or logically (typically with date/time values, RANGE).</p>
<p>There are several keywords available:</p>
<p><strong>CURRENT ROW</strong> &#8211; specifies that window starts or ends at the current row</p>
<p><strong>UNBOUNDED PRECEDING</strong> &#8211; specifies that the window starts at the first row of the partition.</p>
<p><strong>UNBOUNDED FOLLOWING</strong> &#8211; specifies that the window ends at the last row of the partition.<br />
If we don’t specify explicitly the one of the boundaries the <strong>CURRENT ROW</strong> is the default.</p>
<p>Example 1:</p>
<p style="text-align:left;">We want to create a cummulated aggregation (sometimes called running aggregates) for the salaries for the whole table and for each department:<br />
<code><br />
<strong>SELECT department_id,employee_id, last_name, salary, SUM(salary) OVER (ORDER BY salary DESC RANGE UNBOUNDED PRECEDING)  AS cum_range,</strong><br />
<strong> SUM(salary) OVER (PARTITION BY department_id ORDER BY salary DESC RANGE UNBOUNDED PRECEDING) AS cum_range_for_depts,</strong><br />
<strong> SUM(salary) OVER (ORDER BY salary desc ROWS UNBOUNDED PRECEDING)  cum_rows,</strong><br />
<strong> SUM(salary) OVER (PARTITION BY department_id ORDER BY salary DESC ROWS UNBOUNDED PRECEDING)  cum_rows_for_depts</strong><br />
<strong> FROM employees</strong><br />
<strong> --ORDER BY department_id ;</strong><br />
</code><br />
Example 2:</p>
<p>Now we define an increasing, decreasing and fix sized window for each department:<code><strong></strong></code></p>
<p><code><strong>SELECT department_id, last_name, salary,<br />
SUM(salary) OVER (PARTITION BY department_id ORDER BY salary DESC<br />
ROWS UNBOUNDED PRECEDING)  AS cum_sum,<br />
SUM(salary) OVER (PARTITION BY department_id ORDER BY salary DESC<br />
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)  till_current,<br />
AVG(salary) OVER (PARTITION BY department_id ORDER BY salary DESC<br />
RANGE UNBOUNDED PRECEDING) cum_avg , AVG(salary)<br />
OVER ( PARTITION BY department_id ORDER BY salary DESC RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) avg_sal_rest,<br />
AVG(salary)   OVER (PARTITION BY department_id) AS dept_avg<br />
FROM employees<br />
/</strong></code></p>
<p>Example 3:</p>
<p style="text-align:left;">We want to select to 5 most popular products and running total for each quarter in each year.<code><strong></strong></code></p>
<p style="text-align:left;"><code><strong>SELECT CASE WHEN t.rank=1 THEN t.year END year, t.quarter, t.product, t.amount, t.quantity, t.rsum_for_quarter, t.rank rank_for_amount<br />
FROM(SELECT t.calendar_year year,t.calendar_quarter_desc quarter, p.prod_name product, SUM(s.amount_sold) amount, SUM(SUM(s.amount_sold))<br />
OVER (PARTITION BY t.calendar_year,t.calendar_quarter_desc<br />
ORDER BY SUM(s.amount_sold) DESC RANGE UNBOUNDED PRECEDING) rsum_for_quarter, SUM(s.quantity_sold) quantity,<br />
RANK()  OVER (PARTITION BY t.calendar_year , t.calendar_quarter_desc<br />
ORDER BY SUM(s.amount_sold) DESC ) rank<br />
FROM sh.sales s, sh.times t, sh.products p<br />
WHERE s.time_id=t.time_id AND s.prod_id=p.prod_id<br />
GROUP BY t.calendar_year,t.calendar_quarter_desc,p.prod_name) t<br />
WHERE t.rank&lt;5;</strong></code><br />
In the SUM(SUM(s.amount_sold)) expression the inner SUM is standard group function, which is the argument of the outer <strong>SUM</strong> that is an analytical function.<strong></strong></p>
<p>Example 4:<br />
Many times we want to see each element&#8217;s contribution (rate) for the total value.<br />
Also called percentages or sharing value.<br />
In the following example we compute the percentages of each  salary of subtotal (department level) of grand total:</p>
<p style="text-align:left;"><code><strong>SELECT department_id, last_name, salary,<br />
RATIO_TO_REPORT(salary)<br />
OVER (PARTITION BY department_id)  AS rate_for_dept,<br />
RATIO_TO_REPORT(salary) OVER ()   AS rate_for_total<br />
FROM employees;</strong></code></p>
<p>Example 5:<br />
Now we want to compare each employee&#8217;s salary to the lowest anf highest salaries of his/her department using the FIRST_VALUE and LAST_VALUE functions.</p>
<p style="text-align:left;"><code><strong>SELECT t.*,t.salary/t.lowest_sal  rate_for_lowest_sal,<br />
t.salary/t.highest_sal AS rate_for_highest_sal<br />
FROM ( SELECT department_id , last_name, salary,<br />
FIRST_VALUE(salary)<br />
OVER ( PARTITION BY department_id ORDER BY salary<br />
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS lowest_sal,<br />
LAST_VALUE(salary) OVER ( PARTITION BY department_id ORDER BY salary<br />
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED   FOLLOWING) AS highest_sal<br />
FROM employees ORDER BY department_id) t<br />
/</strong></code></p>
<p style="text-align:left;">Example 6:<br />
Many times we need to compute moving values(MAX,SUM,AVG). In this example we calculate moving sum for better understanding:<code><strong></strong></code></p>
<p style="text-align:left;"><code><strong>SELECT last_name, hire_date, salary,<br />
SUM(salary)  OVER (ORDER BY hire_date RANGE BETWEEN NUMTOYMINTERVAL(3,'MONTH') PRECEDING AND NUMTOYMINTERVAL(3,'MONTH') FOLLOWING) AS mov_sum_3_months,<br />
SUM(salary)  OVER (ORDER BY hire_date ROWS BETWEEN 3 PRECEDING AND 3 FOLLOWING) AS mov_sum_3_rows<br />
FROM employees;</strong></code></p>
<p style="text-align:left;">Observe the difference between<strong> RANGE</strong> and <strong>ROWS</strong>.<br />
With RANGE we specified a six months window, while with ROWS we created a 7 row width window.<strong></strong></p>
<p style="text-align:left;"><strong><br />
</strong>To be continued&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/402/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=402&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2011/04/29/analyzing-oracle-data-warehouse-with-analytic-functions-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>
	</item>
		<item>
		<title>Efficient analysis with IN operator</title>
		<link>http://sqlplsql.wordpress.com/2011/04/28/efficient-analysis-with-in-operator-2/</link>
		<comments>http://sqlplsql.wordpress.com/2011/04/28/efficient-analysis-with-in-operator-2/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 09:22:06 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=197</guid>
		<description><![CDATA[SQL developers frequently hear and realize that the subqueries are not efficient, avoid using them. Indeed, many times those SELECT statements, which contains subqueries in the WHERE or HAVING clause are not very efficient. But this not a general rule! I'd like to demonstrate that subqueries with IN operator and correlated subqueries can be very [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=197&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre>SQL developers frequently hear and realize that the subqueries are not efficient,
avoid using them.
Indeed, many times those SELECT statements, which contains subqueries
in the WHERE or HAVING clause are not very efficient.

But this not a general rule!

I'd like to demonstrate that subqueries with IN operator and
correlated subqueries can be very efficient.

Let see the next example:

SELECT last_name, department_id, job_id FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1700);
Here is the execution plan for the SELECT statement:

SQL_ID  gm32t8f65882q, child number 0
-------------------------------------
SELECT last_name, department_id, job_id FROM   employees WHERE
department_id IN (SELECT department_id FROM departments WHERE  location_id = 1700)
Plan hash value: 1021246405
--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |       |       |     6 (100)|          |
|   1 |  NESTED LOOPS                |                   |       |       |            |          |
|   2 |   NESTED LOOPS               |                   |    37 |   814 |     6   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS FULL         | DEPARTMENTS       |     4 |    20 |     4   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN          | EMP_DEPARTMENT_IX |    10 |       |     0   (0)|          |
|   5 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES         |    10 |   170 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("LOCATION_ID"=1700)
4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
====================================================================================================
Let us see the second solution and the execution plan!

SELECT last_name, e.department_id, job_id
FROM employees e,departments d
WHERE e.department_id =d.department_id and location_id = 1700;

SQL_ID  gq10rm3ypv7v2, child number 0
-------------------------------------
SELECT last_name, e.department_id, job_id FROM   employees
e,departments d WHERE  e.department_id =d.department_id and
location_id = 1700
Plan hash value: 1021246405
--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |       |       |     6 (100)|          |
|   1 |  NESTED LOOPS                |                   |       |       |            |          |
|   2 |   NESTED LOOPS               |                   |    37 |   814 |     6   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS FULL         | DEPARTMENTS       |     4 |    20 |     4   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN          | EMP_DEPARTMENT_IX |    10 |       |     0   (0)|          |
|   5 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES         |    10 |   170 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("LOCATION_ID"=1700)
4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
====================================================================================================

We can realize that both quires use the same execution plan (The plan Hash values are same!)
so they are same efficient!</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/197/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=197&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2011/04/28/efficient-analysis-with-in-operator-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>
	</item>
		<item>
		<title>Analyzing Oracle Data Warehouse with Analytic Functions I.</title>
		<link>http://sqlplsql.wordpress.com/2011/04/28/355/</link>
		<comments>http://sqlplsql.wordpress.com/2011/04/28/355/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 09:15:07 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=355</guid>
		<description><![CDATA[Introduction. The Rank family   The traditional SQL statements and functions do not provide strong support for analytic tasks. Basic business intelligence calculations such as moving averages, rankings and lead/lag comparisons have required extensive programming outside of standard SQL , often with performance challenges. From Oracle 8.1.6 Oracle provides powerful new family of functions which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=355&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="center"><strong>Introduction. The Rank family<br />
</strong></p>
<p align="center"><strong> </strong></p>
<p>The traditional SQL statements and functions do not provide strong support for analytic tasks<strong>. </strong>Basic business intelligence calculations such as moving averages, rankings and lead/lag comparisons have required extensive programming outside of standard SQL , often with performance challenges.</p>
<p>From Oracle 8.1.6 Oracle provides powerful new family of functions which are useful for analyzing data warehouses or simple big data sets.</p>
<p>Oracle has created four families of analytic functions</p>
<ul>
<li>Ranking family: RANK, DENSE_RANK, PERCENT_RANK, CUME_DIST</li>
<li>Window Aggregate family: AVG, SUM, MIN, MAX, COUNT, VARIANCE and STDDEV (typically with fix sized window)</li>
<li>Reporting Aggregate family: AVG, SUM, MIN, MAX, COUNT, VARIANCE and STDDEV , RATIO_TO_REPORT (typically with varying sized window)</li>
<li>LAG/LEAD family: LAG  and LEAD</li>
</ul>
<p>It is important to understand the processing order of the functionalyties of the analytic SQL statements</p>
<ul>
<li>The joins will be executed</li>
<li>Evaluating the WHERE, GROUP BY, HAVING clauses</li>
<li>Partitions created</li>
<li>Analytic functions applied to each row in each partition</li>
<li>ORDER BY is processed to allow for precise output ordering.</li>
</ul>
<p>In the following examples we will use the the sample schemas (HR,SH) which are available in all Oracle database.</p>
<p>Ranking family:</p>
<p>Example 1:</p>
<p>Let’s suppose we want rank the salaries in the employees table.</p>
<p>Issuing the following SELECT</p>
<p><strong>SELECT ROWNUM,last_name,salary from<br />
(SELECT * FROM EMPLOYEES ORDER BY salary DESC) t<br />
WHERE ROWNUM&lt;=&amp;counter;</strong></p>
<p>for counter=2  :</p>
<p>1 KING               24000</p>
<p>2 KOCHHAR      17000</p>
<p>for counter=3  :</p>
<p><strong> </strong>1 KING               24000</p>
<p>2 KOCHHAR      17000</p>
<p>3 DE HAAN        17000</p>
<p>Which are the ranks for Kochhar and De Haan?<br />
Solution:</p>
<p><strong>SELECT last_name,salary, </strong><br />
<strong>RANK() OVER( ORDER BY salary DESC) normal_rank, </strong><br />
<strong>DENSE_RANK() OVER( ORDER BY salary desc) dense_rank </strong><br />
<strong>FROM employees;</strong></p>
<p>Example 2:<br />
Let’s see the top 5 best products regarding the revenue for each year:</p>
<p><strong>SELECT t.*<br />
FROM (select t.calendar_year year , p.prod_name product, SUM(s.amount_sold), SUM(s.quantity_sold),<br />
RANK()  OVER (PARTITION BY t.calendar_year ORDER BY SUM(s.amount_sold) DESC ) rank<br />
FROM sh.sales s, sh.times t ,sh.products p<br />
WHERE s.time_id=t.time_id AND s.prod_id=p.prod_id<br />
GROUP BY t.calendar_year,p.prod_name) t<br />
WHERE t.rank&lt;=5;</strong></p>
<p>Example 3:</p>
<p>We can retrieve the 4 best products regarding the revenue for each product category:</p>
<p><strong>SELECT t.*<br />
FROM (select p.prod_category,t.calendar_quarter_desc quarter  , ROUND(SUM(s.amount_sold)) amount ,SUM(s.quantity_sold) quantity,<br />
RANK()  OVER (PARTITION BY P.prod_category<br />
ORDER BY SUM(s.amount_sold) DESC ) rank<br />
FROM sh.sales s, sh.times t ,sh.products p<br />
WHERE s.time_id=t.time_id AND s.prod_id=P.prod_id<br />
GROUP BY p.prod_category,calendar_quarter_desc ) t<br />
WHERE t.rank&lt;=4<br />
/</strong><strong></strong></p>
<p>Example 4:<br />
Now we consider the cumulative distribution of the salaries for each departments:</p>
<p><strong>SELECT department_id, last_name, last_name, salary,<br />
CUME_DIST() OVER(PARTITION BY department_id ORDER BY  Salary)<br />
FROM employees;</strong></p>
<p>Example 5:<br />
We create ranks and subtotals  for amounts in  each year:<strong><br />
</strong></p>
<p><strong>SELECT t.*<br />
FROM (select t.calendar_year year , p.prod_name  product , SUM(s.amount_sold) amount,SUM(s.quantity_sold),<br />
RANK()  OVER (PARTITION BY t.calendar_year ORDER BY SUM(s.amount_sold) DESC ) -1 rank,GROUPING(p.prod_name) grp_id<br />
FROM sh.sales s, sh.times t ,sh.products p<br />
WHERE s.time_id=t.time_id AND s.prod_id=p.prod_id<br />
GROUP BY ROLLUP ( t.calendar_year,p.prod_name)) t<br />
WHERE t.rank&lt;=5<br />
ORDER BY year,grp_id,amount DESC<br />
/</strong></p>
<p>Example 6:<br />
The ROW_NUMBER function &#8211; which is not equivalent with neither ROWNUM nor RANK &#8211; assigns a unique number, in the sequence defined by the ORDER BY clause, to each row within the partition.<strong><strong></strong></strong></p>
<p><strong>SELECT  ROW_NUMBER() OVER(ORDER BY department_id) row_number , ROWNUM ,</strong><br />
<strong> ROW_NUMBER() OVER( PARTITION BY department_id ORDER BY salary DESC) dept_row_number,</strong><br />
<strong> department_id,last_name,salary,</strong><br />
<strong> RANK() OVER( PARTITION BY department_id ORDER BY salary DESC) normal_rank,</strong><br />
<strong> DENSE_RANK() OVER( PARTITION BY department_id ORDER BY salary DESC) dense_rank</strong><br />
<strong> FROM employees</strong><br />
/<br />
To be continued…</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/355/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=355&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2011/04/28/355/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>
	</item>
		<item>
		<title>Trace an Oracle application client user  in 3-tier environment</title>
		<link>http://sqlplsql.wordpress.com/2011/02/10/trace-an-application-client-user-in-3-tier-environment/</link>
		<comments>http://sqlplsql.wordpress.com/2011/02/10/trace-an-application-client-user-in-3-tier-environment/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 14:23:33 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=312</guid>
		<description><![CDATA[Let us suppose that you want to trace client user which was established in an application environment( for example  Oracle  E-Business-Suite application  in 3-tier  architecture) Tracing  a  client Let’s suppose there are two client or application user (JOEL and LASZLO). They use the same application, but different modules and doing different activities. Via connection pool [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=312&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">Let us suppose that you want to trace client user which was established in an application environment( for example  Oracle  E-Business-Suite application  in 3-tier  architecture)</p>
<p><strong>Tracing  a  client</strong></p>
<p>Let’s suppose there are two client or application user (JOEL and LASZLO).<br />
They use the same application, but different modules and doing different activities.<br />
Via connection pool they use –implicitly – the apps Oracle user account.<br />
LASZLO is responsible for REPORTING using the EDUCATION module.<br />
JOEL is manager working with SME REPORTING in MANAGEMENT MODULE.<br />
LASZLO is working in EDUCATION_DEV modul  using  DEVELOPING activity<br />
JOEL wants to observe LASZLO’s activity tracing LASZLO  as a client user:<br />
We can imitate with DBMS_SESSION and DBMS_APPLICATION_INFO packages:</p>
<p>In SQL*Plus APPS executed the following PL/SQL program:<br />
EXEC DBMS_SESSION.SET_IDENTIFIER(&#8216;LASZLO&#8217;)<br />
EXEC DBMS_APPLICATION_INFO.SET_MODULE(&#8216;EDUCATION_DEV&#8217;,'DEVELOPING&#8217;)</p>
<p>In an other SQL*Plus instance APPS executed the following PL/SQL program:<br />
EXEC DBMS_SESSION.SET_IDENTIFIER(&#8216;LASZLO&#8217;)<br />
EXEC DBMS_APPLICATION_INFO.SET_MODULE(&#8216;EDUCATION&#8217;,'REPORTING&#8217;)</p>
<p>In SQL Devepoler SYS executed the following PL/SQL program:<br />
EXEC DBMS_SESSION.SET_IDENTIFIER(&#8216;JOEL&#8217;)<br />
EXEC DBMS_APPLICATION_INFO.SET_MODULE(&#8216;MANAGEMENT&#8217;,'SME REPORTING&#8217;)</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/client_sid_info.jpg"><img class="alignleft size-full wp-image-329" title="client_sid_info" src="http://sqlplsql.files.wordpress.com/2011/02/client_sid_info.jpg?w=450&#038;h=208" alt="" width="450" height="208" /></a></p>
<p>We want to trace LASZLO’S activity<br />
The trace files will be created under this directory:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/dir2.jpg"><img class="alignleft size-full wp-image-332" title="dir" src="http://sqlplsql.files.wordpress.com/2011/02/dir2.jpg?w=450&#038;h=103" alt="" width="450" height="103" /></a></p>
<p>Now  you can execute (as SYS) the following PL/SQL program:<br />
<strong>EXEC DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE(&#8216;LASZLO&#8217;)</strong><br />
If you want to set the statistics level for that session<br />
create AFTER LOGON trigger, like this:<br />
CREATE OR REPLACE TRIGGER logon_trig<br />
AFTER LOGON  ON  hr.SCHEMA<br />
BEGIN<br />
EXECUTE IMMEDIATE &#8216;ALTER SESSION SET STATISTICS_LEVEL=ALL&#8217;;<br />
END;<br />
/</p>
<p>LASZLO executed  the following query  as DEVELOPING using  apps Oracle user :</p>
<p><strong>SELECT /* LASZLO_DEVELOPING */ last_name,e.salary<br />
FROM hr.employees e<br />
WHERE<br />
E.SALARY&gt;(select avg(d.salary) FROM hr.employees d<br />
WHERE d .department_id=e.department_id);</strong></p>
<p>LASZLO executed  the following query  as REPORTING  using  apps Oracle user :</p>
<p><strong>SELECT /* LASZLO_REPORT */ last_name,e.salary<br />
FROM hr.employees e<br />
WHERE E.SALARY&gt;(select avg(d.salary)<br />
FROM hr.employees d<br />
WHERE  d.department_id=e.department_id);</strong></p>
<p>Now  you can execute (as SYS) the following PL/SQL program:<br />
<strong>EXEC DBMS_MONITOR.CLIENT_ID_TRACE_DISABLE(&#8216;LASZLO&#8217;)<br />
</strong>Under the trace directory you will find the following files: orcl_ora_9508.trc and orcl_ora_1752.trc.</p>
<p>But you don’t have to know them!</p>
<p>It is enough to run the <strong>TRCSESS </strong>utility which collects the necessary information from the trace files and produce  a new one:</p>
<p><strong>trcsess output=laszlo.trc clientid=LASZLO</strong></p>
<p>This file contains very detailed information. If You need a more readable text file, please run the tkprof utility in  command  line<br />
<strong>tkprof LASZLO.trc  LASZLO.txt</strong></p>
<p>Part from laszlo.txt (LASZLO_DEVELOPING)<strong><br />
</strong></p>
<p><strong><a href="http://sqlplsql.files.wordpress.com/2011/02/laszlo_dev.jpg"><img class="alignleft size-full wp-image-338" title="LASZLO_DEV" src="http://sqlplsql.files.wordpress.com/2011/02/laszlo_dev.jpg?w=450&#038;h=182" alt="" width="450" height="182" /></a></strong>Part from laszlo.txt (LASZLO_REPORT)</p>
<p><strong><a href="http://sqlplsql.files.wordpress.com/2011/02/laszlo_report1.jpg"><img class="alignleft size-full wp-image-340" title="LASZLO_REPORT" src="http://sqlplsql.files.wordpress.com/2011/02/laszlo_report1.jpg?w=450&#038;h=171" alt="" width="450" height="171" /></a><br />
</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/312/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=312&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2011/02/10/trace-an-application-client-user-in-3-tier-environment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/client_sid_info.jpg" medium="image">
			<media:title type="html">client_sid_info</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/dir2.jpg" medium="image">
			<media:title type="html">dir</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/laszlo_dev.jpg" medium="image">
			<media:title type="html">LASZLO_DEV</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/laszlo_report1.jpg" medium="image">
			<media:title type="html">LASZLO_REPORT</media:title>
		</media:content>
	</item>
		<item>
		<title>Trace an arbitrary Oracle application session in client/server architecture</title>
		<link>http://sqlplsql.wordpress.com/2011/02/10/trace-an-arbitrary-oracle-application-session-in-clientserver-architecture/</link>
		<comments>http://sqlplsql.wordpress.com/2011/02/10/trace-an-arbitrary-oracle-application-session-in-clientserver-architecture/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 11:01:27 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=263</guid>
		<description><![CDATA[Let us suppose that you want to trace a session which was established in client/server architecture (Dedicated Server architecture) Tracing  a  session This method is applicable, if the session was created by an c/s application. Very easy to identify which is the target  session: Let us suppose that  the session is identified by SID=420 and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=263&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">Let us suppose that you want to trace a session which was established in client/server architecture (Dedicated Server architecture)</p>
<p><strong>Tracing  a  session</strong></p>
<p style="text-align:left;">This method is applicable, if the session was created by an c/s application.<br />
Very easy to identify which is the target  session:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/sid_sqldev.jpg"><img class="alignleft size-full wp-image-267" title="sid_sqldev" src="http://sqlplsql.files.wordpress.com/2011/02/sid_sqldev.jpg?w=450&#038;h=197" alt="" width="450" height="197" /></a></p>
<p style="text-align:left;">Let us suppose that  the session is identified by<br />
SID=420 and SERIAL#=2103.<br />
The HR user’s  Operating system process identifier (SPID) is 6684.<br />
You can find the directory of the trace file which will be created later:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/dir_sqldev2.jpg"><img class="alignleft size-full wp-image-277" title="dir_sqldev" src="http://sqlplsql.files.wordpress.com/2011/02/dir_sqldev2.jpg?w=450&#038;h=174" alt="" width="450" height="174" /></a></p>
<p style="text-align:left;">Now  you can execute (as SYS) the following PL/SQL program:<br />
<strong>EXEC DBMS_MONITOR.session_trace_enable(420,2103,TRUE,TRUE,&#8217;ALL_EXECUTIONS&#8217;)</strong></p>
<p>3. argument (waits)  TRUE, wait information is present in the trace<br />
4. argument (binds)  TRUE, bind information is present in the trace<br />
If you want to set the statistics level for that session<br />
create an AFTER LOGON trigger, like this:<br />
<strong>CREATE OR REPLACE TRIGGER logon_trig</strong><br />
<strong> AFTER LOGON  ON  hr.SCHEMA</strong><br />
<strong> BEGIN</strong><br />
<strong> EXECUTE IMMEDIATE<br />
&#8216;ALTER SESSION SET STATISTICS_LEVEL=ALL&#8217;;</strong><br />
<strong> END;</strong><br />
<strong> /</strong></p>
<p>Connect and execute the following query as HR :</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/query1.jpg"><img class="alignleft size-full wp-image-279" title="query" src="http://sqlplsql.files.wordpress.com/2011/02/query1.jpg?w=450&#038;h=107" alt="" width="450" height="107" /></a></p>
<p>You can turn off the tracing for the same session:</p>
<p><strong>EXEC DBMS_MONITOR.session_disable(420,2103)</strong></p>
<p style="text-align:left;">Under the trace directory you will find the following file: orcl_ora_6684.trc where 6684 is HR’s SPID.<br />
This file contains very detailed information.<br />
If You need a more readable text file, please run the tkprof utility in  command  line<br />
<strong>tkprof orcl_ora_6684.trc hr_sqlplus2.txt</strong><br />
Detail from the file:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/tkprof2.jpg"><img class="alignleft size-full wp-image-285" title="tkprof2" src="http://sqlplsql.files.wordpress.com/2011/02/tkprof2.jpg?w=450&#038;h=238" alt="" width="450" height="238" /></a></p>
<p style="text-align:left;">Execute the sqlid stored procedure<br />
(which can be found in &#8220;Trace your Oracle session&#8221; blog)<br />
as SYS:</p>
<p style="text-align:left;">EXEC hr.sqlid(&#8216;MAX(SUM(salary))&#8217;)</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/sqldev_sqlid_report1.jpg"><img class="alignleft size-full wp-image-286" title="sqldev_sqlid_report" src="http://sqlplsql.files.wordpress.com/2011/02/sqldev_sqlid_report1.jpg?w=450&#038;h=508" alt="" width="450" height="508" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/263/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/263/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/263/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=263&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2011/02/10/trace-an-arbitrary-oracle-application-session-in-clientserver-architecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/sid_sqldev.jpg" medium="image">
			<media:title type="html">sid_sqldev</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/dir_sqldev2.jpg" medium="image">
			<media:title type="html">dir_sqldev</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/query1.jpg" medium="image">
			<media:title type="html">query</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/tkprof2.jpg" medium="image">
			<media:title type="html">tkprof2</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/sqldev_sqlid_report1.jpg" medium="image">
			<media:title type="html">sqldev_sqlid_report</media:title>
		</media:content>
	</item>
		<item>
		<title>Tracing your Oracle session</title>
		<link>http://sqlplsql.wordpress.com/2011/02/10/tracing-oracle-sessions/</link>
		<comments>http://sqlplsql.wordpress.com/2011/02/10/tracing-oracle-sessions/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 09:20:47 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=218</guid>
		<description><![CDATA[How to trace Oracle session using SQL and PL/SQL tools? Many times developers and/or DBA’s want to trace their own and/or other session’s  SQL and PL/SQL activities. There are many possibilities to solve this problem especially, if you  want to trace your  session. Very easy to identify which is your session:    Let us suppose [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=218&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre><strong>How to trace Oracle session using SQL and PL/SQL tools?</strong></pre>
<p>Many times developers and/or DBA’s want to trace their own and/or other session’s  SQL and PL/SQL activities.<br />
There are many possibilities to solve this problem especially, if you  want to trace your  session.<br />
Very easy to identify which is your session:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/sid3.jpg"><img class="alignleft size-full wp-image-232" title="sid" src="http://sqlplsql.files.wordpress.com/2011/02/sid3-e1297327136589.jpg?w=450&#038;h=116" alt="" width="450" height="116" /></a></p>
<pre><img src="/DOCUME%7E1/lczinkoc/LOCALS%7E1/Temp/moz-screenshot.png" alt="" /> <strong> </strong><strong> </strong></pre>
<p>Let us suppose that  your session is identified by SID=403 and SERIAL#=1242.<br />
Your Operating system process identifier (SPID) is 8564<br />
You can find the directory of the trace file which will be created later:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/dir1.jpg"><img class="alignleft size-full wp-image-237" title="dir" src="http://sqlplsql.files.wordpress.com/2011/02/dir1.jpg?w=450&#038;h=103" alt="" width="450" height="103" /></a></p>
<p>Modify your session:<strong><br />
ALTER SESSION SET SQL_TRACE=TRUE; </strong><br />
<strong>ALTER SESSION SET  STATISTICS_LEVEL=ALL;</strong></p>
<p>Execute the following query:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/query.jpg"><img class="alignleft size-full wp-image-241" title="query" src="http://sqlplsql.files.wordpress.com/2011/02/query.jpg?w=450&#038;h=107" alt="" width="450" height="107" /></a></p>
<p>Under the trace directory You will find the following file: orcl_ora_8564.trc where 8564 is your SPID.<br />
This file contains very detailed information.<br />
If You need a more readable text file, please run the tkprof utility in  command  line<br />
<strong>tkprof orcl_ora_8564.trc hr_sqlplus.txt</strong></p>
<p>Detail from the text file:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/tkprof.jpg"><img class="alignleft size-full wp-image-247" title="tkprof" src="http://sqlplsql.files.wordpress.com/2011/02/tkprof.jpg?w=450&#038;h=210" alt="" width="450" height="210" /></a></p>
<p>Executing  a suitable PL/SQL stored procedure, called  SQLID:</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/sqlid.jpg"><img class="alignleft size-full wp-image-250" title="SQLID" src="http://sqlplsql.files.wordpress.com/2011/02/sqlid.jpg?w=450&#038;h=308" alt="" width="450" height="308" /></a></p>
<p>You want to find your SELECT statement&#8217;s execution plan with plan statistics (due to statistics level is ALL!)<br />
EXEC SQLID(&#8216;MAX(SUM(salary))&#8217;)</p>
<p><a href="http://sqlplsql.files.wordpress.com/2011/02/sqlid_report.jpg"><img class="alignleft size-full wp-image-251" title="SQLID_REPORT" src="http://sqlplsql.files.wordpress.com/2011/02/sqlid_report.jpg?w=450&#038;h=253" alt="" width="450" height="253" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/218/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/218/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/218/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=218&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2011/02/10/tracing-oracle-sessions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/sid3-e1297327136589.jpg" medium="image">
			<media:title type="html">sid</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/dir1.jpg" medium="image">
			<media:title type="html">dir</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/query.jpg" medium="image">
			<media:title type="html">query</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/tkprof.jpg" medium="image">
			<media:title type="html">tkprof</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/sqlid.jpg" medium="image">
			<media:title type="html">SQLID</media:title>
		</media:content>

		<media:content url="http://sqlplsql.files.wordpress.com/2011/02/sqlid_report.jpg" medium="image">
			<media:title type="html">SQLID_REPORT</media:title>
		</media:content>
	</item>
		<item>
		<title>Efficient Analysis with correlated subquires</title>
		<link>http://sqlplsql.wordpress.com/2010/03/30/efficient-analysis-with-correlated-subquires/</link>
		<comments>http://sqlplsql.wordpress.com/2010/03/30/efficient-analysis-with-correlated-subquires/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 16:14:17 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=206</guid>
		<description><![CDATA[Many developers think that correlated subquires are not efficient. The correct answer starts with "It depends on...". Sometimes we can achieve the same efficient perfomance what we can do with inline views. Let's see the following example: (Who earns more then his/her departments's average salary?) SELECT  e.last_name, e.department_id, e.salary  FROM employees e WHERE e.salary&#62; (SELECT [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=206&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre>Many developers think that correlated subquires are not efficient.
The correct answer starts with "It depends on...".
Sometimes we can achieve the same efficient perfomance
what we can do with inline views.
Let's see the following example:
(Who earns more then his/her departments's average salary?)

<strong>SELECT  e.last_name, e.department_id, e.salary  FROM employees e WHERE e.salary&gt; (SELECT AVG(salary) FROM employees where department_id=e.department_id );</strong>
<strong></strong>
SQL_ID  3jr7194pfhh3s, child number 0
-------------------------------------
select  e.last_name, e.department_id, e.salary  from employees e where
e.salary&gt; (select avg(salary) from employees where
department_id=e.department_id )
Plan hash value: 2167610409
----------------------------------------------------------------------------------
| Id  | Operation            | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |           |       |       |     8 (100)|          |
|*  1 |  HASH JOIN           |           |     5 |   190 |     8  (25)| 00:00:01 |
|   2 |   VIEW               | VW_SQ_1   |    11 |   286 |     4  (25)| 00:00:01 |
|   3 |    HASH GROUP BY     |           |    11 |    77 |     4  (25)| 00:00:01 |
|   4 |     TABLE ACCESS FULL| EMPLOYEES |   107 |   749 |     3   (0)| 00:00:01 |
|   5 |   TABLE ACCESS FULL  | EMPLOYEES |   107 |  1284 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ITEM_1"="E"."DEPARTMENT_ID")
filter("E"."SALARY"&gt;"AVG(SALARY)")
====================================================================================================
We can try with inline view:

<strong>SELECT e.last_name, e.department_id, e.salary ,d.average FROM employees e, (SELECT department_id,AVG(salary) average FROM employeesGROUP BY department_id ) d WHERE e.department_id=d.department_id AND e.salary&gt;d.average;</strong>
<strong></strong>
SQL_ID  b77bmkd7rn84h, child number 0
-------------------------------------
select e.last_name, e.department_id, e.salary ,d.average from employees
e,(select department_id,avg(salary) average from employees group by
department_id ) d where e.department_id=d.department_id and
e.salary&gt;d.average
Plan hash value: 2695105989
----------------------------------------------------------------------------------
| Id  | Operation            | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |           |       |       |     8 (100)|          |
|*  1 |  HASH JOIN           |           |     5 |   190 |     8  (25)| 00:00:01 |
|   2 |   VIEW               |           |    11 |   286 |     4  (25)| 00:00:01 |
|   3 |    HASH GROUP BY     |           |    11 |    55 |     4  (25)| 00:00:01 |
|   4 |     TABLE ACCESS FULL| EMPLOYEES |   107 |   535 |     3   (0)| 00:00:01 |
|   5 |   TABLE ACCESS FULL  | EMPLOYEES |   107 |  1284 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
filter("E"."SALARY"&gt;"D"."AVERAGE")
====================================================================================================
As we can see the result and execution plans are identical!
Just the name of the temporary views are different.
We can realize that Oracle - sometimes - rewrite our SELECT statement achieving the same result,
but with better performance</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/206/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=206&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2010/03/30/efficient-analysis-with-correlated-subquires/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>
	</item>
		<item>
		<title>Efficient analysis with IN operator</title>
		<link>http://sqlplsql.wordpress.com/2010/03/29/efficient-analysis-with-in-operator/</link>
		<comments>http://sqlplsql.wordpress.com/2010/03/29/efficient-analysis-with-in-operator/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 15:52:49 +0000</pubDate>
		<dc:creator>lczinkoc</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://sqlplsql.wordpress.com/?p=198</guid>
		<description><![CDATA[SQL developers frequently hear and realize that the subqueries are not efficient, avoid using them. Indeed, many times those SELECT statements, which contains subquires in the WHERE or HAVING clause are not very efficient. But this not a general rule! I'd like to demonstrate that subquieres with IN operator and correlated subquires can be very [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=198&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre>
<pre>SQL developers frequently hear and realize that the subqueries
are not efficient, avoid using them.
Indeed, many times those SELECT statements, which contains subquires
in the WHERE or HAVING clause are not very efficient.

But this not a general rule!

I'd like to demonstrate that subquieres with IN operator and
correlated subquires can be very efficient.

Let see the next example:

SELECT last_name, department_id, job_id FROM employees
WHERE department_id IN
(SELECT department_id FROM departments WHERE location_id = 1700);
Here is the execution plan for the SELECT statement:

SQL_ID  gm32t8f65882q, child number 0
-------------------------------------
SELECT last_name, department_id, job_id FROM   employees WHERE
department_id IN (SELECT department_id FROM departments WHERE  location_id = 1700)
Plan hash value: 1021246405
--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |       |       |     6 (100)|          |
|   1 |  NESTED LOOPS                |                   |       |       |            |          |
|   2 |   NESTED LOOPS               |                   |    37 |   814 |     6   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS FULL         | DEPARTMENTS       |     4 |    20 |     4   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN          | EMP_DEPARTMENT_IX |    10 |       |     0   (0)|          |
|   5 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES         |    10 |   170 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("LOCATION_ID"=1700)
4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
====================================================================================================
Let us see the second solution and the execution plan!

SELECT last_name, e.department_id, job_id
FROM employees e,departments d
WHERE e.department_id =d.department_id and location_id = 1700;

SQL_ID  gq10rm3ypv7v2, child number 0
-------------------------------------
SELECT last_name, e.department_id, job_id FROM   employees
e,departments d WHERE  e.department_id =d.department_id and
location_id = 1700
Plan hash value: 1021246405
--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |       |       |     6 (100)|          |
|   1 |  NESTED LOOPS                |                   |       |       |            |          |
|   2 |   NESTED LOOPS               |                   |    37 |   814 |     6   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS FULL         | DEPARTMENTS       |     4 |    20 |     4   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN          | EMP_DEPARTMENT_IX |    10 |       |     0   (0)|          |
|   5 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES         |    10 |   170 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("LOCATION_ID"=1700)
4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
====================================================================================================

We can realize that both quires use the same execution plan (The plan Hash values are same!)
so they are same efficient!</pre>
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlplsql.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlplsql.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlplsql.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlplsql.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlplsql.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlplsql.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlplsql.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlplsql.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlplsql.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlplsql.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlplsql.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlplsql.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlplsql.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlplsql.wordpress.com/198/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlplsql.wordpress.com&amp;blog=6275205&amp;post=198&amp;subd=sqlplsql&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlplsql.wordpress.com/2010/03/29/efficient-analysis-with-in-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c9644e49a7a3480589236f5f86f8a6b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lczinkoc</media:title>
		</media:content>
	</item>
	</channel>
</rss>
