Solved

Improving SQL performance

Posted on 2013-10-27
8
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

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 250 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 13

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 250 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to recover a database from a user managed backup
Suggested Courses

627 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