Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Improving SQL performance

Posted on 2013-10-27
8
Medium Priority
?
395 Views
Last Modified: 2013-10-28
Hello Experts,

I have the SQL as below:

SELECT concat(lpad('   '
                  ,LEVEL * 3 - 3)
             ,e.ename) ename
      ,LEVEL
      ,e.empno
      ,e.mgr
     ,(SELECT  a.ename FROM emp a WHERE a.empno = e.mgr) manager
      ,d.dname
      ,d.deptno
      ,(CASE
               WHEN d.deptno = 10 THEN
                d.dname
               ELSE
                NULL
       END) case_clause
  FROM emp  e
      ,dept d
 WHERE e.deptno = d.deptno
CONNECT BY PRIOR e.empno = e.mgr
 START WITH e.mgr IS NULL

Open in new window


And here is the plan for the above query :

Explain Plan
How to make this query more optimize ?
0
Comment
Question by:Swadhin Ray
8 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39604088
toad isn't the best at giving an execution plan

run this script with F5 in toad (at least version 10.5)

explain plan for
select ... // replace this select with your query
set linesize 2000
set pagesize 999
set trim on
set tab off
select * from table(dms_xplan.display);
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39604616
and provide the explain plan result as text not an image please
0
 
LVL 16

Author Comment

by:Swadhin Ray
ID: 39604784
Here is the output:

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 
Connected as scott

SQL> 
SQL> explain plan for
  2  SELECT concat(lpad('   '
  3                    ,LEVEL * 3 - 3)
  4               ,e.ename) ename
  5        ,LEVEL
  6        ,e.empno
  7        ,e.mgr
  8        ,(SELECT a.ename FROM emp a WHERE a.empno = e.mgr) manager
  9        ,d.dname
 10        ,d.deptno
 11        ,(CASE
 12                 WHEN d.deptno = 10 THEN
 13                  d.dname
 14                 ELSE
 15                  NULL
 16         END) case_clause
 17    FROM emp  e
 18        ,dept d
 19   WHERE e.deptno = d.deptno
 20  CONNECT BY PRIOR e.empno = e.mgr
 21   START WITH e.mgr IS NULL;

Explained

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 2319208209
--------------------------------------------------------------------------------
| Id  | Operation                               | Name    | Rows  | Bytes | Cost
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                        |         |    14 |   770 |
|   1 |  TABLE ACCESS BY INDEX ROWID            | EMP     |     1 |    10 |
|*  2 |   INDEX UNIQUE SCAN                     | PK_EMP  |     1 |       |
|*  3 |  CONNECT BY NO FILTERING WITH START-WITH|         |       |       |
|   4 |   MERGE JOIN                            |         |    14 |   420 |
|   5 |    TABLE ACCESS BY INDEX ROWID          | DEPT    |     4 |    52 |
|   6 |     INDEX FULL SCAN                     | PK_DEPT |     4 |       |
|*  7 |    SORT JOIN                            |         |    14 |   238 |
|   8 |     TABLE ACCESS FULL                   | EMP     |    14 |   238 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("A"."EMPNO"=:B1)
   3 - access("E"."MGR"=PRIOR "E"."EMPNO")
       filter("E"."MGR" IS NULL)
   7 - access("E"."DEPTNO"="D"."DEPTNO")
       filter("E"."DEPTNO"="D"."DEPTNO")

24 rows selected

SQL> 

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39604982
@Geert_Gruwez: that screenshot looks like PL/SQL Developer to me ;-)

@slobaray: Maybe you'd gain some (little) more performance by changing the connect-by to an CTE (using recursive with clause)... -> Cost 7 <-> Cost 4
0
 
LVL 16

Author Comment

by:Swadhin Ray
ID: 39605016
@Alex140181 :

Can you please share the code..
0
 
LVL 35

Accepted Solution

by:
johnsone earned 1000 total points
ID: 39605277
If those row count estimates are correct, then I don't see how you are going to improve the performance much on a query that is only dealing with tables that have 14 rows at a maximum.  If those tables have a lot more rows, I would suggest gathering statistics on them.  The second thing I would try is a FIRST_ROWS hint.
0
 
LVL 14

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 1000 total points
ID: 39605327
There are tons of examples out there regarding the EMP and DEPT tables, this one's from http://technology.amis.nl/2009/09/01/oracle-rdbms-11gr2-goodbye-connect-by-or-the-end-of-hierarchical-querying-as-we-know-it/:

it looks like...

with departments as
( select deptno dept_id
  ,      dname  dept_name
  from   dept
  where  loc in ('NEW YORK' ,'DALLAS')
)
, employees (empno, name, job, mgr, hierlevel) as
( select empno, ename, job, mgr, 1
  from   emp
  where  mgr is null
  union all
  select e.empno, e.ename, e.job
  ,      e.mgr, m.hierlevel + 1
  from   emp e
         join
         employees m
         on (m.empno = e.mgr)
         join
         departments
         on (e.deptno = departments.dept_id)
)
, subordinate_numbers as
( select mgr emp_id
  ,      count(*) number_of_subordinates
  from   employees
  group by mgr
)
select *
from   employees e
       left outer join
       subordinate_numbers s
       on (e.empno = s.emp_id)

Open in new window

0
 
LVL 16

Author Closing Comment

by:Swadhin Ray
ID: 39605447
Thanks..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question