Tune a sql query.

Hello,
database is oracle 10.2.0.4.0
a query is
SELECT * FROM OPTIVA.SFI_AP_BATCH_ING_LTS_SUBLTS_V WHERE Plant = 'GB' and Batch_no = 000361602;
its explain plan is
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Inst |IN-OUT|
| 0 | SELECT STATEMENT | | 1 | 1567 | | 44518 (9)| 00:08:55 | | |
| 1 | NESTED LOOPS | | 1 | 1567 | | 44518 (9)| 00:08:55 | | |
|* 2 | HASH JOIN | | 1 | 1548 | | 44517 (9)| 00:08:55 | | |
| 3 | MERGE JOIN CARTESIAN | | 1 | 1531 | | 44402 (9)| 00:08:53 | | |
|* 4 | HASH JOIN | | 1 | 1488 | | 44179 (9)| 00:08:51 | | |
|* 5 | HASH JOIN | | 1 | 1188 | | 37416 (2)| 00:07:29 | | |
| 6 | REMOTE | | 12000 | 738K| | 1675 (2)| 00:00:21 | OPTIV~ | R->S |
| 7 | MERGE JOIN CARTESIAN | | 2900 | 3186K| | 35740 (2)| 00:07:09 | | |
|* 8 | HASH JOIN | | 1 | 1103 | | 35517 (2)| 00:07:07 | | |
|* 9 | HASH JOIN | | 1 | 627 | | 18611 (2)| 00:03:44 | | |
| 10 | VIEW | SFI_PLM_BATCH_HDR_V | 1 | 140 | | 16906 (2)| 00:03:23 | | |
| 11 | SORT ORDER BY | | 1 | 283 | | 16906 (2)| 00:03:23 | | |
| 12 | SORT UNIQUE | | 1 | 283 | | 16905 (2)| 00:03:23 | | |
|* 13 | HASH JOIN | | 1 | 283 | | 16904 (2)| 00:03:23 | | |
| 14 | MERGE JOIN CARTESIAN | | 38 | 7486 | | 15914 (2)| 00:03:11 | | |
| 15 | REMOTE | GME_BATCH_HEADER | 1 | 162 | | 15531 (2)| 00:03:07 | OPTIV~ | R->S |
| 16 | BUFFER SORT | | 2204 | 77140 | | 383 (2)| 00:00:05 | | |
|* 17 | TABLE ACCESS FULL | FSFORMULA | 2204 | 77140 | | 383 (2)| 00:00:05 | | |
| 18 | REMOTE | FM_FORM_MST | 413K| 33M| | 985 (3)| 00:00:12 | OPTIV~ | R->S |
| 19 | VIEW | SFI_AP_INVENTORY_V_MOD | 1 | 487 | | 1705 (2)| 00:00:21 | | |
| 20 | SORT ORDER BY | | 1 | 411 | | 1705 (2)| 00:00:21 | | |
| 21 | NESTED LOOPS | | 1 | 411 | | 1704 (2)| 00:00:21 | | |
| 22 | NESTED LOOPS OUTER | | 1 | 252 | | 1702 (2)| 00:00:21 | | |
| 23 | NESTED LOOPS | | 1 | 204 | | 1700 (2)| 00:00:21 | | |
| 24 | MERGE JOIN CARTESIAN | | 1 | 152 | | 1695 (2)| 00:00:21 | | |
| 25 | MERGE JOIN CARTESIAN| | 1 | 65 | | 5 (0)| 00:00:01 | | |
| 26 | NESTED LOOPS | | 1 | 57 | | 4 (0)| 00:00:01 | | |
| 27 | REMOTE | ORG_ORGANIZATION_DEFINITIONS | 1 | 22 | | 3 (0)| 00:00:01 | OPTIV~ | R->S |
| 28 | REMOTE | MTL_PARAMETERS | 1 | 35 | | 1 (0)| 00:00:01 | OPTIV~ | R->S |
| 29 | BUFFER SORT | | 1 | 8 | | 4 (0)| 00:00:01 | | |
| 30 | REMOTE | IC_WHSE_MST | 1 | 8 | | 1 (0)| 00:00:01 | OPTIV~ | R->S |
| 31 | BUFFER SORT | | 239 | 20793 | | 1694 (2)| 00:00:21 | | |
| 32 | REMOTE | IC_ITEM_MST | 239 | 20793 | | 1690 (2)| 00:00:21 | OPTIV~ | R->S |
| 33 | REMOTE | IC_LOCT_INV | 1 | 52 | | 5 (0)| 00:00:01 | OPTIV~ | R->S |
| 34 | REMOTE | BULK_ASSAYS | 1 | 48 | | 2 (0)| 00:00:01 | OPTIV~ | R->S |
| 35 | REMOTE | IC_LOTS_MST | 1 | 159 | | 2 (0)| 00:00:01 | OPTIV~ | R->S |
| 36 | VIEW | SFI_PLM_BATCH_HDR_V | 1 | 476 | | 16906 (2)| 00:03:23 | | |
| 37 | SORT ORDER BY | | 1 | 283 | | 16906 (2)| 00:03:23 | | |
| 38 | SORT UNIQUE | | 1 | 283 | | 16905 (2)| 00:03:23 | | |
|* 39 | HASH JOIN | | 1 | 283 | | 16904 (2)| 00:03:23 | | |
| 40 | MERGE JOIN CARTESIAN | | 38 | 7486 | | 15914 (2)| 00:03:11 | | |
| 41 | REMOTE | GME_BATCH_HEADER | 1 | 162 | | 15531 (2)| 00:03:07 | OPTIV~ | R->S |
| 42 | BUFFER SORT | | 2204 | 77140 | | 383 (2)| 00:00:05 | | |
|* 43 | TABLE ACCESS FULL | FSFORMULA | 2204 | 77140 | | 383 (2)| 00:00:05 | | |
| 44 | REMOTE | FM_FORM_MST | 413K| 33M| | 985 (3)| 00:00:12 | OPTIV~ | R->S |
| 45 | BUFFER SORT | | 2900 | 63800 | | 35740 (2)| 00:07:09 | | |
|* 46 | TABLE ACCESS FULL | FSITEM | 2900 | 63800 | | 223 (2)| 00:00:03 | | |
| 47 | VIEW | SFI_AP_FLAT_FORMULA_V | 24429 | 2099K| | 6722 (50)| 00:01:21 | | |
| 48 | SORT GROUP BY | | 24429 | 2457K| 5768K| 6722 (50)| 00:01:21 | | |
|* 49 | HASH JOIN | | 24429 | 2457K| | 6145 (55)| 00:01:14 | | |
|* 50 | TABLE ACCESS FULL | FSFORMULA | 2204 | 154K| | 383 (2)| 00:00:05 | | |
|* 51 | TABLE ACCESS FULL | FSFORMULATECHPARAM | 145K| 4394K| | 5760 (59)| 00:01:10 | | |
| 52 | BUFFER SORT | | 34797 | 1461K| | 44402 (9)| 00:08:53 | | |
|* 53 | TABLE ACCESS FULL | FSITEM | 34797 | 1461K| | 223 (2)| 00:00:03 | | |
|* 54 | TABLE ACCESS FULL | FSFORMULAINGRED | 44332 | 735K| | 114 (4)| 00:00:02 | | |
|* 55 | TABLE ACCESS BY INDEX ROWID | FSFORMULA | 1 | 19 | | 1 (0)| 00:00:01 | | |
|* 56 | INDEX UNIQUE SCAN | FS_FORMULA_PK | 1 | | | 0 (0)| 00:00:01 | | |
it runs about 2 minutes 45 seconds, I use hint (like parallel and DRIVING_SITE), can make it faster as 2 minutes, but can't gain more,
any better idea?
thanks a lot
Edited by: 951932 on Sep 7, 2012 4:12 PM

951932 wrote:
its explain plan is
| Id  | Operation                          | Name                         | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     | Inst   |IN-OUT|
|   0 | SELECT STATEMENT                   |                              |     1 |  1567 |       | 44518   (9)| 00:08:55 |        |      |
|   1 |  NESTED LOOPS                      |                              |     1 |  1567 |       | 44518   (9)| 00:08:55 |        |      |
|*  2 |   HASH JOIN                        |                              |     1 |  1548 |       | 44517   (9)| 00:08:55 |        |      |
|   3 |    MERGE JOIN CARTESIAN            |                              |     1 |  1531 |       | 44402   (9)| 00:08:53 |        |      |
|*  4 |     HASH JOIN                      |                              |     1 |  1488 |       | 44179   (9)| 00:08:51 |        |      |
|*  5 |      HASH JOIN                     |                              |     1 |  1188 |       | 37416   (2)| 00:07:29 |        |      |
|   6 |       REMOTE                       |                              | 12000 |   738K|       |  1675   (2)| 00:00:21 | OPTIV~ | R->S |
|   7 |       MERGE JOIN CARTESIAN         |                              |  2900 |  3186K|       | 35740   (2)| 00:07:09 |        |      |
|*  8 |        HASH JOIN                   |                              |     1 |  1103 |       | 35517   (2)| 00:07:07 |        |      |
|*  9 |         HASH JOIN                  |                              |     1 |   627 |       | 18611   (2)| 00:03:44 |        |      |
|  10 |          VIEW                      | SFI_PLM_BATCH_HDR_V          |     1 |   140 |       | 16906   (2)| 00:03:23 |        |      |
|  11 |           SORT ORDER BY            |                              |     1 |   283 |       | 16906   (2)| 00:03:23 |        |      |
|  12 |            SORT UNIQUE             |                              |     1 |   283 |       | 16905   (2)| 00:03:23 |        |      |
|* 13 |             HASH JOIN              |                              |     1 |   283 |       | 16904   (2)| 00:03:23 |        |      |
|  14 |              MERGE JOIN CARTESIAN  |                              |    38 |  7486 |       | 15914   (2)| 00:03:11 |        |      |
|  15 |               REMOTE               | GME_BATCH_HEADER             |     1 |   162 |       | 15531   (2)| 00:03:07 | OPTIV~ | R->S |
|  16 |               BUFFER SORT          |                              |  2204 | 77140 |       |   383   (2)| 00:00:05 |        |      |
|* 17 |                TABLE ACCESS FULL   | FSFORMULA                    |  2204 | 77140 |       |   383   (2)| 00:00:05 |        |      |
|  18 |              REMOTE                | FM_FORM_MST                  |   413K|    33M|       |   985   (3)| 00:00:12 | OPTIV~ | R->S |
|  19 |          VIEW                      | SFI_AP_INVENTORY_V_MOD       |     1 |   487 |       |  1705   (2)| 00:00:21 |        |      |
|  20 |           SORT ORDER BY            |                              |     1 |   411 |       |  1705   (2)| 00:00:21 |        |      |
|  21 |            NESTED LOOPS            |                              |     1 |   411 |       |  1704   (2)| 00:00:21 |        |      |
|  22 |             NESTED LOOPS OUTER     |                              |     1 |   252 |       |  1702   (2)| 00:00:21 |        |      |
|  23 |              NESTED LOOPS          |                              |     1 |   204 |       |  1700   (2)| 00:00:21 |        |      |
|  24 |               MERGE JOIN CARTESIAN |                              |     1 |   152 |       |  1695   (2)| 00:00:21 |        |      |
|  25 |                MERGE JOIN CARTESIAN|                              |     1 |    65 |       |     5   (0)| 00:00:01 |        |      |
|  26 |                 NESTED LOOPS       |                              |     1 |    57 |       |     4   (0)| 00:00:01 |        |      |
|  27 |                  REMOTE            | ORG_ORGANIZATION_DEFINITIONS |     1 |    22 |       |     3   (0)| 00:00:01 | OPTIV~ | R->S |
|  28 |                  REMOTE            | MTL_PARAMETERS               |     1 |    35 |       |     1   (0)| 00:00:01 | OPTIV~ | R->S |
|  29 |                 BUFFER SORT        |                              |     1 |     8 |       |     4   (0)| 00:00:01 |        |      |
|  30 |                  REMOTE            | IC_WHSE_MST                  |     1 |     8 |       |     1   (0)| 00:00:01 | OPTIV~ | R->S |
|  31 |                BUFFER SORT         |                              |   239 | 20793 |       |  1694   (2)| 00:00:21 |        |      |
|  32 |                 REMOTE             | IC_ITEM_MST                  |   239 | 20793 |       |  1690   (2)| 00:00:21 | OPTIV~ | R->S |
|  33 |               REMOTE               | IC_LOCT_INV                  |     1 |    52 |       |     5   (0)| 00:00:01 | OPTIV~ | R->S |
|  34 |              REMOTE                | BULK_ASSAYS                  |     1 |    48 |       |     2   (0)| 00:00:01 | OPTIV~ | R->S |
|  35 |             REMOTE                 | IC_LOTS_MST                  |     1 |   159 |       |     2   (0)| 00:00:01 | OPTIV~ | R->S |
|  36 |         VIEW                       | SFI_PLM_BATCH_HDR_V          |     1 |   476 |       | 16906   (2)| 00:03:23 |        |      |
|  37 |          SORT ORDER BY             |                              |     1 |   283 |       | 16906   (2)| 00:03:23 |        |      |
|  38 |           SORT UNIQUE              |                              |     1 |   283 |       | 16905   (2)| 00:03:23 |        |      |
|* 39 |            HASH JOIN               |                              |     1 |   283 |       | 16904   (2)| 00:03:23 |        |      |
|  40 |             MERGE JOIN CARTESIAN   |                              |    38 |  7486 |       | 15914   (2)| 00:03:11 |        |      |
|  41 |              REMOTE                | GME_BATCH_HEADER             |     1 |   162 |       | 15531   (2)| 00:03:07 | OPTIV~ | R->S |
|  42 |              BUFFER SORT           |                              |  2204 | 77140 |       |   383   (2)| 00:00:05 |        |      |
|* 43 |               TABLE ACCESS FULL    | FSFORMULA                    |  2204 | 77140 |       |   383   (2)| 00:00:05 |        |      |
|  44 |             REMOTE                 | FM_FORM_MST                  |   413K|    33M|       |   985   (3)| 00:00:12 | OPTIV~ | R->S |
|  45 |        BUFFER SORT                 |                              |  2900 | 63800 |       | 35740   (2)| 00:07:09 |        |      |
|* 46 |         TABLE ACCESS FULL          | FSITEM                       |  2900 | 63800 |       |   223   (2)| 00:00:03 |        |      |
|  47 |      VIEW                          | SFI_AP_FLAT_FORMULA_V        | 24429 |  2099K|       |  6722  (50)| 00:01:21 |        |      |
|  48 |       SORT GROUP BY                |                              | 24429 |  2457K|  5768K|  6722  (50)| 00:01:21 |        |      |
|* 49 |        HASH JOIN                   |                              | 24429 |  2457K|       |  6145  (55)| 00:01:14 |        |      |
|* 50 |         TABLE ACCESS FULL          | FSFORMULA                    |  2204 |   154K|       |   383   (2)| 00:00:05 |        |      |
|* 51 |         TABLE ACCESS FULL          | FSFORMULATECHPARAM           |   145K|  4394K|       |  5760  (59)| 00:01:10 |        |      |
|  52 |     BUFFER SORT                    |                              | 34797 |  1461K|       | 44402   (9)| 00:08:53 |        |      |
|* 53 |      TABLE ACCESS FULL             | FSITEM                       | 34797 |  1461K|       |   223   (2)| 00:00:03 |        |      |
|* 54 |    TABLE ACCESS FULL               | FSFORMULAINGRED              | 44332 |   735K|       |   114   (4)| 00:00:02 |        |      |
|* 55 |   TABLE ACCESS BY INDEX ROWID      | FSFORMULA                    |     1 |    19 |       |     1   (0)| 00:00:01 |        |      |
|* 56 |    INDEX UNIQUE SCAN               | FS_FORMULA_PK                |     1 |       |       |     0   (0)| 00:00:01 |        |      |
it runs about 2 minutes 45 seconds, I use hint (like parallel and DRIVING_SITE), can make it faster as 2 minutes, but can't gain more,
This isn't a complete execution plan - you've omitted the predicate section, and you've omitted the "remote SQL" section. However, even with those bits in place you're probably not going to find anyone who can unravel the plan to figure why you have a problem - even if you supply the definitions of all the views involved.
Given the earlier comments about cartesian merge joins, it's possible that the statistics on the remote database (particularly) are sufficiently stale that the optimizer's estimates are completely wrecked - so check stats.
To get a handle on where the time is going, and where you most need to spend your energy, you could run the query with the /*+ gather_plan_statistics */ hint (or set statistics_level=all) and they use dbms_xplan.display_cursor() with the 'allstats last' format option to show us where the work was done.
Regards
Jonathan Lewis
http://jonathanlewis.wordpress.com
Author: <b><em>Oracle Core</em></b>
To post code, statspack/AWR report, execution plans or trace files, start and end the section with the tag {noformat}{noformat} (lowercase, curly brackets, no spaces) so that the text appears in fixed format.
There is a +"Preview"+ tab at the top of the text entry panel. Use this to check what your message will look like before you post the message. If it looks a complete mess you're unlikely to get a response. (Click on the +"Plain text"+ tab if you want to edit the text to tidy it up.)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • How to tune past SQL query??

    Hi Team,
    Straight to issue --> I am seeing an query running for long time. When i begun to trace that particular query it got over by the time and now how to trace that specific SID and QUERY..
    I am working on 10.2.0.4 version DB..
    How to trace an sql query after its execution?? pls provide steps how to begin with..
    regards
    dkoracle

    dkoracle wrote:
    Hi Team,
    Straight to issue --> I am seeing an query running for long time. When i begun to trace that particular query it got over by the time and now how to trace that specific SID and QUERY..
    I am working on 10.2.0.4 version DB..
    How to trace an sql query after its execution?? pls provide steps how to begin with..Can not be done.
    ALTER SESSION SET SQL_TRACE=TRUE;
    -- run query again

  • Help me tune the sql query

    Hi,
    TKPROF: Release 8.1.7.0.0 - Production on Wed Sep 23 23:04:43 2009
    (c) Copyright 2000 Oracle Corporation.  All rights reserved.
    Trace file: pindb_ora_9943.trc
    Sort options: prsela  exeela  fchela  fchela 
    count    = number of times OCI procedure was executed
    cpu      = cpu time in seconds executing
    elapsed  = elapsed time in seconds executing
    disk     = number of physical reads of buffers from disk
    query    = number of buffers gotten for consistent read
    current  = number of buffers gotten in current mode (usually for update)
    rows     = number of rows processed by the fetch or execute call
    SELECT f.poid_id0, COUNT (*), SUM (amount), d.subscription_amt, d.free_mb
    FROM event_t a,
    event_bal_impacts_t b,
    (SELECT DISTINCT a.obj_id0 AS account_obj_id0, a.node_location,
    a.plan_obj_id0
    FROM account_products_t a
    WHERE plan_obj_id0 > 0 AND service_obj_id0 > 0
    UNION
    SELECT DISTINCT a.account_obj_id0, a.node_location, a.plan_obj_id0
    FROM audit_account_products_t a
    WHERE plan_obj_id0 > 0
    AND service_obj_id0 > 0
    AND effective_end_t > 0) c,
    iq_prov_tags_t d,
    event_session_dialup_t e,
    plan_t f
    WHERE a.poid_id0 = b.obj_id0
    AND a.poid_id0 = e.obj_id0
    AND b.node_location = c.node_location
    AND a.account_obj_id0 = c.account_obj_id0
    AND c.plan_obj_id0 = d.plan_poid_id0
    AND d.plan_poid_id0 = f.poid_id0
    AND a.end_t >= pin_date_to_seconds (TRUNC (  SYSDATE
    - 1), NULL)
    AND a.end_t < pin_date_to_seconds (TRUNC (SYSDATE), NULL)
    AND b.resource_id IN (1000051)
    GROUP BY f.poid_id0, d.subscription_amt, d.free_mb
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.09       0.08          0          0          0           0
    Execute      2      0.07       0.07          0          0          0           0
    Fetch        2    235.91    2295.25    1509704    1674184        346          11
    total        5    236.07    2295.40    1509704    1674184        346          11
    Misses in library cache during parse: 1
    Optimizer goal: CHOOSE
    Parsing user id: 28 
    Rows     Row Source Operation
         11  SORT GROUP BY
         69   NESTED LOOPS
         70    NESTED LOOPS
         70     NESTED LOOPS
         70      HASH JOIN
         70       HASH JOIN
    300499        TABLE ACCESS BY INDEX ROWID EVENT_T
    300500         INDEX RANGE SCAN (object id 26458)
    816394        TABLE ACCESS BY INDEX ROWID EVENT_BAL_IMPACTS_T
    816395         INDEX RANGE SCAN (object id 26463)
    2385556       VIEW
    2385556        SORT UNIQUE
    3124105         UNION-ALL
    638617          TABLE ACCESS FULL ACCOUNT_PRODUCTS_T
    2485488          TABLE ACCESS FULL AUDIT_ACCOUNT_PRODUCTS_T
        138      TABLE ACCESS BY INDEX ROWID IQ_PROV_TAGS_T
        138       INDEX UNIQUE SCAN (object id 26554)
        138     INDEX UNIQUE SCAN (object id 23148)
         69    INDEX UNIQUE SCAN (object id 27372)
    ALTER SESSION SET SQL_TRACE = TRUE
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        0      0.00       0.00          0          0          0           0
    total        1      0.00       0.00          0          0          0           0
    Misses in library cache during parse: 0
    Misses in library cache during execute: 1
    Optimizer goal: CHOOSE
    Parsing user id: 28 
    OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.09       0.08          0          0          0           0
    Execute      3      0.07       0.07          0          0          0           0
    Fetch        2    235.91    2295.25    1509704    1674184        346          11
    total        6    236.07    2295.40    1509704    1674184        346          11
    Misses in library cache during parse: 1
    Misses in library cache during execute: 1
    OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        2      0.01       0.00          0          0          0           0
    Execute      2      0.00       0.00          0          0          0           0
    Fetch       20      0.00       0.01          0         40          0          18
    total       24      0.01       0.01          0         40          0          18
    Misses in library cache during parse: 1
        2  user  SQL statements in session.
        2  internal SQL statements in session.
        4  SQL statements in session.
    Trace file: pindb_ora_9943.trc
    Trace file compatibility: 8.00.04
    Sort options: prsela  exeela  fchela  fchela 
           1  session in tracefile.
           2  user  SQL statements in trace file.
           2  internal SQL statements in trace file.
           4  SQL statements in trace file.
           3  unique SQL statements in trace file.
         118  lines in trace file.Inputs are appreciated
    Prakash GR

    sorry hash joint for asking this type of question but i do not have any option since i am not query tuner i need inputs so that i can put some effort on this
    Would you like a cup of tea with this my dear ?chilled beer will be offered if any inputs given
    Thanks
    Prakash GR

  • TUNNING A SQL QUERY AND UNSERSTAND EXPLAIN PLAN

    I was trying my handing in tunning sql queries -
    Though I have manged to reduce the cost lil ..creating some index, but have genarated a g8 interesting in tunning - Can some experts ( I know there are lots available :-) ) help me on the approch on "HOW TO TUNE A QUERY"
    moreover I also would like to understand - how to debug a explain plan, - please help ...
    Regards..

    Hi,
    Welcome to this forum...
    I would suggest your to read first the official documentations :
    - The concepts of an Oracle DBMS (this is important to know, because Oracle structures, processes, objects (etc) are the building blocks of your database)
    - SQL reference guide
    - PL/SQL reference guide
    and then at a later stage the Performance and tuning guide:
    You can start there:
    http://download.oracle.com/docs/cd/B14117_01/nav/portal_1.htm
    Once it's done maybe read the Performance and tuning guide:
    http://download.oracle.com/docs/cd/B14117_01/server.101/b10752/toc.htm
    (Chapter 19 explain ... how to interpret the explain plan ..)
    I wish you good chance and be patient: I'm working with Oracle for more that 10 years and I'm still learning! ...
    Rem: The Oracle website is full of interesting articles, and examples, just "search" your special points of interests ..

  • How to tune this SQL Query?

    Hi all expert out there,
    I am using Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production With the Partitioning, Real Application Clusters, OLAP, Data Mining and Real Application Testing options.
    I have this ERP 11i query took 3-4hours to execute. can anyone help to have a look at the Query so make it more faster? I am already out of my idea of solving this. Kindly helpzzz...
    SELECT
    /*+ first_rows(1)*/
    hou.name "OU" ,-- e.g. Seagate Technology USOP1 ,
    mc.description,
    oel.ordered_item "Product Part Number",
    oeh.order_number "Order Num",
    hp.party_name "Bill To Cust Name",
    hcsu.location "Ship To Cust Num",
    oel.line_number || '.' || oel.shipment_number "Order Line Num",
    ft.nls_territory "Ship To Country",
    mc.segment7 "designapplication",
    ccm.formattedcapacity||'GB' "Capacity",
    oel.attribute1 "Order Category",
    oel.Subinventory "Subinventory",
    to_char(oel.actual_shipment_date,'MM/DD/YYYY DY') "Shipment Date",
    NVL(oel.shipped_quantity,0) "Net Units"
    FROM
    oe_order_headers_all oeh
    ,oe_order_lines_all oel
    ,apps.hz_parties hp
    ,apps.hz_cust_accounts hca
    ,apps.hz_cust_site_uses_all hcsu
    ,apps.hz_cust_acct_sites_all hcas
    , apps.seaeng_ccfamilymodelinfo ccm
    ,apps.hz_party_sites hps
    ,apps.fnd_territories ft
    ,apps.hz_locations hl
    ,apps.hr_organization_units hou
    ,apps.mtl_parameters mp
    ,apps.mtl_item_categories mic
    ,apps.mtl_categories_b mc
    ,apps.fnd_lookup_values flv
    WHERE
    oeh.header_id = oel.header_id
    AND oel.flow_status_code = 'CLOSED'
    AND oeh.invoice_to_org_id = hcsu.site_use_id
    AND hl.country = ft.territory_code
    AND hps.location_id = hl.location_id
    AND hcas.party_site_id = hps.party_site_id
    AND hcsu.cust_acct_site_id = hcas.cust_acct_site_id
    AND hcas.cust_account_id = hca.cust_account_id
    AND hca.party_id = hp.party_id
    AND oeh.org_id = hou.organization_id
    AND oel.ship_from_org_id = mp.organization_id
    AND oel.inventory_item_id = mic.inventory_item_id
    AND mic.category_id = mc.category_id
    AND mic.category_set_id = 4
    AND mc.description = ccm.stmodelnumber
    AND flv.lookup_code = hca.sales_channel_code
    AND flv.lookup_type = 'SALES_CHANNEL'
    AND mc.segment7 IN ('PSG','ESG','NSG')
    AND flv.lookup_code NOT IN ('RTL','AD-RTL','EU')
    AND oel.attribute1 IN ('NB','NBEOL','NBSEA')
    AND oel.subinventory IN ('KFGI','AFGI','SFGIF','FGIF')
    AND hou.organization_id = 189
    AND trunc(oel.actual_shipment_date) between TO_DATE('01-Jan-2009','DD-MON-YYYY') and TO_DATE('31-JAN-2009','DD-MON-YYYY')
    regards,
    Lygine
    Edited by: user8989062 on Jun 7, 2010 6:39 PM

    5) The TKPROF output for this statement looks like the following:
    SELECT
    /*+ first_rows(1)*/
    hou.name "OU" ,-- e.g. Seagate Technology USOP1 ,
    mc.description,
    oel.ordered_item "Product Part Number",
    oeh.order_number "Order Num",
      hp.party_name  "Bill To Cust Name",
    hcsu.location "Ship To Cust Num",
    oel.line_number || '.' || oel.shipment_number "Order Line Num",
    ft.nls_territory  "Ship To Country",
    mc.segment7 "designapplication",
    ccm.formattedcapacity||'GB' "Capacity",
    oel.attribute1 "Order Category",
    oel.Subinventory "Subinventory",
    to_char(oel.actual_shipment_date,'MM/DD/YYYY DY') "Shipment Date",
    NVL(oel.shipped_quantity,0) "Net Units"
    FROM
          oe_order_headers_all oeh
         ,oe_order_lines_all   oel
         ,apps.hz_parties  hp
         ,apps.hz_cust_accounts  hca
         ,apps.hz_cust_site_uses_all   hcsu
         ,apps.hz_cust_acct_sites_all  hcas
        , apps.seaeng_ccfamilymodelinfo ccm
         ,apps.hz_party_sites hps
         ,apps.fnd_territories ft
         ,apps.hz_locations hl
         ,apps.hr_organization_units  hou
         ,apps.mtl_parameters  mp
         ,apps.mtl_item_categories mic
         ,apps.mtl_categories_b mc
         ,apps.fnd_lookup_values flv
    WHERE
               oeh.header_id = oel.header_id
    AND   oel.flow_status_code = 'CLOSED'
    AND   oeh.invoice_to_org_id = hcsu.site_use_id
    AND   hl.country = ft.territory_code
    AND   hps.location_id = hl.location_id
    AND   hcas.party_site_id = hps.party_site_id
    AND   hcsu.cust_acct_site_id = hcas.cust_acct_site_id
    AND   hcas.cust_account_id = hca.cust_account_id
    AND   hca.party_id   = hp.party_id
    AND   oeh.org_id = hou.organization_id
    AND   oel.ship_from_org_id = mp.organization_id
    AND   oel.inventory_item_id = mic.inventory_item_id
    AND   mic.category_id = mc.category_id
    AND   mic.category_set_id = 4
    AND   mc.description = ccm.stmodelnumber
    AND   flv.lookup_code = hca.sales_channel_code
    AND   flv.lookup_type = 'SALES_CHANNEL'
    AND mc.segment7 IN ('PSG','ESG','NSG')
    AND flv.lookup_code NOT IN ('RTL','AD-RTL','EU')
    AND oel.attribute1 IN ('NB','NBEOL','NBSEA')
    AND oel.subinventory IN ('KFGI','AFGI','SFGIF','FGIF')
    AND hou.organization_id = 189
    AND trunc(oel.actual_shipment_date) between TO_DATE('01-Jan-2009','DD-MON-YYYY') and TO_DATE('31-JAN-2009','DD-MON-YYYY')
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.79       0.79          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        7     22.23     240.85      19781     536082          0         592
    total        9     23.02     241.64      19781     536082          0         592
    Misses in library cache during parse: 1
    Optimizer mode: FIRST_ROWS
    Parsing user id: 173 
    Rows     Row Source Operation
        592  NESTED LOOPS  (cr=536082 pr=19781 pw=0 time=240822989 us)
        592   NESTED LOOPS  (cr=534885 pr=19776 pw=0 time=239960969 us)
        592    NESTED LOOPS  (cr=533688 pr=19776 pw=0 time=239902332 us)
        594     NESTED LOOPS  (cr=532493 pr=19776 pw=0 time=239811721 us)
        594      NESTED LOOPS  (cr=531892 pr=19776 pw=0 time=239794703 us)
        594       NESTED LOOPS  (cr=530693 pr=19772 pw=0 time=239325284 us)
        594        NESTED LOOPS  (cr=529498 pr=19770 pw=0 time=239152940 us)
        594         NESTED LOOPS  (cr=527709 pr=19766 pw=0 time=236850676 us)
        594          NESTED LOOPS  (cr=525920 pr=19764 pw=0 time=235640068 us)
       2990           NESTED LOOPS  (cr=516943 pr=19688 pw=0 time=195144282 us)
       2990            NESTED LOOPS  (cr=516936 pr=19688 pw=0 time=195120297 us)
       2990             NESTED LOOPS  (cr=510806 pr=19682 pw=0 time=194678671 us)
       3000              HASH JOIN  (cr=504799 pr=19669 pw=0 time=193829763 us)
       3000               NESTED LOOPS  (cr=472280 pr=360 pw=0 time=49218087 us)
          1                NESTED LOOPS  (cr=1029 pr=34 pw=0 time=1745829 us)
          1                 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS_TL (cr=424 pr=25 pw=0 time=1361442 us)
          1                  INDEX UNIQUE SCAN HR_ALL_ORGANIZATION_UNTS_TL_PK (cr=423 pr=25 pw=0 time=1361397 us)(object id 44637)
          1                 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS (cr=605 pr=9 pw=0 time=384370 us)
          1                  INDEX UNIQUE SCAN HR_ORGANIZATION_UNITS_PK (cr=1 pr=0 pw=0 time=27 us)(object id 43498)
       3000                TABLE ACCESS BY INDEX ROWID OE_ORDER_LINES_ALL (cr=471251 pr=326 pw=0 time=47466249 us)
    156922                 INDEX RANGE SCAN OE_ORDER_LINES_N20 (cr=420 pr=7 pw=0 time=787919 us)(object id 37005064)
    263469               TABLE ACCESS FULL MTL_ITEM_CATEGORIES (cr=32519 pr=19309 pw=0 time=144242743 us)
       2990              TABLE ACCESS BY INDEX ROWID MTL_CATEGORIES_B (cr=6007 pr=13 pw=0 time=843569 us)
       3000               INDEX UNIQUE SCAN MTL_CATEGORIES_B_U1 (cr=3007 pr=2 pw=0 time=162636 us)(object id 37199)
       2990             TABLE ACCESS BY INDEX ROWID SEAENG_CCFAMILYMODELINFO (cr=6130 pr=6 pw=0 time=437784 us)
       2990              INDEX UNIQUE SCAN SEAENG_CCFAMILYMODELINFO_U1 (cr=2997 pr=0 pw=0 time=52872 us)(object id 35838918)
       2990            INDEX UNIQUE SCAN MTL_PARAMETERS_U1 (cr=7 pr=0 pw=0 time=16332 us)(object id 37657)
        594           TABLE ACCESS BY INDEX ROWID OE_ORDER_HEADERS_ALL (cr=8977 pr=76 pw=0 time=40491633 us)
       2990            INDEX UNIQUE SCAN OE_ORDER_HEADERS_U1 (cr=5987 pr=45 pw=0 time=2504442 us)(object id 41952)
        594          TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=1789 pr=2 pw=0 time=1208576 us)
        594           INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=1195 pr=1 pw=0 time=105934 us)(object id 25124976)
        594         TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCT_SITES_ALL (cr=1789 pr=4 pw=0 time=2300224 us)
        594          INDEX UNIQUE SCAN HZ_CUST_ACCT_SITES_U1 (cr=1195 pr=2 pw=0 time=152238 us)(object id 25124996)
        594        TABLE ACCESS BY INDEX ROWID HZ_PARTY_SITES (cr=1195 pr=2 pw=0 time=170875 us)
        594         INDEX UNIQUE SCAN HZ_PARTY_SITES_U1 (cr=601 pr=1 pw=0 time=104550 us)(object id 25124993)
        594       TABLE ACCESS BY INDEX ROWID HZ_LOCATIONS (cr=1199 pr=4 pw=0 time=467329 us)
        594        INDEX UNIQUE SCAN HZ_LOCATIONS_U1 (cr=601 pr=0 pw=0 time=57749 us)(object id 25124992)
        594      TABLE ACCESS BY INDEX ROWID FND_TERRITORIES (cr=601 pr=0 pw=0 time=15562 us)
        594       INDEX UNIQUE SCAN FND_TERRITORIES_U1 (cr=7 pr=0 pw=0 time=6492 us)(object id 33083)
        592     TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=1195 pr=0 pw=0 time=88925 us)
        594      INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=601 pr=0 pw=0 time=27607 us)(object id 81600)
        592    INDEX RANGE SCAN FND_LOOKUP_VALUES_U1 (cr=1197 pr=0 pw=0 time=56714 us)(object id 32878)
        592   TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=1197 pr=5 pw=0 time=860140 us)
        592    INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=599 pr=0 pw=0 time=51136 us)(object id 25126074)
    Elapsed times include waiting on following events:
      Event waited on                             Times   Max. Wait  Total Waited
      ----------------------------------------   Waited  ----------  ------------
      SQL*Net message to client                       7        0.00          0.00
      row cache lock                                  1        0.00          0.00
      gc current block 2-way                      24112        0.00         19.28
      gc cr grant 2-way                             406        0.00          0.19
      db file sequential read                       441        0.09         14.11
      gc cr block 2-way                             284        0.00          0.23
      gc cr block busy                              147        0.19          9.59
      gc current block congested                      2        0.00          0.00
      gc cr multi block request                   13079        0.00          3.98
      SQL*Net message from client                     7        0.27          1.87
      gc cr failure                                  39        0.00          0.02
      cr request retry                               39        0.98         38.27
      SQL*Net more data to client                    12        0.00          0.00
      db file scattered read                       2446        0.13        132.58
      gc cr disk read                                 5        0.00          0.00
    ********************************************************************************6) The DBMS_XPLAN.DISPLAY_CURSOR output:
    SQL> select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
    PLAN_TABLE_OUTPUT
    SQL_ID  b8pfhfxscqn1m, child number 0
    SELECT  /*+ gather_plan_statistics */  hou.name "OU" ,-- e.g. Seagate Technology USOP1 ,  mc.descript
    oeh.order_number "Order Num",   hp.party_name  "Bill To Cust Name",  hcsu.location "Ship To Cust Num",
    Num",  ft.nls_territory  "Ship To Country",  mc.segment7 "designapplication",  ccm.formattedcapacity
    oel.Subinventory "Subinventory",  to_char(oel.actual_shipment_date,'MM/DD/YYYY DY') "Shipment Date",
    oe_order_headers_all oeh      ,oe_order_lines_all   oel      ,apps.hz_parties  hp      ,apps.hz_cust
    ,apps.hz_cust_acct_sites_all  hcas     , apps.seaeng_ccfamilymodelinfo ccm      ,apps.hz_party_sites hps
        ,apps.hr_organization_units  hou      ,apps.mtl_paramete
    Plan hash value: 414863479
    | Id  | Operation                                  | Name                           | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  O
    |   1 |  NESTED LOOPS                              |                                |      1 |      1 |    592 |00:02:25.13 |     535K|  57267 |       |
    |   2 |   NESTED LOOPS                             |                                |      1 |      1 |    592 |00:02:24.22 |     533K|  57185 |      
    |   3 |    NESTED LOOPS                            |                                |      1 |      1 |    592 |00:02:24.15 |     532K|  57183 |     
    |   4 |     NESTED LOOPS                           |                                |      1 |      1 |    594 |00:02:24.00 |     531K|  57171 |     
    |   5 |      NESTED LOOPS                          |                                |      1 |      1 |    594 |00:02:23.98 |     530K|  57170 |    
    |   6 |       NESTED LOOPS                         |                                |      1 |      1 |    594 |00:02:23.49 |     529K|  57129 |   
    |   7 |        NESTED LOOPS                        |                                |      1 |      1 |    594 |00:02:23.08 |     528K|  57096 |  
    |   8 |         NESTED LOOPS                       |                                |      1 |      1 |    594 |00:02:22.51 |     526K|  57031 |       |
    |   9 |          NESTED LOOPS                      |                                |      1 |      1 |    594 |00:02:21.79 |     524K|  56968 |       |
    |  10 |           NESTED LOOPS                     |                                |      1 |      1 |   2990 |00:02:09.65 |     515K|  55703 |      
    |  11 |            NESTED LOOPS                    |                                |      1 |      1 |   2990 |00:02:09.62 |     515K|  55703 |     
    |  12 |             NESTED LOOPS                   |                                |      1 |      1 |   2990 |00:02:08.47 |     509K|  55599 |     
    |* 13 |              HASH JOIN                     |                                |      1 |      2 |   3000 |00:02:07.88 |     503K|  55516 |   974K
    |  14 |               NESTED LOOPS                 |                                |      1 |      2 |   3000 |00:01:39.07 |     471K|  23025 |   
    |  15 |                NESTED LOOPS                |                                |      1 |      1 |      1 |00:00:00.01 |       4 |      0 |  
    |  16 |                 TABLE ACCESS BY INDEX ROWID| HR_ALL_ORGANIZATION_UNITS_TL   |      1 |      1 |      1 |00:00
    |* 17 |                  INDEX UNIQUE SCAN         | HR_ALL_ORGANIZATION_UNTS_TL_PK |      1 |      1 |      1 |00:00:00.0
    |* 18 |                 TABLE ACCESS BY INDEX ROWID| HR_ALL_ORGANIZATION_UNITS      |      1 |      1 |      1 |00:00:
    |* 19 |                  INDEX UNIQUE SCAN         | HR_ORGANIZATION_UNITS_PK       |      1 |      1 |      1 |00:00:00.01
    |* 20 |                TABLE ACCESS BY INDEX ROWID | OE_ORDER_LINES_ALL             |      1 |      2 |   3000 |00:01
    |* 21 |                 INDEX RANGE SCAN           | OE_ORDER_LINES_N20             |      1 |  47575 |    156K|00:00:01.76 |     420
    |* 22 |               TABLE ACCESS FULL            | MTL_ITEM_CATEGORIES            |      1 |  96977 |    263K|00:00:28.49
    |* 23 |              TABLE ACCESS BY INDEX ROWID   | MTL_CATEGORIES_B               |   3000 |      1 |   2990 |00:00:0
    |* 24 |               INDEX UNIQUE SCAN            | MTL_CATEGORIES_B_U1            |   3000 |      1 |   3000 |00:00:00.21
    |  25 |             TABLE ACCESS BY INDEX ROWID    | SEAENG_CCFAMILYMODELINFO       |   2990 |      1 |   2990 |
    |* 26 |              INDEX UNIQUE SCAN             | SEAENG_CCFAMILYMODELINFO_U1    |   2990 |      1 |   2990 |00:00:
    |* 27 |            INDEX UNIQUE SCAN               | MTL_PARAMETERS_U1              |   2990 |      1 |   2990 |00:00:00.02 |    
    |* 28 |           TABLE ACCESS BY INDEX ROWID      | OE_ORDER_HEADERS_ALL           |   2990 |      1 |    594 |00:00:
    |* 29 |            INDEX UNIQUE SCAN               | OE_ORDER_HEADERS_U1            |   2990 |      1 |   2990 |00:00:04.29 |   
    |  30 |          TABLE ACCESS BY INDEX ROWID       | HZ_CUST_SITE_USES_ALL          |    594 |      1 |    594 |00:00:
    |* 31 |           INDEX UNIQUE SCAN                | HZ_CUST_SITE_USES_U1           |    594 |      1 |    594 |00:00:00.31 |   
    |  32 |         TABLE ACCESS BY INDEX ROWID        | HZ_CUST_ACCT_SITES_ALL         |    594 |      1 |    594 |00:00:
    |* 33 |          INDEX UNIQUE SCAN                 | HZ_CUST_ACCT_SITES_U1          |    594 |      1 |    594 |00:00:00.23 |   
    |  34 |        TABLE ACCESS BY INDEX ROWID         | HZ_PARTY_SITES                 |    594 |      1 |    594 |00:00:0
    |* 35 |         INDEX UNIQUE SCAN                  | HZ_PARTY_SITES_U1              |    594 |      1 |    594 |00:00:00.17 |     601
    |  36 |       TABLE ACCESS BY INDEX ROWID          | HZ_LOCATIONS                   |    594 |      1 |    594 |00:00:00.4
    |* 37 |        INDEX UNIQUE SCAN                   | HZ_LOCATIONS_U1                |    594 |      1 |    594 |00:00:00.17 |   
    |  38 |      TABLE ACCESS BY INDEX ROWID           | FND_TERRITORIES                |    594 |      1 |    594 |00:00:00
    |* 39 |       INDEX UNIQUE SCAN                    | FND_TERRITORIES_U1             |    594 |      1 |    594 |00:00:00.02
    |* 40 |     TABLE ACCESS BY INDEX ROWID            | HZ_CUST_ACCOUNTS               |    594 |      1 |    592 |00:00:0
    |* 41 |      INDEX UNIQUE SCAN                     | HZ_CUST_ACCOUNTS_U1            |    594 |      1 |    594 |00:00:00.04 |
    |* 42 |    INDEX RANGE SCAN                        | FND_LOOKUP_VALUES_U1           |    592 |      1 |    592 |00:00:00.06 |   
    |  43 |   TABLE ACCESS BY INDEX ROWID              | HZ_PARTIES                     |    592 |      1 |    592 |00:00:00.91 |  
    |* 44 |    INDEX UNIQUE SCAN                       | HZ_PARTIES_U1                  |    592 |      1 |    592 |00:00:00.40 |     599
    Predicate Information (identified by operation id):
      13 - access("OEL"."INVENTORY_ITEM_ID"="MIC"."INVENTORY_ITEM_ID")
      17 - access("HAOTL"."ORGANIZATION_ID"=189 AND "HAOTL"."LANGUAGE"=USERENV('LANG'))
           filter(DECODE("HR_SECURITY"."VIEW_ALL"(),'Y','TRUE',"HR_SECURITY"."SHOW_RECORD"('HR_ALL_ORGAN
      18 - filter("HAO"."BUSINESS_GROUP_ID"=DECODE("HR_GENERAL"."GET_XBG_PROFILE"(),'Y',"HAO"."BUSINESS_
      19 - access("HAO"."ORGANIZATION_ID"=189)
      20 - filter(("OEL"."FLOW_STATUS_CODE"='CLOSED' AND INTERNAL_FUNCTION("OEL"."ATTRIBUTE1") AND INTER
      21 - access("OEL"."SYS_NC00342$">=TO_DATE(' 2009-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "O
                  'syyyy-mm-dd hh24:mi:ss'))
      22 - filter("MIC"."CATEGORY_SET_ID"=4)
      23 - filter(("MC"."DESCRIPTION" IS NOT NULL AND INTERNAL_FUNCTION("MC"."SEGMENT7")))
      24 - access("MIC"."CATEGORY_ID"="MC"."CATEGORY_ID")
      26 - access("MC"."DESCRIPTION"="CCM"."STMODELNUMBER")
      27 - access("OEL"."SHIP_FROM_ORG_ID"="MP"."ORGANIZATION_ID")
      28 - filter("OEH"."ORG_ID"=189)
      29 - access("OEH"."HEADER_ID"="OEL"."HEADER_ID")
      31 - access("OEH"."INVOICE_TO_ORG_ID"="HCSU"."SITE_USE_ID")
      33 - access("HCSU"."CUST_ACCT_SITE_ID"="HCAS"."CUST_ACCT_SITE_ID")
      35 - access("HCAS"."PARTY_SITE_ID"="HPS"."PARTY_SITE_ID")
      37 - access("HPS"."LOCATION_ID"="HL"."LOCATION_ID")
      39 - access("HL"."COUNTRY"="FT"."TERRITORY_CODE")
      40 - filter(("HCA"."SALES_CHANNEL_CODE"<>'RTL' AND "HCA"."SALES_CHANNEL_CODE"<>'AD-RTL' AND "HCA".
      41 - access("HCAS"."CUST_ACCOUNT_ID"="HCA"."CUST_ACCOUNT_ID")
      42 - access("FLV"."LOOKUP_TYPE"='SALES_CHANNEL' AND "FLV"."LOOKUP_CODE"="HCA"."SALES_CHANNEL_CODE"
           filter(("FLV"."LOOKUP_CODE"<>'RTL' AND "FLV"."LOOKUP_CODE"<>'AD-RTL' AND "FLV"."LOOKUP_CODE"<
      44 - access("HCA"."PARTY_ID"="HP"."PARTY_ID")
    90 rows selected.
    {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              &nbs

  • Need to tune the sql query

    Hi,
    PLease help me to tune the query
    select primary_rep,
    rep_ssn,
    nvl(product_type_desc, 'UNKNOWN') as productName,
    ROUND(sum(contributions)) as contribution,
    ROUND(sum(withdrawls)) as withdrawls,
    ROUND(sum(netflow)) as netflow
    from dev.NETFLOW_VIEW_TSS_P
    where bd = 'TSS'
    and actvtydate between '01-Jan-2011' and '30-Sep-2011'     
    and rep_ssn IN (select tax_id from broker_view where broker_id = '600468')
    group by primary_rep, rep_ssn, nvl(product_type_desc, 'UNKNOWN');
    explain plan details below
    | Id  | Operation                                     | Name                        | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     | Pstart| Pstop |
    |   0 | SELECT STATEMENT                              |                             |    79 |   158K|       | 74348   (1)| 00:14:53 |       |       |
    |   1 |  HASH GROUP BY                                |                             |    79 |   158K|       | 74348   (1)| 00:14:53 |       |       |
    |*  2 |   HASH JOIN                                   |                             |    79 |   158K|       | 74347   (1)| 00:14:53 |       |       |
    |   3 |    VIEW                                       | VW_NSO_1                    |     1 |     6 |       |    18  (17)| 00:00:01 |       |       |
    |   4 |     HASH UNIQUE                               |                             |     1 |   100 |       |    18  (17)| 00:00:01 |       |       |
    |   5 |      NESTED LOOPS OUTER                       |                             |     1 |   100 |       |    17  (12)| 00:00:01 |       |       |
    |   6 |       MERGE JOIN CARTESIAN                    |                             |     1 |    92 |       |    17  (12)| 00:00:01 |       |       |
    |   7 |        NESTED LOOPS OUTER                     |                             |     1 |    71 |       |    15  (14)| 00:00:01 |       |       |
    |   8 |         NESTED LOOPS OUTER                    |                             |     1 |    67 |       |    12  (17)| 00:00:01 |       |       |
    |*  9 |          HASH JOIN OUTER                      |                             |     1 |    63 |       |    10  (20)| 00:00:01 |       |       |
    |  10 |           NESTED LOOPS OUTER                  |                             |     1 |    56 |       |     6   (0)| 00:00:01 |       |       |
    |  11 |            NESTED LOOPS OUTER                 |                             |     1 |    52 |       |     4   (0)| 00:00:01 |       |       |
    |  12 |             TABLE ACCESS BY INDEX ROWID       | AFFILIATE                   |     1 |    45 |       |     2   (0)| 00:00:01 |       |       |
    |* 13 |              INDEX UNIQUE SCAN                | AFFILIATE_PK                |     1 |       |       |     1   (0)| 00:00:01 |       |       |
    |* 14 |             TABLE ACCESS FULL                 | PORTAL_BROKER               |     1 |     7 |       |     2   (0)| 00:00:01 |       |       |
    |  15 |            VIEW PUSHED PREDICATE              |                             |     1 |     4 |       |     2   (0)| 00:00:01 |       |       |
    |  16 |             NESTED LOOPS                      |                             |     1 |    28 |       |     2   (0)| 00:00:01 |       |       |
    |  17 |              NESTED LOOPS                     |                             |     1 |    21 |       |     2   (0)| 00:00:01 |       |       |
    |* 18 |               TABLE ACCESS BY INDEX ROWID     | LOCATION_GROUP_TYPE_DESC    |     1 |     5 |       |     1   (0)| 00:00:01 |       |       |
    |* 19 |                INDEX UNIQUE SCAN              | LOCATION_GROUP_TYPE_DESC_PK |     1 |       |       |     0   (0)| 00:00:01 |       |       |
    |* 20 |               INDEX RANGE SCAN                | LOCATION_GROUP_UK           |     1 |    16 |       |     1   (0)| 00:00:01 |       |       |
    |* 21 |              INDEX UNIQUE SCAN                | CONTACT_METHOD_PK           |     1 |     7 |       |     0   (0)| 00:00:01 |       |       |
    |  22 |           VIEW                                |                             |     1 |     7 |       |     3  (34)| 00:00:01 |       |       |
    |  23 |            HASH UNIQUE                        |                             |     1 |    11 |       |     3  (34)| 00:00:01 |       |       |
    |* 24 |             INDEX RANGE SCAN                  | AFFILIATE_TYPE_PK           |     1 |    11 |       |     2   (0)| 00:00:01 |       |       |
    |  25 |          VIEW PUSHED PREDICATE                |                             |     1 |     4 |       |     2   (0)| 00:00:01 |       |       |
    |  26 |           NESTED LOOPS                        |                             |     1 |    28 |       |     2   (0)| 00:00:01 |       |       |
    |  27 |            NESTED LOOPS                       |                             |     1 |    21 |       |     2   (0)| 00:00:01 |       |       |
    |* 28 |             TABLE ACCESS BY INDEX ROWID       | LOCATION_GROUP_TYPE_DESC    |     1 |     5 |       |     1   (0)| 00:00:01 |       |       |
    |* 29 |              INDEX UNIQUE SCAN                | LOCATION_GROUP_TYPE_DESC_PK |     1 |       |       |     0   (0)| 00:00:01 |       |       |
    |* 30 |             INDEX RANGE SCAN                  | LOCATION_GROUP_UK           |     1 |    16 |       |     1   (0)| 00:00:01 |       |       |
    |* 31 |            INDEX UNIQUE SCAN                  | CONTACT_METHOD_PK           |     1 |     7 |       |     0   (0)| 00:00:01 |       |       |
    |  32 |         VIEW PUSHED PREDICATE                 |                             |     1 |     4 |       |     3   (0)| 00:00:01 |       |       |
    |  33 |          NESTED LOOPS OUTER                   |                             |     1 |    35 |       |     3   (0)| 00:00:01 |       |       |
    |  34 |           NESTED LOOPS                        |                             |     1 |    32 |       |     3   (0)| 00:00:01 |       |       |
    |  35 |            NESTED LOOPS                       |                             |     1 |    22 |       |     2   (0)| 00:00:01 |       |       |
    |* 36 |             TABLE ACCESS BY INDEX ROWID       | LOCATION_GROUP_TYPE_DESC    |     1 |     5 |       |     1   (0)| 00:00:01 |       |       |
    |* 37 |              INDEX UNIQUE SCAN                | LOCATION_GROUP_TYPE_DESC_PK |     1 |       |       |     0   (0)| 00:00:01 |       |       |
    |* 38 |             INDEX RANGE SCAN                  | LOCATION_GROUP_UK           |     1 |    17 |       |     1   (0)| 00:00:01 |       |       |
    |  39 |            TABLE ACCESS BY INDEX ROWID        | ADDRESS                     |     1 |    10 |       |     1   (0)| 00:00:01 |       |       |
    |* 40 |             INDEX UNIQUE SCAN                 | ADDRESS_PK                  |     1 |       |       |     0   (0)| 00:00:01 |       |       |
    |* 41 |           INDEX UNIQUE SCAN                   | PROV_STDSC_PK               |     1 |     3 |       |     0   (0)| 00:00:01 |       |       |
    |  42 |        BUFFER SORT                            |                             |     1 |    21 |       |    14  (15)| 00:00:01 |       |       |
    |* 43 |         INDEX RANGE SCAN                      | I_BROKER_BKBRCMB            |     1 |    21 |       |     2   (0)| 00:00:01 |       |       |
    |* 44 |       INDEX UNIQUE SCAN                       | BROKER_BRANCH_PK            |     1 |     8 |       |     0   (0)| 00:00:01 |       |       |
    |  45 |    VIEW                                       | NETFLOW_VIEW_TSS_P          |   391K|   766M|       | 74325   (1)| 00:14:52 |       |       |
    |  46 |     UNION-ALL                                 |                             |       |       |       |            |          |       |       |
    |* 47 |      HASH JOIN RIGHT OUTER                    |                             | 11645 |  1671K|       | 49023   (1)| 00:09:49 |       |       |
    |  48 |       VIEW                                    |                             |     1 |    38 |       | 13602   (1)| 00:02:44 |       |       |
    |* 49 |        FILTER                                 |                             |       |       |       |            |          |       |       |
    |  50 |         TABLE ACCESS FULL                     | SALES_REPORT_REP_MAPPING    |  9023 |   326K|       |    58   (2)| 00:00:01 |       |       |
    |* 51 |         FILTER                                |                             |       |       |       |            |          |       |       |
    |  52 |          SORT GROUP BY NOSORT                 |                             |     1 |    11 |       |     3   (0)| 00:00:01 |       |       |
    |  53 |           TABLE ACCESS BY INDEX ROWID         | SALES_REPORT_REP_MAPPING    |     1 |    11 |       |     3   (0)| 00:00:01 |       |       |
    |* 54 |            INDEX RANGE SCAN                   | I_SAL_REP_MAP_REPID         |     1 |       |       |     2   (0)| 00:00:01 |       |       |
    |  55 |       NESTED LOOPS                            |                             | 11645 |  1239K|       | 35420   (1)| 00:07:06 |       |       |
    |* 56 |        HASH JOIN RIGHT OUTER                  |                             | 11645 |   898K|       | 12112   (1)| 00:02:26 |       |       |
    |  57 |         INDEX FAST FULL SCAN                  | I_VENDORS_ACCTLOC           |   986 |  3944 |       |     2   (0)| 00:00:01 |       |       |
    |* 58 |         HASH JOIN RIGHT OUTER                 |                             | 11645 |   852K|       | 12109   (1)| 00:02:26 |       |       |
    |* 59 |          MAT_VIEW ACCESS FULL                 | SECS_DESC                   | 26777 |   418K|       |  3082   (1)| 00:00:37 |       |       |
    |* 60 |          FILTER                               |                             |       |       |       |            |          |       |       |
    |* 61 |           HASH JOIN RIGHT OUTER               |                             | 11645 |   670K|       |  9026   (1)| 00:01:49 |       |       |
    |  62 |            MAT_VIEW ACCESS FULL               | ENTRYCODES                  |  1640 | 11480 |       |     8   (0)| 00:00:01 |       |       |
    |* 63 |            HASH JOIN                          |                             | 34936 |  1774K|       |  9017   (1)| 00:01:49 |       |       |
    |* 64 |             INDEX FULL SCAN                   | I_CODE                      |    30 |   420 |       |     1   (0)| 00:00:01 |       |       |
    |  65 |             PARTITION RANGE ALL               |                             |   342K|    12M|       |  9013   (1)| 00:01:49 |     1 |    20 |
    |  66 |              TABLE ACCESS BY LOCAL INDEX ROWID| BKPG                        |   342K|    12M|       |  9013   (1)| 00:01:49 |     1 |    20 |
    |* 67 |               INDEX RANGE SCAN                | I_BKPG_ACT_REC_IDX          |   342K|       |       |  1412   (1)| 00:00:17 |     1 |    20 |
    |  68 |        TABLE ACCESS BY INDEX ROWID            | ACCT_CUBE                   |     1 |    30 |       |     2   (0)| 00:00:01 |       |       |
    |* 69 |         INDEX UNIQUE SCAN                     | ACCT_CUBE_PK                |     1 |       |       |     1   (0)| 00:00:01 |       |       |
    |* 70 |      HASH JOIN RIGHT OUTER                    |                             |   379K|    52M|       | 25303   (1)| 00:05:04 |       |       |
    |  71 |       VIEW                                    |                             |     1 |    38 |       | 13602   (1)| 00:02:44 |       |       |
    |* 72 |        FILTER                                 |                             |       |       |       |            |          |       |       |
    |  73 |         TABLE ACCESS FULL                     | SALES_REPORT_REP_MAPPING    |  9023 |   326K|       |    58   (2)| 00:00:01 |       |       |
    |* 74 |         FILTER                                |                             |       |       |       |            |          |       |       |
    |  75 |          SORT GROUP BY NOSORT                 |                             |     1 |    11 |       |     3   (0)| 00:00:01 |       |       |
    |  76 |           TABLE ACCESS BY INDEX ROWID         | SALES_REPORT_REP_MAPPING    |     1 |    11 |       |     3   (0)| 00:00:01 |       |       |
    |* 77 |            INDEX RANGE SCAN                   | I_SAL_REP_MAP_REPID         |     1 |       |       |     2   (0)| 00:00:01 |       |       |
    |* 78 |       HASH JOIN                               |                             |   379K|    39M|    33M| 11696   (2)| 00:02:21 |       |       |
    |* 79 |        HASH JOIN                              |                             |   379K|    28M|       |  3210   (2)| 00:00:39 |       |       |
    |* 80 |         INDEX FAST FULL SCAN                  | I_VENDORS_ACCTLOC           |   986 |  3944 |       |     2   (0)| 00:00:01 |       |       |
    |* 81 |         HASH JOIN                             |                             |   379K|    27M|    14M|  3204   (2)| 00:00:39 |       |       |
    |  82 |          INDEX FAST FULL SCAN                 | SECS_DESC_PK                |   744K|  6545K|       |   457   (3)| 00:00:06 |       |       |
    |* 83 |          FILTER                               |                             |       |       |       |            |          |       |       |
    |* 84 |           HASH JOIN RIGHT OUTER               |                             |   379K|    24M|       |   569   (3)| 00:00:07 |       |       |
    |  85 |            MAT_VIEW ACCESS FULL               | MFENTRYCODES                |  6853 | 61677 |       |    15   (0)| 00:00:01 |       |       |
    |  86 |            PARTITION RANGE ALL                |                             |  1138K|    62M|       |   542   (1)| 00:00:07 |     1 |    15 |
    |* 87 |             TABLE ACCESS BY LOCAL INDEX ROWID | MFBKPG                      |  1138K|    62M|       |   542   (1)| 00:00:07 |     1 |    15 |
    |* 88 |              INDEX RANGE SCAN                 | I_MFBKPGP_ACTDT_ACCT_TST    |   904 |       |       |   107   (0)| 00:00:02 |     1 |    15 |
    |  89 |        INDEX FAST FULL SCAN                   | I_ACCT_CUBE_REPID_MKT       |  1952K|    52M|       |  3096   (2)| 00:00:38 |       |       |
    Predicate Information (identified by operation id):
       2 - access("REP_SSN"="$nso_col_1")
       9 - access("A"."AFFILIATE_ID"="MGR"."AFFILIATE_ID"(+))
      13 - access("A"."AFFILIATE_ID"='600468')
      14 - filter("PORTAL_BROKER"."BROKER_ID"(+)='600468')
      18 - filter("LT"."ADDRESS_CONTACT_IND"='C')
      19 - access("LT"."LOCATION_GROUP_TYPE_CODE"='WF')
      20 - access("L"."LOCATION_GROUP_TYPE_CODE"='WF' AND "L"."AFFILIATE_ID"="A"."AFFILIATE_ID")
           filter("L"."CONTACT_METHOD_ID" IS NOT NULL)
      21 - access("L"."CONTACT_METHOD_ID"="B"."CONTACT_METHOD_ID")
      24 - access("AFFILIATE_ID"='600468')
           filter("AFFILIATE_TYPE"='BBE' OR "AFFILIATE_TYPE"='BRM' OR "AFFILIATE_TYPE"='ISP')
      28 - filter("LT"."ADDRESS_CONTACT_IND"='C')
      29 - access("LT"."LOCATION_GROUP_TYPE_CODE"='WT')
      30 - access("L"."LOCATION_GROUP_TYPE_CODE"='WT' AND "L"."AFFILIATE_ID"="A"."AFFILIATE_ID")
           filter("L"."CONTACT_METHOD_ID" IS NOT NULL)
      31 - access("L"."CONTACT_METHOD_ID"="B"."CONTACT_METHOD_ID")
      36 - filter("LT"."ADDRESS_CONTACT_IND"='A')
      37 - access("LT"."LOCATION_GROUP_TYPE_CODE"='WM')
      38 - access("L"."LOCATION_GROUP_TYPE_CODE"='WM' AND "L"."AFFILIATE_ID"="A"."AFFILIATE_ID")
           filter("L"."ADDRESS_ID" IS NOT NULL)
      40 - access("L"."ADDRESS_ID"="A"."ADDRESS_ID")
      41 - access("A"."PROVINCE_STATE_CODE"="S"."PROVINCE_STATE_CODE"(+))
      43 - access("B"."BROKER_ID"='600468')
      44 - access("B"."BRANCH_ID"="BR"."BRANCH_ID"(+))
      47 - access("B"."REPID"="K"."REPID"(+))
      49 - filter( EXISTS (SELECT 0 FROM DEV."SALES_REPORT_REP_MAPPING" "SALES_REPORT_REP_MAPPING" WHERE "REPID"=:B1 GROUP BY "REPID" HAVING
                  SUM(NVL("PCT",100))=100))
      51 - filter(SUM(NVL("PCT",100))=100)
      54 - access("REPID"=:B1)
      56 - access(NVL("V"."VENDOR_CD"(+),'xxx')=NVL("VENDOR_CD",'yyy'))
      58 - access("CUSIP"(+)="A"."CUSIP")
      59 - filter("COMM_SECTYPE"(+)='MF')
      60 - filter(NVL("Z"."PERF_INCLUDE_PORTFOLIO",'xx')='Y')
      61 - access("A"."ENTRYCODE"="Z"."ENTRYCODE"(+))
      63 - access("CODE_IN"=SUBSTR("A"."ACCT",1,3))
      64 - access("CODE_TYPE"='NETFLOW')
           filter("CODE_TYPE"='NETFLOW')
      67 - access("A"."ACTVTYDATE">=TO_DATE(' 2011-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "A"."ACTVTYDATE"<=TO_DATE(' 2011-09-30
                  00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
      69 - access("B"."ACCOUNTKEY"='11,,,'||"A"."ACCT")
      70 - access("A"."REPID"="K"."REPID"(+))
      72 - filter( EXISTS (SELECT 0 FROM DEV."SALES_REPORT_REP_MAPPING" "SALES_REPORT_REP_MAPPING" WHERE "REPID"=:B1 GROUP BY "REPID" HAVING
                  SUM(NVL("PCT",100))=100))
      74 - filter(SUM(NVL("PCT",100))=100)
      77 - access("REPID"=:B1)
      78 - access("A"."ACCOUNTKEY"='20,'||"B"."VENDOR_CD"||','||"B"."FUNDNO"||','||"B"."ACCT")
      79 - access("V"."VENDOR_CD"="B"."VENDOR_CD")
      80 - filter("V"."VENDOR_CD"<>'HNR' AND "V"."VENDOR_CD"<>'CNL' AND "V"."VENDOR_CD"<>'IRE' AND "V"."VENDOR_CD"<>'WLR' AND
                  "V"."VENDOR_CD"<>'WLP' AND "V"."VENDOR_CD"<>'PER' AND "V"."VENDOR_CD"<>'BHV')
      81 - access("SD"."CUSIP"="B"."CUSIP")
      83 - filter(NVL("E"."PERF_INCLUDE_PORTFOLIO",'xx')='Y')
      84 - access("B"."ENTRYCODE"="E"."ENTRYCODE"(+))
      87 - filter("B"."VENDOR_CD"<>'HNR' AND "B"."VENDOR_CD"<>'CNL' AND "B"."VENDOR_CD"<>'IRE' AND "B"."VENDOR_CD"<>'WLR' AND
                  "B"."VENDOR_CD"<>'WLP' AND "B"."VENDOR_CD"<>'PER' AND "B"."VENDOR_CD"<>'BHV')
      88 - access(SYS_OP_DESCEND("ACTVTYDATE")>=HEXTORAW('8790F6E1FEF8FEFAFF')  AND SYS_OP_DESCEND("ACTVTYDATE")<=HEXTORAW('8790FEF8FEF8FEFAFF')
           filter(SYS_OP_UNDESCEND(SYS_OP_DESCEND("ACTVTYDATE"))>=TO_DATE(' 2011-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
                  SYS_OP_UNDESCEND(SYS_OP_DESCEND("ACTVTYDATE"))<=TO_DATE(' 2011-09-30 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
    158 rows selected.Thanks

    So this is a VIEW with I think 18 tables (I may have missed one or two), some of which are referenced more than one. Some of these tables have tens of miilions of rows. What exactly are you expecting from us?
    I guess this is a data warehouse or BI query. Sometimes such queries just take an age to run and there's nothing which can be done except result caching. Other times the queries can be tuned, but iit requires way more context and domain knowledge than you have provided.
    Please start by reading [url https://forums.oracle.com/forums/thread.jspa?threadID=2174552#9360003]this FAQ. .
    Cheers, APC

  • Tune the SQL Query need a Suggestion

    Hi,
    There is a view created which takes 17seconds to execute the query, When i split the query and execute each query takes .15 seconds to execute. Below it the query which i need to improve the performance of the code please need your suggestion in order to tweek so that time take for execution become less
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_1 "Product Name",
    TRIM (UPPER (serial_number_1)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_1 != 'NULL'
    AND Serial_Number_1 != 'NULL'
    AND C.Amid = A.Amid(+)
    UNION
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_2 "Product Name",
    TRIM (UPPER (serial_number_2)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_2 != 'NULL'
    AND serial_number_2 != 'NULL'
    AND C.Amid = A.Amid(+)
    UNION
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_3 "Product Name",
    TRIM (UPPER (serial_number_3)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_3 != 'NULL'
    AND serial_number_3 != 'NULL'
    AND C.Amid = A.Amid(+)
    UNION
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_4 "Product Name",
    TRIM (UPPER (serial_number_4)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_4 != 'NULL'
    AND serial_number_4 != 'NULL'
    AND C.Amid = A.Amid(+)
    UNION
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_5 "Product Name",
    TRIM (UPPER (serial_number_5)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_5 != 'NULL'
    AND serial_number_5 != 'NULL'
    AND C.Amid = A.Amid(+)
    UNION
    SELECT 'ERP' "Source",
    Imv.Bill_To "Distibutor",
    Imv.Ship_To "Reseller",
    Ship_To_City "Reseller City",
    Ship_To_State "Reseller State",
    Edb2_Global.Country_Name_Theater(Imv.Ship_To_Country ,'COUNTRY') "Reseller Country",
    Edb2_Global.Country_Name_Theater(Imv.Ship_To_Country ,'THEATER') "Reseller Theater",
    Cl.Company_Name "Customer Name",
    Cl.City "Customer City",
    Cl.State "Customer State",
    Edb2_Global.Country_Name_Theater(Cl.Country,'COUNTRY') "Customer Country",
    Edb2_Global.Country_Name_Theater(Cl.Country,'THEATER') "Customer Theater",
    Imv.So_Booked_Date "Sales Book Date",
    Imv.Ship_Date "Ship Date",
    Imv.Order_Number "Sales Order Number",
    Imv.Cust_Po_Number "PO Number",
    Cp.Item_Name "Product Name",
    Cp.Product_Serial_Number "Product Serial Number",
    TO_CHAR(Imv.contract_number)"Contract ID",
    Imv.Service_Description "Support Name",
    Imv.Service_Start_Date "Start Date",
    Imv.Service_End_Date "Expire Date",
    Imv.Header_Status "Header Status",
    Imv.Line_Status "Line Status",
    Imv.Line_Type "Line Type",
    Imv.Termination_Reason "Termination Reason",
    Cl.Location_Id "Location_id",
    cp.id "Product_id"
    FROM ib_support_contracts_mv imv,
    customer_products cp,
    customer_locations cl
    WHERE Upper (Trim (Imv.Serial_Number)) = Upper (Trim (Cp.Product_Serial_Number(+)))
    AND Cp.Location_Id = Cl.Location_Id(+)
    There is a union for each and every query, Please suggest how to tweek
    Thanks
    Sudhir

    Try
    select distinct a.*
    from (SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_1 "Product Name",
    TRIM (UPPER (serial_number_1)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_1 != 'NULL'
    AND Serial_Number_1 != 'NULL'
    AND C.Amid = A.Amid(+)
    union all
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_2 "Product Name",
    TRIM (UPPER (serial_number_2)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_2 != 'NULL'
    AND serial_number_2 != 'NULL'
    AND C.Amid = A.Amid(+)
    union all
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_3 "Product Name",
    TRIM (UPPER (serial_number_3)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_3 != 'NULL'
    AND serial_number_3 != 'NULL'
    AND C.Amid = A.Amid(+)
    union all
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_4 "Product Name",
    TRIM (UPPER (serial_number_4)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_4 != 'NULL'
    AND serial_number_4 != 'NULL'
    AND C.Amid = A.Amid(+)
    union all
    SELECT 'Parature' "Source",
    NULL "Distibutor",
    reseller_name "Reseller",
    NULL "Reseller City",
    NULL "Reseller State",
    A.Country "Reseller Country",
    DECODE (a.state, 'APAC', 'APAC', 'EMEA', 'EMEA', NULL, 'Unknown', 'US' ) "Reseller Theater",
    NULL "Customer Name",
    NULL "Customer City",
    NULL "Customer State",
    NULL "Customer Country",
    NULL "Customer Theater",
    NULL "Sales Book Date",
    NULL "Ship Date",
    c.sales_order_number "Sales Order Number",
    C.Po_Number "PO Number",
    controller_model_number_5 "Product Name",
    TRIM (UPPER (serial_number_5)) "Product Serial Number",
    c.contract_id "Contract ID",
    product_name "Support Name",
    TO_DATE (c.contract_purchase_date) "Start Date",
    TO_DATE (c.contract_expiration_date) "Expire Date",
    NULL "Header Status",
    NULL "Line Status",
    NULL "Line Type",
    NULL "Termination Reason",
    NULL "Location_id",
    NULL "Product_id"
    FROM edb_assets c,
    edb_accounts a
    WHERE controller_model_number_5 != 'NULL'
    AND serial_number_5 != 'NULL'
    AND C.Amid = A.Amid(+)
    union all
    SELECT 'ERP' "Source",
    Imv.Bill_To "Distibutor",
    Imv.Ship_To "Reseller",
    Ship_To_City "Reseller City",
    Ship_To_State "Reseller State",
    Edb2_Global.Country_Name_Theater(Imv.Ship_To_Country ,'COUNTRY') "Reseller Country",
    Edb2_Global.Country_Name_Theater(Imv.Ship_To_Country ,'THEATER') "Reseller Theater",
    Cl.Company_Name "Customer Name",
    Cl.City "Customer City",
    Cl.State "Customer State",
    Edb2_Global.Country_Name_Theater(Cl.Country,'COUNTRY') "Customer Country",
    Edb2_Global.Country_Name_Theater(Cl.Country,'THEATER') "Customer Theater",
    Imv.So_Booked_Date "Sales Book Date",
    Imv.Ship_Date "Ship Date",
    Imv.Order_Number "Sales Order Number",
    Imv.Cust_Po_Number "PO Number",
    Cp.Item_Name "Product Name",
    Cp.Product_Serial_Number "Product Serial Number",
    TO_CHAR(Imv.contract_number)"Contract ID",
    Imv.Service_Description "Support Name",
    Imv.Service_Start_Date "Start Date",
    Imv.Service_End_Date "Expire Date",
    Imv.Header_Status "Header Status",
    Imv.Line_Status "Line Status",
    Imv.Line_Type "Line Type",
    Imv.Termination_Reason "Termination Reason",
    Cl.Location_Id "Location_id",
    cp.id "Product_id"
    FROM ib_support_contracts_mv imv,
    customer_products cp,
    customer_locations cl
    WHERE Upper (Trim (Imv.Serial_Number)) = Upper (Trim (Cp.Product_Serial_Number(+)))
    AND Cp.Location_Id = Cl.Location_Id(+)) a
    Edited by: HuaMin on May 9, 2012 2:58 PM

  • Fine Tune a SQl Query

    Hello ,
    Please find here a query I need to enhance , it takes now above  1 minute to complete and supposed to not exceed 5 sec.
    appreciate your support, 
    declare @@StartDate as datetime,
    @@EndDate as datetime,
    @@CCODE as varchar(6)
    set @@StartDate = '01/01/2014'
    set @@EndDate = '01/31/2014'
    set @@CCODE = '0077662'
    select
    'ALLOWED' as Descr, @@StartDate as StartDate, @@EndDate as EndDate, LEFT(ID,6) as CCODE,COUNT(*) as No_of_CUST,COUNT(IDs) as No_of_IDs
    from dbo.tbl_tableMaster
    where
    date between @@startdate and @@Enddate
    and Type = 0
    and code = 1
    and KindOFMaterial in ( '00','01')
    and profile = 'VIP'
    and LEFT(ID,6) = @@CCODE
    GROUP BY LEFT(ID,6)

    If you will be able to change this procedure and use single @, I would go for it. 
    I know it's a bit a bad habit, but personally I almost always fix the code to my liking when I am working with some code written by anyone else. Even knowing 'if it's ain't broken don't fix it' I would go ahead and fix it.
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Performance Tunning of Sql query

    Hi,
    I have table A with size 120 Million and two more tables are of size 2 Million on Table B and less than 1 Million size on table C.
    I had created Partition and Parallel degree 4 on the table A. Created table B with Parallel degree 2 and Created table C with NOPARALLEL.
    My query is using above tables with joins and inserting into table D using HINT /*+ APPEND NOLOGGING*/
    I had executed the explain on the above criteria the cost is showing 20767.
    Later created Tables A,B and C with NOPARALLEL. Applied HINT on Table D /*+ APPEND NOLOGGING*/
    and als applied HINT /*+ PARALLEL(A, 4) PARALLEL(B C, 2) on select query which uses to insert into Table D.
    My question which is best practice on PARALLEL degree creation at table level or query level:
    a) Creating table with Paralle (degree 4)
    b) Applying HINT /*+ PARALLEL (TABLE A , 4) */ at query level
    Regards,
    Prakash

    957901 wrote:
    Hi,
    I have table A with size 120 Million and two more tables are of size 2 Million on Table B and less than 1 Million size on table C.
    I had created Partition and Parallel degree 4 on the table A. Created table B with Parallel degree 2 and Created table C with NOPARALLEL.
    My query is using above tables with joins and inserting into table D using HINT /*+ APPEND NOLOGGING*/
    I had executed the explain on the above criteria the cost is showing 20767. Cost is meaningless out of context.
    Later created Tables A,B and C with NOPARALLEL. Applied HINT on Table D /*+ APPEND NOLOGGING*/
    and als applied HINT /*+ PARALLEL(A, 4) PARALLEL(B C, 2) on select query which uses to insert into Table D.
    My question which is best practice on PARALLEL degree creation at table level or query level:
    a) Creating table with Paralle (degree 4)
    b) Applying HINT /*+ PARALLEL (TABLE A , 4) */ at query level Whichever works best for what you're needing to achieve.
    Which is another way of saying you haven't provided enough information for anyone here to make any informed decision or suggestion for you.
    {message:id=9360002}
    For performance issues/tuning requests, read the two threads linked to by this FAQ: {message:id=9360003}

  • Request for tunning the SQL query

    Hi,
    I've a query mentioned below.I've tried it in different ways.But still it is taking much time.
    And there are some 9 indexes created on the column siteid. And both the table having indexes.But still full table scanning occuring for both the tables. Can any one help on this.Or can anyone write the same query in a different way.
    select r.recordkey,
         r.orgid,
         r.siteid,
         r.class,
    w.pscdate,
         w.ascdate,
         w.targstartdate,
         w.targcompdate,
         w.schedfinish,
         w.actfinish,
         w.estdur,
         w.wonum,
         w.mprn,
    w.sihousename wositehousename,
         w.sihouseno wositehouseno,
         w.sistreet wositestreet,
    w.sicounty wositecounty,
         w.siposttown wositeposttown,
         w.sipostcode wositepostcode,
    w.workorderid
    from relatedrecord r,
    workorder w
    where r.relatedreckey = case when w.parent is null then w.wonum else w.parent end
    and r.orgid = w.orgid
    and r.siteid = w.siteid
    and r.relatedrecclass = 'WORKORDER'
    Explain Plan Res:
    Operation     Object Name     Rows     Bytes     Cost     Object Node     In/Out     PStart     PStop
    SELECT STATEMENT Optimizer Mode=ALL_ROWS          38 K          4819                     
    HASH JOIN          38 K     5 M     4819                     
    TABLE ACCESS FULL     MAXIMO.RELATEDRECORD     30 K     1 M     390                     
    TABLE ACCESS FULL     MAXIMO.WORKORDER     229 K     26 M     2922                     
    Regards,
    Arjun.

    Hi,
    Some question.
    1) r.relatedrecclass = 'WORKORDER', how many records are there with relatedrecclass as "WORKORDER" compared to the total number of records in that table (in percentage)
    2) Do we have index on relatedrecclass? (if possible in combination with recordkey and class column)
    3) What is the total number of records in both the table?
    4) What is the expected number of records with the below query?
    Regards
    Anurag

  • How to tune sql query

    Hi,
    How I can tune sql query? what are the steps for tuning.

    First important step: Infomation gathering. One part can be learning to use the powerful search engine. E.g. search this forum for: "When your query takes too long".
    Second step: Information processing. Check the data you found and apply it to your problem
    Third step: Testing. Compare the result of the applied change before and after. Was there any performance gain as expected?

  • How do tune sql query? Can somebody help me in this? Atleast prescribe book

    how do tune sql query? Can somebody help me in this? Atleast prescribe a reference book.
    I am not able understand How to analyze tkproof output.

    Check out asktom.oracle.com
    Lot's of threads on interpreting tkprof output - no simple easy solution here.
    You need to understand what is happening and THINK. Then you can be a good engineer.
    As for good books...
    Tom Kyte's books are really, really good.
    http://www.amazon.com/Effective-Oracle-Design-Osborne-ORACLE/dp/0072230657/ref=sr_1_3?ie=UTF8&s=books&qid=1266783471&sr=8-3
    http://www.amazon.com/Expert-Oracle-Database-Architecture-Programming/dp/1590595300/ref=sr_1_1?ie=UTF8&s=books&qid=1266783471&sr=8-1
    Good luck!

  • SQL query tunning help

    Hi All,
    Could you please help me out the below SQL query tuning .
    Temp table is having 1 Million records
    Master table is having 60 Million records
    Query :
    SELECT B.*,U.ID, SD, LE, LAE
    FROM client.Temp B, client.Master
    U WHERE U.policyno = B.policyno
    AND B.UPFLAG = 0
    1.     Indexes are created on both email columns and Upflag for both tables.
    2.     Gathered DBMS Stats for MASTER Table
    Data is loading 100k/hour on production .

    When your query takes too long ...
    When your query takes too long ...
    HOW TO: Post a SQL statement tuning request - template posting
    HOW TO: Post a SQL statement tuning request - template posting

  • Error while executing a sql query for select

    HI All,
    ORA-01652: unable to extend temp segment by 128 in tablespace PSTEMP i'm getting this error while i'm executing the sql query for selecting the data.

    I am having 44GB of temp space, while executing the below query my temp space is getting full, Expert please let us know how the issue can be resolved..
    1. I dont want to increase the temp space
    2. I need to tune the query, please provide your recomendations.
    insert /*+APPEND*/ into CST_DSA.HIERARCHY_MISMATCHES
    (REPORT_NUM,REPORT_TYPE,REPORT_DESC,GAP,CARRIED_ITEMS,CARRIED_ITEM_TYPE,NO_OF_ROUTE_OF_CARRIED_ITEM,CARRIED_ITEM_ROUTE_NO,CARRIER_ITEMS,CARRIER_ITEM_TYPE,CARRIED_ITEM_PROTECTION_TYPE,SOURCE_SYSTEM)
    select
    REPORTNUMBER,REPORTTYPE,REPORTDESCRIPTION ,NULL,
    carried_items,carried_item_type,no_of_route_of_carried_item,carried_item_route_no,carrier_items,
    carrier_item_type,carried_item_protection_type,'PACS'
    from
    (select distinct
    c.REPORTNUMBER,c.REPORTTYPE,c.REPORTDESCRIPTION ,NULL,
    a.carried_items,a.carried_item_type,a.no_of_route_of_carried_item,a.carried_item_route_no,a.carrier_items,
    a.carrier_item_type,a.carried_item_protection_type,'PACS'
    from CST_ASIR.HIERARCHY_asir a,CST_DSA.M_PB_CIRCUIT_ROUTING b ,CST_DSA.REPORT_METADATA c
    where a.carrier_item_type in('Connection') and a.carried_item_type in('Service')
    AND a.carrier_items=b.mux
    and c.REPORTNUMBER=(case
    when a.carrier_item_type in ('ServicePackage','Service','Connection') then 10
    else 20
    end)
    and a.carrier_items not in (select carried_items from CST_ASIR.HIERARCHY_asir where carried_item_type in('Connection') ))A
    where not exists
    (select *
    from CST_DSA.HIERARCHY_MISMATCHES B where
    A.REPORTNUMBER=B.REPORT_NUM and
    A.REPORTTYPE=B.REPORT_TYPE and
    A.REPORTDESCRIPTION=B.REPORT_DESC and
    A.CARRIED_ITEMS=B.CARRIED_ITEMS and
    A.CARRIED_ITEM_TYPE=B.CARRIED_ITEM_TYPE and
    A.NO_OF_ROUTE_OF_CARRIED_ITEM=B.NO_OF_ROUTE_OF_CARRIED_ITEM and
    A.CARRIED_ITEM_ROUTE_NO=B.CARRIED_ITEM_ROUTE_NO and
    A.CARRIER_ITEMS=B.CARRIER_ITEMS and
    A.CARRIER_ITEM_TYPE=B.CARRIER_ITEM_TYPE and
    A.CARRIED_ITEM_PROTECTION_TYPE=B.CARRIED_ITEM_PROTECTION_TYPE
    AND B.SOURCE_SYSTEM='PACS'
    Explain Plan
    ==========
    Plan
    INSERT STATEMENT ALL_ROWSCost: 129 Bytes: 1,103 Cardinality: 1                                                        
         20 LOAD AS SELECT CST_DSA.HIERARCHY_MISMATCHES                                                   
              19 PX COORDINATOR                                              
                   18 PX SEND QC (RANDOM) PARALLEL_TO_SERIAL SYS.:TQ10002 :Q1002Cost: 129 Bytes: 1,103 Cardinality: 1                                         
                        17 NESTED LOOPS PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 129 Bytes: 1,103 Cardinality: 1                                    
                             15 HASH JOIN RIGHT ANTI NA PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 129 Bytes: 1,098 Cardinality: 1                               
                                  4 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 63 Bytes: 359,283 Cardinality: 15,621                          
                                       3 PX SEND BROADCAST PARALLEL_TO_PARALLEL SYS.:TQ10001 :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621                     
                                            2 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621                
                                                 1 MAT_VIEW ACCESS FULL MAT_VIEW PARALLEL_COMBINED_WITH_PARENT CST_ASIR.HIERARCHY :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621           
                                  14 NESTED LOOPS ANTI PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 65 Bytes: 40,256,600 Cardinality: 37,448                          
                                       11 HASH JOIN PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 65 Bytes: 6,366,160 Cardinality: 37,448                     
                                            8 BUFFER SORT PARALLEL_COMBINED_WITH_CHILD :Q1002               
                                                 7 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 1 Bytes: 214 Cardinality: 2           
                                                      6 PX SEND BROADCAST PARALLEL_FROM_SERIAL SYS.:TQ10000 Cost: 1 Bytes: 214 Cardinality: 2      
                                                           5 INDEX FULL SCAN INDEX CST_DSA.IDX$$_06EF0005 Cost: 1 Bytes: 214 Cardinality: 2
                                            10 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1002Cost: 63 Bytes: 2,359,224 Cardinality: 37,448                
                                                 9 MAT_VIEW ACCESS FULL MAT_VIEW PARALLEL_COMBINED_WITH_PARENT CST_ASIR.HIERARCHY :Q1002Cost: 63 Bytes: 2,359,224 Cardinality: 37,448           
                                       13 TABLE ACCESS BY INDEX ROWID TABLE PARALLEL_COMBINED_WITH_PARENT CST_DSA.HIERARCHY_MISMATCHES :Q1002Cost: 0 Bytes: 905 Cardinality: 1                     
                                            12 INDEX RANGE SCAN INDEX PARALLEL_COMBINED_WITH_PARENT SYS.HIERARCHY_MISMATCHES_IDX3 :Q1002Cost: 0 Cardinality: 1                
                             16 INDEX RANGE SCAN INDEX PARALLEL_COMBINED_WITH_PARENT CST_DSA.IDX$$_06EF0001 :Q1002Cost: 1 Bytes: 5 Cardinality: 1

  • Sql query slow due to case statement on Joins

    Hi
    The sql query runs very slow for 30 min when the below case statement is added on the joins. Could you please let me know how to tune it. if the case statement is not there then it runs only for 1 min.
    *( CASE*
    WHEN PO_DIST_GL_CODE_COMB.SEGMENT2 <> '1000'
    THEN  PO_DIST_GL_CODE_COMB.SEGMENT1 || PO_DIST_GL_CODE_COMB.SEGMENT2 || '_' || NVL(PO_DIST_GL_CODE_COMB.SEGMENT6,'000')
    WHEN DT_REQ_ALL.EMPMGMTCD IS NOT NULL AND
    PO_DIST_GL_CODE_COMB.SEGMENT2 = '1000'
    THEN DT_REQ_ALL.EMPMGMTCD
    END =DB2.DB2_FDW_MGMT_V.MH_CHILD  )
    SELECT  DISTINCT
      D.DB2_FDW_MGMT_V.RC_PARENT,
      DT_REQ_ALL.FULL_NAME,
      DT_REQ_ALL.EMP_COMPANY_CODE,
      DT_REQ_ALL.EMP_COST_CENTER,
      PO.PO_VENDORS.VENDOR_NAME,
      PO_PO_HEADERS_ALL2.SEGMENT1,
      PO_PO_HEADERS_ALL2.CREATION_DATE,
      PO_DIST_GL_CODE_COMB.SEGMENT1,
      PO_DIST_GL_CODE_COMB.SEGMENT2,
      PO_PO_HEADERS_ALL2.CURRENCY_CODE,
      PO_INV_DIST_ALL.INVOICE_NUM,
      PO_INV_DIST_ALL.INVOICE_DATE,
      (PO_INV_DIST_ALL.INVOICE_AMOUNT* PO_Rates_GL_DR.CONVERSION_RATE),
      (NVL(to_number(PO_DIST_ALL.AMOUNT_BILLED),0) * PO_Rates_GL_DR.CONVERSION_RATE),
      PO_LINES_LOC.LINE_NUM,
      GL.GL_SETS_OF_BOOKS.NAME,
      CASE
            WHEN TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE) > PO_INV_DIST_ALL.INVOICE_DATE
            THEN 1
            ELSE 0
        END ,
      PO.PO_REQUISITION_LINES_ALL.LINE_LOCATION_ID,
      TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE,'WW') + 8 WEEK_Ending
    FROM
      DB2.DB2_FDW_MGMT_V,
       PO.PO_VENDORS,
      PO.PO_HEADERS_ALL  PO_PO_HEADERS_ALL2,
      GL.GL_CODE_COMBINATIONS  PO_DIST_GL_CODE_COMB,
      AP.AP_INVOICES_ALL  PO_INV_DIST_ALL,
       PO.PO_DISTRIBUTIONS_ALL  PO_DIST_ALL,
      PO.PO_LINES_ALL  PO_LINES_LOC,
      GL.GL_SETS_OF_BOOKS,
      PO.PO_REQUISITION_LINES_ALL,
      PO.PO_LINE_LOCATIONS_ALL,
      AP.AP_INVOICE_DISTRIBUTIONS_ALL  PO_DIST_INV_DIST_ALL,
      APPS.HR_OPERATING_UNITS,
      PO.PO_REQ_DISTRIBUTIONS_ALL,
       SELECT DISTINCT
                            PO_RDA.DISTRIBUTION_ID,
                            PO_RLA.requisition_line_id,
                            PO_RHA.DESCRIPTION PO_Descr,
                            PO_RHA.NOTE_TO_AUTHORIZER PO_Justification,
                            Req_Emp.FULL_NAME,
                            GL_CC.SEGMENT1         Req_Company_Code,
                            GL_CC.SEGMENT2         Req_Cost_Center,
                            Req_Emp_CC.SEGMENT1    Emp_Company_Code,
                            Req_Emp_CC.SEGMENT2    Emp_Cost_Center,
                            (Case
                            When GL_CC.SEGMENT2 <> 8000
                            Then TRUNC(GL_CC.SEGMENT1) || TRUNC(GL_CC.SEGMENT2) || '_' || NVL(GL_CC.SEGMENT6,'000')
                            Else TRUNC(Req_Emp_CC.SEGMENT1) || TRUNC(Req_Emp_CC.SEGMENT2) || '_' || NVL(Req_Emp_CC.SEGMENT6,'000')
                            End) EmpMgmtCD
                FROM
                            PO.po_requisition_lines_all PO_rla,
                            PO.po_requisition_headers_all PO_rha,
                            PO.PO_REQ_DISTRIBUTIONS_ALL po_RDA,
                            GL.GL_CODE_COMBINATIONS gl_cc,
                            HR.PER_ALL_PEOPLE_F  Req_Emp,
                            HR.PER_ALL_ASSIGNMENTS_F Req_Emp_Assign,
                            HR.hr_all_organization_units Req_Emp_Org,
                            HR.pay_cost_allocation_keyflex Req_Emp_CC
                WHERE
                            PO_RDA.CODE_COMBINATION_ID = GL_CC.CODE_COMBINATION_ID and
                            PO_RLA.REQUISITION_LINE_ID = PO_RDA.REQUISITION_LINE_ID AND
                            PO_RLA.to_person_id = Req_Emp.PERSON_ID AND
                            PO_RLA.REQUISITION_HEADER_ID = PO_RHA.REQUISITION_HEADER_ID AND
                            (trunc(PO_rla.CREATION_DATE) between Req_Emp.effective_start_date and Req_Emp.effective_end_date OR
                            Req_Emp.effective_start_date IS NULL) AND
                            Req_Emp.PERSON_ID = Req_Emp_Assign.PERSON_ID AND
                            Req_Emp_Assign.organization_id = Req_Emp_Org.organization_id AND
                            (trunc(PO_rla.CREATION_DATE) between Req_Emp_Assign.effective_start_date and Req_Emp_Assign.effective_end_date OR
            Req_Emp_Assign.effective_start_date IS NULL) AND
            Req_Emp_Assign.primary_flag = 'Y' AND
            Req_Emp_Assign.assignment_type = 'E' AND
            Req_Emp_Org.cost_allocation_keyflex_id = Req_Emp_CC.cost_allocation_keyflex_id
      )  DT_REQ_ALL,
      SELECT
            FROM_CURRENCY,
            TO_CURRENCY,
            CONVERSION_DATE,
            CONVERSION_RATE
        FROM GL.GL_DAILY_RATES
        UNION
        SELECT Distinct
            'USD',
            'USD',
            CONVERSION_DATE,
            1
        FROM GL.GL_DAILY_RATES
      )  PO_Rates_GL_DR
    WHERE
      ( PO_DIST_GL_CODE_COMB.CODE_COMBINATION_ID=PO_DIST_ALL.CODE_COMBINATION_ID  )
      AND  ( PO_DIST_ALL.LINE_LOCATION_ID=PO.PO_LINE_LOCATIONS_ALL.LINE_LOCATION_ID  )
      AND  ( PO_PO_HEADERS_ALL2.VENDOR_ID=PO.PO_VENDORS.VENDOR_ID  )
      AND  ( PO_PO_HEADERS_ALL2.ORG_ID=APPS.HR_OPERATING_UNITS.ORGANIZATION_ID  )
      AND  ( GL.GL_SETS_OF_BOOKS.SET_OF_BOOKS_ID=APPS.HR_OPERATING_UNITS.SET_OF_BOOKS_ID  )
      AND  ( PO_PO_HEADERS_ALL2.CURRENCY_CODE=PO_Rates_GL_DR.FROM_CURRENCY  )
      AND  ( trunc(PO_PO_HEADERS_ALL2.CREATION_DATE)=PO_Rates_GL_DR.CONVERSION_DATE  )
      AND  ( PO_DIST_ALL.REQ_DISTRIBUTION_ID=PO.PO_REQ_DISTRIBUTIONS_ALL.DISTRIBUTION_ID(+)  )
      AND  ( PO.PO_REQ_DISTRIBUTIONS_ALL.REQUISITION_LINE_ID=PO.PO_REQUISITION_LINES_ALL.REQUISITION_LINE_ID(+)  )
      AND  ( PO_LINES_LOC.PO_HEADER_ID=PO_PO_HEADERS_ALL2.PO_HEADER_ID  )
      AND  ( PO.PO_LINE_LOCATIONS_ALL.PO_LINE_ID=PO_LINES_LOC.PO_LINE_ID  )
      AND  ( PO_DIST_ALL.REQ_DISTRIBUTION_ID=DT_REQ_ALL.DISTRIBUTION_ID(+)  )
      AND  ( PO_DIST_ALL.PO_DISTRIBUTION_ID=PO_DIST_INV_DIST_ALL.PO_DISTRIBUTION_ID(+)  )
      AND  ( PO_INV_DIST_ALL.INVOICE_ID(+)=PO_DIST_INV_DIST_ALL.INVOICE_ID  )
      AND  ( PO_INV_DIST_ALL.SOURCE(+) <> 'XML GATEWAY'   )
      AND 
       ( NVL(PO_PO_HEADERS_ALL2.CANCEL_FLAG,'N') <> 'Y'   )
       AND
       ( NVL(PO_PO_HEADERS_ALL2.CLOSED_CODE, 'OPEN') <> 'FINALLY CLOSED'  )
       AND
       ( NVL(PO_PO_HEADERS_ALL2.AUTHORIZATION_STATUS,'IN PROCESS') <> 'REJECTED'  )
       AND
       ( TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE)  BETWEEN TO_DATE('01-jan-2011') AND TO_DATE('04-jan-2011')  )
       AND
       PO_Rates_GL_DR.TO_CURRENCY  =  'USD'
         AND
       DB2.DB2_FDW_MGMT_V.RC_PARENT  In  ( 'Unavailable','Corp','Commercial'  )
       AND
      ( CASE
            WHEN PO_DIST_GL_CODE_COMB.SEGMENT2 <> '1000'
            THEN  PO_DIST_GL_CODE_COMB.SEGMENT1 || PO_DIST_GL_CODE_COMB.SEGMENT2 || '_' || NVL(PO_DIST_GL_CODE_COMB.SEGMENT6,'000')
            WHEN DT_REQ_ALL.EMPMGMTCD IS NOT NULL AND
                    PO_DIST_GL_CODE_COMB.SEGMENT2 = '1000'
            THEN DT_REQ_ALL.EMPMGMTCD     
        END =DB2.DB2_FDW_MGMT_V.MH_CHILD  )Explain plan. sorry can't get the explain plan from sql. this is from toad.
    Plan
    SELECT STATEMENT  ALL_ROWSCost: 53,932  Bytes: 2,607  Cardinality: 1                                                                                                                               
         79 HASH UNIQUE  Cost: 53,932  Bytes: 2,607  Cardinality: 1                                                                                                                          
              78 NESTED LOOPS OUTER  Cost: 53,931  Bytes: 2,607  Cardinality: 1                                                                                                                     
                   75 NESTED LOOPS OUTER  Cost: 53,928  Bytes: 2,560  Cardinality: 1                                                                                                                
                        72 NESTED LOOPS  Cost: 53,902  Bytes: 2,552  Cardinality: 1                                                                                                           
                             69 NESTED LOOPS OUTER  Cost: 53,900  Bytes: 2,533  Cardinality: 1                                                                                                      
                                  66 NESTED LOOPS OUTER  Cost: 53,898  Bytes: 2,521  Cardinality: 1                                                                                                 
                                       63 HASH JOIN OUTER  Cost: 53,896  Bytes: 2,509  Cardinality: 1                                                                                            
                                            40 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_DISTRIBUTIONS_ALL Cost: 3  Bytes: 26  Cardinality: 1                                                                                       
                                                 39 NESTED LOOPS  Cost: 17,076  Bytes: 2,400  Cardinality: 1                                                                                  
                                                      37 NESTED LOOPS  Cost: 17,073  Bytes: 2,374  Cardinality: 1                                                                             
                                                           34 NESTED LOOPS  Cost: 17,070  Bytes: 2,362  Cardinality: 1                                                                        
                                                                31 NESTED LOOPS  Cost: 17,066  Bytes: 2,347  Cardinality: 1                                                                   
                                                                     29 NESTED LOOPS  Cost: 17,066  Bytes: 2,339  Cardinality: 1                                                              
                                                                          26 NESTED LOOPS  Cost: 17,065  Bytes: 2,312  Cardinality: 1                                                         
                                                                               23 NESTED LOOPS  Cost: 17,064  Bytes: 2,287  Cardinality: 1                                                    
                                                                                    20 NESTED LOOPS  Cost: 17,062  Bytes: 2,261  Cardinality: 1                                               
                                                                                         17 NESTED LOOPS  Cost: 17,056  Bytes: 6,678  Cardinality: 3                                          
                                                                                              15 HASH JOIN  Cost: 17,056  Bytes: 6,663  Cardinality: 3                                     
                                                                                                   13 MERGE JOIN CARTESIAN  Cost: 135  Bytes: 30,352  Cardinality: 14                                
                                                                                                        5 VIEW VIEW DB2.DB2_FDW_MGMT_V Cost: 4  Bytes: 2,128  Cardinality: 1                           
                                                                                                             4 SORT UNIQUE  Cost: 4  Cardinality: 1                      
                                                                                                                  3 UNION-ALL                 
                                                                                                                       1 REMOTE REMOTE SERIAL_FROM_REMOTE PRDFDW.WORLD          
                                                                                                                       2 FAST DUAL  Cost: 3  Cardinality: 1            
                                                                                                        12 BUFFER SORT  Cost: 135  Bytes: 560  Cardinality: 14                           
                                                                                                             11 VIEW DB2. Cost: 131  Bytes: 560  Cardinality: 14                      
                                                                                                                  10 SORT UNIQUE  Cost: 131  Bytes: 310  Cardinality: 14                 
                                                                                                                       9 UNION-ALL            
                                                                                                                            7 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_DAILY_RATES Cost: 65  Bytes: 270  Cardinality: 9       
                                                                                                                                 6 INDEX SKIP SCAN INDEX (UNIQUE) GL.GL_DAILY_RATES_U1 Cost: 64  Cardinality: 1 
                                                                                                                            8 INDEX SKIP SCAN INDEX (UNIQUE) GL.GL_DAILY_RATES_U1 Cost: 64  Bytes: 4,368  Cardinality: 546       
                                                                                                   14 TABLE ACCESS FULL TABLE PO.PO_HEADERS_ALL Cost: 16,920  Bytes: 32,754  Cardinality: 618                                
                                                                                              16 INDEX UNIQUE SCAN INDEX (UNIQUE) HR.HR_ORGANIZATION_UNITS_PK Cost: 0  Bytes: 5  Cardinality: 1                                     
                                                                                         19 TABLE ACCESS BY INDEX ROWID TABLE HR.HR_ORGANIZATION_INFORMATION Cost: 2  Bytes: 35  Cardinality: 1                                          
                                                                                              18 INDEX RANGE SCAN INDEX HR.HR_ORGANIZATION_INFORMATIO_FK2 Cost: 1  Cardinality: 2                                     
                                                                                    22 TABLE ACCESS BY INDEX ROWID TABLE HR.HR_ORGANIZATION_INFORMATION Cost: 2  Bytes: 26  Cardinality: 1                                               
                                                                                         21 INDEX RANGE SCAN INDEX HR.HR_ORGANIZATION_INFORMATIO_FK2 Cost: 1  Cardinality: 1                                          
                                                                               25 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_SETS_OF_BOOKS Cost: 1  Bytes: 25  Cardinality: 1                                                    
                                                                                    24 INDEX UNIQUE SCAN INDEX (UNIQUE) GL.GL_SETS_OF_BOOKS_U2 Cost: 0  Cardinality: 1                                               
                                                                          28 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_VENDORS Cost: 1  Bytes: 27  Cardinality: 1                                                         
                                                                               27 INDEX UNIQUE SCAN INDEX (UNIQUE) PO.PO_VENDORS_U1 Cost: 0  Cardinality: 1                                                    
                                                                     30 INDEX UNIQUE SCAN INDEX (UNIQUE) HR.HR_ALL_ORGANIZATION_UNTS_TL_PK Cost: 0  Bytes: 8  Cardinality: 1                                                              
                                                                33 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_LINES_ALL Cost: 4  Bytes: 60  Cardinality: 4                                                                   
                                                                     32 INDEX RANGE SCAN INDEX (UNIQUE) PO.PO_LINES_U2 Cost: 2  Cardinality: 4                                                              
                                                           36 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_LINE_LOCATIONS_ALL Cost: 3  Bytes: 12  Cardinality: 1                                                                        
                                                                35 INDEX RANGE SCAN INDEX PO.PO_LINE_LOCATIONS_N1 Cost: 2  Cardinality: 1                                                                   
                                                      38 INDEX RANGE SCAN INDEX PO.PO_DISTRIBUTIONS_N1 Cost: 2  Cardinality: 1                                                                             
                                            62 VIEW DB2. Cost: 36,819  Bytes: 1,090  Cardinality: 10                                                                                       
                                                 61 HASH UNIQUE  Cost: 36,819  Bytes: 2,580  Cardinality: 10                                                                                  
                                                      60 NESTED LOOPS  Cost: 36,818  Bytes: 2,580  Cardinality: 10                                                                             
                                                           57 NESTED LOOPS  Cost: 36,798  Bytes: 2,390  Cardinality: 10                                                                        
                                                                54 NESTED LOOPS  Cost: 36,768  Bytes: 2,220  Cardinality: 10                                                                   
                                                                     51 NESTED LOOPS  Cost: 36,758  Bytes: 1,510  Cardinality: 10                                                              
                                                                          48 NESTED LOOPS  Cost: 36,747  Bytes: 1,050  Cardinality: 10                                                         
                                                                               45 HASH JOIN  Cost: 36,737  Bytes: 960  Cardinality: 10                                                    
                                                                                    43 HASH JOIN  Cost: 34,602  Bytes: 230,340  Cardinality: 3,490                                               
                                                                                         41 TABLE ACCESS FULL TABLE HR.PER_ALL_PEOPLE_F Cost: 1,284  Bytes: 1,848,420  Cardinality: 44,010                                          
                                                                                         42 TABLE ACCESS FULL TABLE PO.PO_REQUISITION_LINES_ALL Cost: 31,802  Bytes: 18,340,080  Cardinality: 764,170                                          
                                                                                    44 TABLE ACCESS FULL TABLE HR.PER_ALL_ASSIGNMENTS_F Cost: 2,134  Bytes: 822,540  Cardinality: 27,418                                               
                                                                               47 TABLE ACCESS BY INDEX ROWID TABLE HR.HR_ALL_ORGANIZATION_UNITS Cost: 1  Bytes: 9  Cardinality: 1                                                    
                                                                                    46 INDEX UNIQUE SCAN INDEX (UNIQUE) HR.HR_ORGANIZATION_UNITS_PK Cost: 0  Cardinality: 1                                               
                                                                          50 TABLE ACCESS BY INDEX ROWID TABLE HR.PAY_COST_ALLOCATION_KEYFLEX Cost: 1  Bytes: 46  Cardinality: 1                                                         
                                                                               49 INDEX UNIQUE SCAN INDEX (UNIQUE) HR.PAY_COST_ALLOCATION_KEYFLE_PK Cost: 0  Cardinality: 1                                                    
                                                                     53 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_REQUISITION_HEADERS_ALL Cost: 1  Bytes: 71  Cardinality: 1                                                              
                                                                          52 INDEX UNIQUE SCAN INDEX (UNIQUE) PO.PO_REQUISITION_HEADERS_U1 Cost: 0  Cardinality: 1                                                         
                                                                56 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_REQ_DISTRIBUTIONS_ALL Cost: 3  Bytes: 17  Cardinality: 1                                                                   
                                                                     55 INDEX RANGE SCAN INDEX PO.PO_REQ_DISTRIBUTIONS_N1 Cost: 2  Cardinality: 1                                                              
                                                           59 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_CODE_COMBINATIONS Cost: 2  Bytes: 19  Cardinality: 1                                                                        
                                                                58 INDEX UNIQUE SCAN INDEX (UNIQUE) GL.GL_CODE_COMBINATIONS_U1 Cost: 1  Cardinality: 1                                                                   
                                       65 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_REQ_DISTRIBUTIONS_ALL Cost: 2  Bytes: 12  Cardinality: 1                                                                                            
                                            64 INDEX UNIQUE SCAN INDEX (UNIQUE) PO.PO_REQ_DISTRIBUTIONS_U1 Cost: 1  Cardinality: 1                                                                                       
                                  68 TABLE ACCESS BY INDEX ROWID TABLE PO.PO_REQUISITION_LINES_ALL Cost: 2  Bytes: 12  Cardinality: 1                                                                                                 
                                       67 INDEX UNIQUE SCAN INDEX (UNIQUE) PO.PO_REQUISITION_LINES_U1 Cost: 1  Cardinality: 1                                                                                            
                             71 TABLE ACCESS BY INDEX ROWID TABLE GL.GL_CODE_COMBINATIONS Cost: 2  Bytes: 19  Cardinality: 1                                                                                                      
                                  70 INDEX UNIQUE SCAN INDEX (UNIQUE) GL.GL_CODE_COMBINATIONS_U1 Cost: 1  Cardinality: 1                                                                                                 
                        74 TABLE ACCESS BY INDEX ROWID TABLE AP.AP_INVOICE_DISTRIBUTIONS_ALL Cost: 26  Bytes: 16  Cardinality: 2                                                                                                           
                             73 INDEX RANGE SCAN INDEX AP.AP_INVOICE_DISTRIBUTIONS_N7 Cost: 2  Cardinality: 37                                                                                                      
                   77 TABLE ACCESS BY INDEX ROWID TABLE AP.AP_INVOICES_ALL Cost: 3  Bytes: 47  Cardinality: 1                                                                                                                
                        76 INDEX RANGE SCAN INDEX (UNIQUE) AP.AP_INVOICES_U1 Cost: 2  Cardinality: 1                                                                                                           Thanks

    Forming a new table "new_table" with 3 tables which particiapate in CASE statement logic.
    with  DT_REQ_ALL  as
       SELECT DISTINCT
                            PO_RDA.DISTRIBUTION_ID,
                            PO_RLA.requisition_line_id,
                            PO_RHA.DESCRIPTION PO_Descr,
                            PO_RHA.NOTE_TO_AUTHORIZER PO_Justification,
                            Req_Emp.FULL_NAME,
                            GL_CC.SEGMENT1         Req_Company_Code,
                            GL_CC.SEGMENT2         Req_Cost_Center,
                            Req_Emp_CC.SEGMENT1    Emp_Company_Code,
                            Req_Emp_CC.SEGMENT2    Emp_Cost_Center,
                            (Case
                            When GL_CC.SEGMENT2  8000
                            Then TRUNC(GL_CC.SEGMENT1) || TRUNC(GL_CC.SEGMENT2) || '_' || NVL(GL_CC.SEGMENT6,'000')
                            Else TRUNC(Req_Emp_CC.SEGMENT1) || TRUNC(Req_Emp_CC.SEGMENT2) || '_' || NVL(Req_Emp_CC.SEGMENT6,'000')
                            End) EmpMgmtCD
                FROM
                            PO.po_requisition_lines_all PO_rla,
                            PO.po_requisition_headers_all PO_rha,
                            PO.PO_REQ_DISTRIBUTIONS_ALL po_RDA,
                            GL.GL_CODE_COMBINATIONS gl_cc,
                            HR.PER_ALL_PEOPLE_F  Req_Emp,
                            HR.PER_ALL_ASSIGNMENTS_F Req_Emp_Assign,
                            HR.hr_all_organization_units Req_Emp_Org,
                            HR.pay_cost_allocation_keyflex Req_Emp_CC
                WHERE
                            PO_RDA.CODE_COMBINATION_ID = GL_CC.CODE_COMBINATION_ID and
                            PO_RLA.REQUISITION_LINE_ID = PO_RDA.REQUISITION_LINE_ID AND
                            PO_RLA.to_person_id = Req_Emp.PERSON_ID AND
                            PO_RLA.REQUISITION_HEADER_ID = PO_RHA.REQUISITION_HEADER_ID AND
                            (trunc(PO_rla.CREATION_DATE) between Req_Emp.effective_start_date and Req_Emp.effective_end_date OR
                            Req_Emp.effective_start_date IS NULL) AND
                            Req_Emp.PERSON_ID = Req_Emp_Assign.PERSON_ID AND
                            Req_Emp_Assign.organization_id = Req_Emp_Org.organization_id AND
                            (trunc(PO_rla.CREATION_DATE) between Req_Emp_Assign.effective_start_date and Req_Emp_Assign.effective_end_date OR
            Req_Emp_Assign.effective_start_date IS NULL) AND
            Req_Emp_Assign.primary_flag = 'Y' AND
            Req_Emp_Assign.assignment_type = 'E' AND
            Req_Emp_Org.cost_allocation_keyflex_id = Req_Emp_CC.cost_allocation_keyflex_id
    SELECT  DISTINCT
      D.DB2_FDW_MGMT_V.RC_PARENT,
      DT_REQ_ALL.FULL_NAME,
      DT_REQ_ALL.EMP_COMPANY_CODE,
      DT_REQ_ALL.EMP_COST_CENTER,
      PO.PO_VENDORS.VENDOR_NAME,
      PO_PO_HEADERS_ALL2.SEGMENT1,
      PO_PO_HEADERS_ALL2.CREATION_DATE,
      PO_DIST_GL_CODE_COMB.SEGMENT1,
      PO_DIST_GL_CODE_COMB.SEGMENT2,
      PO_PO_HEADERS_ALL2.CURRENCY_CODE,
      PO_INV_DIST_ALL.INVOICE_NUM,
      PO_INV_DIST_ALL.INVOICE_DATE,
      (PO_INV_DIST_ALL.INVOICE_AMOUNT* PO_Rates_GL_DR.CONVERSION_RATE),
      (NVL(to_number(PO_DIST_ALL.AMOUNT_BILLED),0) * PO_Rates_GL_DR.CONVERSION_RATE),
      PO_LINES_LOC.LINE_NUM,
      GL.GL_SETS_OF_BOOKS.NAME,
      CASE
            WHEN TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE) > PO_INV_DIST_ALL.INVOICE_DATE
            THEN 1
            ELSE 0
        END ,
      PO.PO_REQUISITION_LINES_ALL.LINE_LOCATION_ID,
      TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE,'WW') + 8 WEEK_Ending
    FROM
      ( SELECT * FROM
          DB2.DB2_FDW_MGMT_V,
          GL.GL_CODE_COMBINATIONS  PO_DIST_GL_CODE_COMB,
          DT_REQ_ALL
        WHERE
              DB2.DB2_FDW_MGMT_V.RC_PARENT  In  ( 'Unavailable','Corp','Commercial'  )
           AND
            CASE
               WHEN PO_DIST_GL_CODE_COMB.SEGMENT2  <>  '1000'
               THEN  PO_DIST_GL_CODE_COMB.SEGMENT1 || PO_DIST_GL_CODE_COMB.SEGMENT2 || '_' || NVL(PO_DIST_GL_CODE_COMB.SEGMENT6,'000')
               WHEN DT_REQ_ALL.EMPMGMTCD IS NOT NULL AND
                    PO_DIST_GL_CODE_COMB.SEGMENT2 = '1000'
               THEN DT_REQ_ALL.EMPMGMTCD     
            END =DB2.DB2_FDW_MGMT_V.MH_CHILD
       )   new_table,
       PO.PO_VENDORS,
      PO.PO_HEADERS_ALL  PO_PO_HEADERS_ALL2,
      AP.AP_INVOICES_ALL  PO_INV_DIST_ALL,
       PO.PO_DISTRIBUTIONS_ALL  PO_DIST_ALL,
      PO.PO_LINES_ALL  PO_LINES_LOC,
      GL.GL_SETS_OF_BOOKS,
      PO.PO_REQUISITION_LINES_ALL,
      PO.PO_LINE_LOCATIONS_ALL,
      AP.AP_INVOICE_DISTRIBUTIONS_ALL  PO_DIST_INV_DIST_ALL,
      APPS.HR_OPERATING_UNITS,
      PO.PO_REQ_DISTRIBUTIONS_ALL,
      SELECT
            FROM_CURRENCY,
            TO_CURRENCY,
            CONVERSION_DATE,
            CONVERSION_RATE
        FROM GL.GL_DAILY_RATES
        UNION
        SELECT Distinct
            'USD',
            'USD',
            CONVERSION_DATE,
            1
        FROM GL.GL_DAILY_RATES
      )  PO_Rates_GL_DR
    WHERE
      ( PO_DIST_GL_CODE_COMB.CODE_COMBINATION_ID=PO_DIST_ALL.CODE_COMBINATION_ID  )
      AND  ( PO_DIST_ALL.LINE_LOCATION_ID=PO.PO_LINE_LOCATIONS_ALL.LINE_LOCATION_ID  )
      AND  ( PO_PO_HEADERS_ALL2.VENDOR_ID=PO.PO_VENDORS.VENDOR_ID  )
      AND  ( PO_PO_HEADERS_ALL2.ORG_ID=APPS.HR_OPERATING_UNITS.ORGANIZATION_ID  )
      AND  ( GL.GL_SETS_OF_BOOKS.SET_OF_BOOKS_ID=APPS.HR_OPERATING_UNITS.SET_OF_BOOKS_ID  )
      AND  ( PO_PO_HEADERS_ALL2.CURRENCY_CODE=PO_Rates_GL_DR.FROM_CURRENCY  )
      AND  ( trunc(PO_PO_HEADERS_ALL2.CREATION_DATE)=PO_Rates_GL_DR.CONVERSION_DATE  )
      AND  ( PO_DIST_ALL.REQ_DISTRIBUTION_ID=PO.PO_REQ_DISTRIBUTIONS_ALL.DISTRIBUTION_ID(+)  )
      AND  ( PO.PO_REQ_DISTRIBUTIONS_ALL.REQUISITION_LINE_ID=PO.PO_REQUISITION_LINES_ALL.REQUISITION_LINE_ID(+)  )
      AND  ( PO_LINES_LOC.PO_HEADER_ID=PO_PO_HEADERS_ALL2.PO_HEADER_ID  )
      AND  ( PO.PO_LINE_LOCATIONS_ALL.PO_LINE_ID=PO_LINES_LOC.PO_LINE_ID  )
      AND  ( PO_DIST_ALL.REQ_DISTRIBUTION_ID=DT_REQ_ALL.DISTRIBUTION_ID(+)  )
      AND  ( PO_DIST_ALL.PO_DISTRIBUTION_ID=PO_DIST_INV_DIST_ALL.PO_DISTRIBUTION_ID(+)  )
      AND  ( PO_INV_DIST_ALL.INVOICE_ID(+)=PO_DIST_INV_DIST_ALL.INVOICE_ID  )
      AND  ( PO_INV_DIST_ALL.SOURCE(+)  'XML GATEWAY'   )
      AND 
       ( NVL(PO_PO_HEADERS_ALL2.CANCEL_FLAG,'N')  'Y'   )
       AND
       ( NVL(PO_PO_HEADERS_ALL2.CLOSED_CODE, 'OPEN')  'FINALLY CLOSED'  )
       AND
       ( NVL(PO_PO_HEADERS_ALL2.AUTHORIZATION_STATUS,'IN PROCESS')  'REJECTED'  )
       AND
       ( TRUNC(PO_PO_HEADERS_ALL2.CREATION_DATE)  BETWEEN TO_DATE('01-jan-2011') AND TO_DATE('04-jan-2011')  )
       AND
       PO_Rates_GL_DR.TO_CURRENCY  =  'USD'
      

Maybe you are looking for