Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Jason.

Asked: August 30, 2017 - 8:55 pm UTC

Last updated: September 19, 2017 - 2:58 am UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

Hi

I have the following query:

SELECT cust_id,
             cosmos_cust_id,
             prot_id,
             customer_name,
             protocol_name,
             dist_order_no,
             cust_ivr_ref_no,
             ship_to_country,
             ship_date,
             shipment_date,
             shipped_mntr_cnt,
             upload_mntr_cnt,
             otg_state,
             ship_otg,
             ship_status,
             ship_to_site_no,
             ship_fac_name,
             ship_fac_id,
             tempez_status,
             monitors_alarmed,
             excursion_status,
             excursion_decision,
             ship_source,
             username AS locking_user,
             courier_name,
             courier_id
        FROM    shipment_landingpage_dtls_v
             LEFT JOIN
                (SELECT SUBSTR (lock_id,
                                REGEXP_INSTR (lock_id,
                                              '[^SHIPMENTS_]{1}',
                                              1,
                                              1,
                                              0,
                                              'i'))
                           AS locking_ship,
                        l.username AS username
                   FROM application_lock l
                  WHERE     SUBSTR (UPPER (lock_id), 1, 10) = 'SHIPMENTS_'
                        AND SYSDATE <
                                 lock_time
                               + .0138888888888888888888888888888888888889) a
             ON '' || ship_id = a.locking_ship(+)
       WHERE     1 = 1
             AND ship_id IN
                    (SELECT s.ship_id
                       FROM monitor m
                            INNER JOIN container c
                               ON c.ctr_id = m.mon_ctr_id
                            INNER JOIN shipment s
                               ON s.ship_id = c.ctr_ship_id
                      WHERE     m.deactivated IS NULL
                            AND c.deactivated IS NULL
                            AND s.deactivated IS NULL
                            AND m.deactivated IS NULL
                            AND LOWER (m.mon_serial_number) =
                                   LOWER ('2703743130'))


When I run the subquery in isolation, it takes millseconds and returns a ship id. If I hardcode the ship id value (instead of using the subquery), the overall query returns in milliseconds. Yet when I run the overall query (including the subquery, it takes 3 minutes).

Am I being naïve when I think that the overall query time should be query + subquery?

and Connor said...

Change

SELECT cust_id,
             cosmos_cust_id,
             prot_id,
             customer_name,
             protocol_name,
             dist_order_no,
 ...


to this

SELECT /*+ gather_plan_statistics */ cust_id,
             cosmos_cust_id,
             prot_id,
             customer_name,
             protocol_name,
             dist_order_no,
...


then run your query in SQL Plus and then do:

select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'))

then paste that in as a review (in code tags to keep the spacing)

Then we can see the cardinalities and help you further.

Rating

  (6 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

DBMS_XPLAN output

Jason Shannon, September 06, 2017 - 2:10 pm UTC

Thank you for looking at this.

Regards

Jason

</>



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                               | Name                        | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  | Writes |  OMem |  1Mem | Used-Mem | Used-Tmp|

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                                        |                             |      1 |        |      1 |00:01:33.97 |     907K|  21281 |  23290 |       |       |          |         |
|*  1 |  HASH JOIN SEMI                                         |                             |      1 |      1 |      1 |00:01:33.97 |     907K|  21281 |  23290 |    23M|  4027K| 7460K (1)|   19456 |
|*  2 |   HASH JOIN OUTER                                       |                             |      1 |      1 |  82282 |00:01:36.86 |     903K|  21064 |  21027 |    24M|  4018K| 7588K (1)|   21504 |
|   3 |    VIEW                                                 | SHIPMENT_LANDINGPAGE_DTLS_V |      1 |      1 |  82282 |00:01:33.02 |     903K|  18584 |  18547 |       |       |          |         |
|   4 |     HASH UNIQUE                                         |                             |      1 |      1 |  82282 |00:01:32.90 |     903K|  18584 |  18547 |    30M|  3914K|    9M (1)|   22528 |
|*  5 |      FILTER                                             |                             |      1 |        |  82282 |00:01:32.14 |     903K|  15856 |  15819 |       |       |          |         |
|   6 |       NESTED LOOPS                                      |                             |      1 |      1 |  82393 |00:01:09.55 |     903K|  15856 |  15819 |       |       |          |         |
|*  7 |        HASH JOIN                                        |                             |      1 |      1 |  82412 |00:00:28.71 |     903K|  15856 |  15819 |    20M|  4106K| 7858K (1)|   17408 |
|*  8 |         HASH JOIN                                       |                             |      1 |      1 |  82412 |00:00:12.96 |     903K|  13779 |  13742 |    20M|  4122K| 7465K (1)|   19456 |
|   9 |          NESTED LOOPS                                   |                             |      1 |      1 |  82412 |00:00:09.44 |     899K|  10389 |  10367 |       |       |          |         |
|  10 |           NESTED LOOPS                                  |                             |      1 |      1 |  82412 |00:00:09.18 |     817K|  10389 |  10367 |       |       |          |         |
|* 11 |            HASH JOIN                                    |                             |      1 |      1 |  82412 |00:00:08.96 |     817K|  10389 |  10367 |    18M|  4206K| 3859K (1)|   18432 |
|* 12 |             HASH JOIN                                   |                             |      1 |      1 |  82412 |00:00:08.65 |     815K|   8157 |   8135 |    16M|  2122K| 3855K (1)|   18432 |
|* 13 |              HASH JOIN                                  |                             |      1 |      1 |  82412 |00:00:07.61 |     812K|   4377 |   4362 |  1483K|  1483K| 1716K (0)|         |
|* 14 |               TABLE ACCESS FULL                         | PROTOCOL                    |      1 |   1678 |   1684 |00:00:00.01 |      22 |      0 |      0 |       |       |          |         |
|* 15 |               HASH JOIN                                 |                             |      1 |    114 |  82412 |00:00:07.50 |     812K|   4377 |   4362 |    13M|  2208K| 3110K (1)|   11264 |
|* 16 |                HASH JOIN                                |                             |      1 |      1 |  82412 |00:00:07.42 |     811K|   3162 |   3147 |    13M|  2221K| 2432K (1)|   12288 |
|  17 |                 NESTED LOOPS                            |                             |      1 |      1 |  82412 |00:00:07.05 |     811K|   1812 |   1797 |       |       |          |         |
|  18 |                  NESTED LOOPS                           |                             |      1 |      1 |  82412 |00:00:06.45 |     736K|   1812 |   1797 |       |       |          |         |
|  19 |                   NESTED LOOPS OUTER                    |                             |      1 |      1 |    102K|00:00:06.31 |     633K|   1812 |   1797 |       |       |          |         |
|* 20 |                    HASH JOIN OUTER                      |                             |      1 |      1 |    102K|00:00:05.68 |     530K|   1812 |   1797 |  8998K|  3001K| 5544K (1)|    6144 |
|  21 |                     NESTED LOOPS                        |                             |      1 |      1 |    102K|00:00:04.82 |     489K|   1107 |   1092 |       |       |          |         |
|  22 |                      NESTED LOOPS                       |                             |      1 |      1 |    104K|00:00:04.51 |     382K|   1107 |   1092 |       |       |          |         |
|  23 |                       VIEW                              | TEMPEZ_STATUS_V             |      1 |      1 |    104K|00:00:04.04 |     281K|   1107 |   1092 |       |       |          |         |
|  24 |                        HASH GROUP BY                    |                             |      1 |      1 |    104K|00:00:03.84 |     281K|   1107 |   1092 |    11M|  2595K| 8591K (1)|    7168 |
|  25 |                         NESTED LOOPS OUTER              |                             |      1 |      1 |    114K|00:00:03.77 |     281K|    270 |    255 |       |       |          |         |
|  26 |                          NESTED LOOPS OUTER             |                             |      1 |      1 |    114K|00:00:02.97 |     210K|    270 |    255 |       |       |          |         |
|  27 |                           NESTED LOOPS OUTER            |                             |      1 |      1 |    114K|00:00:02.12 |     140K|    270 |    255 |       |       |          |         |
|  28 |                            VIEW                         |                             |      1 |      1 |    114K|00:00:01.37 |   70134 |    270 |    255 |       |       |          |         |
|  29 |                             NESTED LOOPS                |                             |      1 |      1 |    114K|00:00:01.31 |   70134 |    270 |    255 |       |       |          |         |
|  30 |                              NESTED LOOPS               |                             |      1 |      1 |    116K|00:00:01.01 |   49267 |    270 |    255 |       |       |          |         |
|* 31 |                               HASH JOIN                 |                             |      1 |      1 |    112K|00:00:00.56 |    3497 |    270 |    255 |  6574K|  2024K| 8741K (0)|         |
|* 32 |                                TABLE ACCESS FULL        | SHIPMENT                    |      1 |    102K|    104K|00:00:00.04 |    1450 |      0 |      0 |       |       |          |         |
|* 33 |                                HASH JOIN                |                             |      1 |    109K|    112K|00:00:00.24 |    2047 |    270 |    255 |  6568K|  2337K| 7954K (1)|    3072 |
|* 34 |                                 TABLE ACCESS FULL       | CONTAINER                   |      1 |    110K|    112K|00:00:00.03 |     597 |      0 |      0 |       |       |          |         |
|  35 |                                 TABLE ACCESS FULL       | SHIPMENT                    |      1 |    102K|    104K|00:00:00.07 |    1450 |      0 |      0 |       |       |          |         |
|* 36 |                               INDEX RANGE SCAN          | MON_CTR_ID_IDX1             |    112K|      1 |    116K|00:00:00.41 |   45770 |      0 |      0 |       |       |          |         |
|* 37 |                              TABLE ACCESS BY INDEX ROWID| MONITOR                     |    116K|      1 |    114K|00:00:00.24 |   20867 |      0 |      0 |       |       |          |         |
|* 38 |                            TABLE ACCESS BY INDEX ROWID  | MONITOR                     |    114K|      1 |    339 |00:00:00.62 |   70347 |      0 |      0 |       |       |          |         |
|* 39 |                             INDEX RANGE SCAN            | MON_CTR_ID_IDX1             |    114K|      1 |    124K|00:00:00.38 |   46024 |      0 |      0 |       |       |          |         |
|* 40 |                           TABLE ACCESS BY INDEX ROWID   | MONITOR                     |    114K|      1 |    114K|00:00:00.58 |   70347 |      0 |      0 |       |       |          |         |
|* 41 |                            INDEX RANGE SCAN             | MON_CTR_ID_IDX1             |    114K|      1 |    124K|00:00:00.37 |   46024 |      0 |      0 |       |       |          |         |
|* 42 |                          TABLE ACCESS BY INDEX ROWID    | MONITOR                     |    114K|      1 |    112K|00:00:00.67 |   70347 |      0 |      0 |       |       |          |         |
|* 43 |                           INDEX RANGE SCAN              | MON_CTR_ID_IDX1             |    114K|      1 |    124K|00:00:00.37 |   46024 |      0 |      0 |       |       |          |         |
|* 44 |                       INDEX UNIQUE SCAN                 | PK_SHIP                     |    104K|      1 |    104K|00:00:00.33 |     100K|      0 |      0 |       |       |          |         |
|* 45 |                      TABLE ACCESS BY INDEX ROWID        | SHIPMENT                    |    104K|      1 |    102K|00:00:00.30 |     107K|      0 |      0 |       |       |          |         |
|  46 |                     VIEW                                | SHIP_EXCUR_DTLS_V           |      1 |    100 |   1099 |00:00:00.26 |   41217 |      0 |      0 |       |       |          |         |
|* 47 |                      FILTER                             |                             |      1 |        |   1099 |00:00:00.25 |   41217 |      0 |      0 |       |       |          |         |
|* 48 |                       HASH JOIN RIGHT OUTER             |                             |      1 |   1998 |   2020 |00:00:00.01 |     517 |      0 |      0 |  1245K|  1245K|  849K (0)|         |
|  49 |                        VIEW                             |                             |      1 |   1422 |   1434 |00:00:00.01 |     474 |      0 |      0 |       |       |          |         |
|  50 |                         NESTED LOOPS                    |                             |      1 |   1422 |   1434 |00:00:00.01 |     474 |      0 |      0 |       |       |          |         |
|* 51 |                          HASH JOIN                      |                             |      1 |   1422 |   1434 |00:00:00.01 |      36 |      0 |      0 |  1301K|  1301K|  475K (0)|         |
|  52 |                           TABLE ACCESS FULL             | EXCURSION_DECISION_TYPE_L   |      1 |      3 |      3 |00:00:00.01 |       6 |      0 |      0 |       |       |          |         |
|  53 |                           TABLE ACCESS FULL             | ADJUDICATION                |      1 |   1896 |   1916 |00:00:00.01 |      30 |      0 |      0 |       |       |          |         |
|* 54 |                          INDEX UNIQUE SCAN              | PK_EXCR                     |   1434 |      1 |   1434 |00:00:00.01 |     438 |      0 |      0 |       |       |          |         |
|* 55 |                        HASH JOIN                        |                             |      1 |   1998 |   2020 |00:00:00.01 |      43 |      0 |      0 |  1451K|  1451K|  609K (0)|         |
|  56 |                         TABLE ACCESS FULL               | EXCURSION_STATUS            |      1 |      7 |      7 |00:00:00.01 |       6 |      0 |      0 |       |       |          |         |
|  57 |                         TABLE ACCESS FULL               | EXCURSION                   |      1 |   1998 |   2020 |00:00:00.01 |      37 |      0 |      0 |       |       |          |         |
|  58 |                       SORT GROUP BY NOSORT              |                             |   1100 |      1 |   1099 |00:00:00.26 |   40700 |      0 |      0 |       |       |          |         |
|* 59 |                        TABLE ACCESS FULL                | EXCURSION                   |   1100 |      2 |   1101 |00:00:00.25 |   40700 |      0 |      0 |       |       |          |         |
|* 60 |                    TABLE ACCESS BY INDEX ROWID          | SHIPMENT_COURIER            |    102K|      1 |    102K|00:00:00.49 |     102K|      0 |      0 |       |       |          |         |
|* 61 |                     INDEX UNIQUE SCAN                   | PK_SC_ID                    |    102K|      1 |    102K|00:00:00.17 |       4 |      0 |      0 |       |       |          |         |
|* 62 |                   INDEX RANGE SCAN                      | COSMOS_SHIP_SUMMARY_IDX1    |    102K|      1 |  82412 |00:00:00.61 |     103K|      0 |      0 |       |       |          |         |
|  63 |                  TABLE ACCESS BY INDEX ROWID            | COSMOS_SHIP_SUMMARY         |  82412 |      1 |  82412 |00:00:00.51 |   75558 |      0 |      0 |       |       |          |         |
|* 64 |                 TABLE ACCESS FULL                       | CUSTOMER                    |      1 |    355 |    355 |00:00:00.01 |      22 |      0 |      0 |       |       |          |         |
|  65 |                VIEW                                     | SHIPPING_FACILITIES_V       |      1 |    562 |    492 |00:00:00.01 |      45 |      0 |      0 |       |       |          |         |
|  66 |                 SORT UNIQUE                             |                             |      1 |    562 |    492 |00:00:00.01 |      45 |      0 |      0 | 59392 | 59392 |53248  (0)|         |
|  67 |                  UNION-ALL                              |                             |      1 |        |    492 |00:00:00.01 |      45 |      0 |      0 |       |       |          |         |
|* 68 |                   HASH JOIN                             |                             |      1 |    110 |     62 |00:00:00.01 |      27 |      0 |      0 |  1599K|  1599K| 1250K (0)|         |
|* 69 |                    HASH JOIN                            |                             |      1 |     66 |     62 |00:00:00.01 |      12 |      0 |      0 |  1969K|  1969K|  810K (0)|         |
|* 70 |                     TABLE ACCESS FULL                   | FACILITY                    |      1 |     62 |     62 |00:00:00.01 |       6 |      0 |      0 |       |       |          |         |
|* 71 |                     TABLE ACCESS FULL                   | FACILITY_LOCATION           |      1 |    126 |    126 |00:00:00.01 |       6 |      0 |      0 |       |       |          |         |
|* 72 |                    VIEW                                 |                             |      1 |    455 |    274 |00:00:00.01 |      15 |      0 |      0 |       |       |          |         |
|* 73 |                     WINDOW SORT PUSHED RANK             |                             |      1 |    455 |    456 |00:00:00.01 |      15 |      0 |      0 | 38912 | 38912 |34816  (0)|         |
|  74 |                      MAT_VIEW ACCESS FULL               | COSMOS_FACILITY_ADDRESS_MVW |      1 |    455 |    456 |00:00:00.01 |      15 |      0 |      0 |       |       |          |         |
|* 75 |                   HASH JOIN                             |                             |      1 |    452 |    430 |00:00:00.01 |      18 |      0 |      0 |  1015K|  1015K| 1286K (0)|         |
|* 76 |                    HASH JOIN                            |                             |      1 |     67 |     64 |00:00:00.01 |      12 |      0 |      0 |  1015K|  1015K| 1239K (0)|         |
|* 77 |                     TABLE ACCESS FULL                   | FACILITY                    |      1 |     64 |     64 |00:00:00.01 |       6 |      0 |      0 |       |       |          |         |
|* 78 |                     TABLE ACCESS FULL                   | FACILITY_LOCATION           |      1 |    126 |    126 |00:00:00.01 |       6 |      0 |      0 |       |       |          |         |
|* 79 |                    TABLE ACCESS FULL                    | PROTOCOL_FACILITY           |      1 |    430 |    430 |00:00:00.01 |       6 |      0 |      0 |       |       |          |         |
|  80 |              VIEW                                       | SHIPMENT_MNTR_CNT_V         |      1 |   5049 |    102K|00:00:00.86 |    3945 |   1548 |   1541 |       |       |          |         |
|* 81 |               FILTER                                    |                             |      1 |        |    102K|00:00:00.79 |    3945 |   1548 |   1541 |       |       |          |         |
|  82 |                HASH GROUP BY                            |                             |      1 |   5049 |    102K|00:00:00.74 |    3945 |   1548 |   1541 |    10M|  2691K| 4212K (1)|    4096 |
|* 83 |                 HASH JOIN RIGHT OUTER                   |                             |      1 |    110K|    113K|00:00:00.51 |    3945 |   1113 |   1106 |  2710K|  2710K| 1538K (0)|         |
|  84 |                  INDEX FAST FULL SCAN                   | MU_MON_ID_IDX1              |      1 |  23010 |  23474 |00:00:00.01 |      77 |      0 |      0 |       |       |          |         |
|* 85 |                  HASH JOIN RIGHT OUTER                  |                             |      1 |    110K|    113K|00:00:00.44 |    3868 |   1113 |   1106 |  2168K|  2168K|  903K (0)|         |
|* 86 |                   TABLE ACCESS FULL                     | MONITOR_UPLOAD              |      1 |   1290 |   1290 |00:00:00.01 |     247 |      0 |      0 |       |       |          |         |
|* 87 |                   HASH JOIN                             |                             |      1 |    110K|    113K|00:00:00.34 |    3621 |   1113 |   1106 |  8917K|  3319K| 3248K (1)|    7168 |
|* 88 |                    TABLE ACCESS FULL                    | MONITOR                     |      1 |    112K|    114K|00:00:00.05 |    1574 |      0 |      0 |       |       |          |         |
|* 89 |                    HASH JOIN                            |                             |      1 |    107K|    110K|00:00:00.17 |    2047 |    378 |    401 |  6568K|  2337K| 4649K (1)|    4096 |
|* 90 |                     TABLE ACCESS FULL                   | CONTAINER                   |      1 |    110K|    112K|00:00:00.03 |     597 |      0 |      0 |       |       |          |         |
|* 91 |                     TABLE ACCESS FULL                   | SHIPMENT                    |      1 |    100K|    102K|00:00:00.04 |    1450 |      0 |      0 |       |       |          |         |
|  92 |             TABLE ACCESS FULL                           | SHIPMENT                    |      1 |    102K|    104K|00:00:00.03 |    1450 |      0 |      0 |       |       |          |         |
|* 93 |            INDEX UNIQUE SCAN                            | PK_SHIP_STAT                |  82412 |      1 |  82412 |00:00:00.13 |       4 |      0 |      0 |       |       |          |         |
|  94 |           TABLE ACCESS BY INDEX ROWID                   | SHIPMENT_STATUS             |  82412 |      1 |  82412 |00:00:00.16 |   82412 |      0 |      0 |       |       |          |         |
|  95 |          VIEW                                           | SHIP_OTG_STATE_V            |      1 |    102K|    104K|00:00:03.22 |    3624 |   1096 |   1081 |       |       |          |         |
|  96 |           SORT GROUP BY                                 |                             |      1 |    102K|    104K|00:00:03.00 |    3624 |   1096 |   1081 |  1377K|   596K| 3889K (1)|    2048 |
|  97 |            VIEW                                         |                             |      1 |    104K|    104K|00:00:00.62 |    3621 |    941 |    926 |       |       |          |         |
|  98 |             HASH UNIQUE                                 |                             |      1 |    104K|    104K|00:00:00.56 |    3621 |    941 |    926 |  6559K|  2626K| 4029K (1)|    1024 |
|* 99 |              FILTER                                     |                             |      1 |        |    114K|00:00:00.56 |    3621 |    885 |    870 |       |       |          |         |
|*100 |               HASH JOIN RIGHT OUTER                     |                             |      1 |    111K|    114K|00:00:00.50 |    3621 |    885 |    870 |  2616K|  2616K|  163K (0)|         |
| 101 |                TABLE ACCESS FULL                        | OTG_MONITOR_STATUS          |      1 |      1 |      0 |00:00:00.01 |       0 |      0 |      0 |       |       |          |         |
|*102 |                HASH JOIN                                |                             |      1 |    111K|    114K|00:00:00.38 |    3621 |    885 |    870 |  6779K|  2261K| 3833K (1)|    5120 |
|*103 |                 TABLE ACCESS FULL                       | MONITOR                     |      1 |    112K|    114K|00:00:00.05 |    1574 |      0 |      0 |       |       |          |         |
|*104 |                 HASH JOIN                               |                             |      1 |    109K|    112K|00:00:00.17 |    2047 |    330 |    330 |  6201K|  2421K| 4763K (1)|    3072 |
|*105 |                  TABLE ACCESS FULL                      | SHIPMENT                    |      1 |    102K|    104K|00:00:00.04 |    1450 |      0 |      0 |       |       |          |         |
|*106 |                  TABLE ACCESS FULL                      | CONTAINER                   |      1 |    110K|    112K|00:00:00.04 |     597 |      0 |      0 |       |       |          |         |
| 107 |         REMOTE                                          |                             |      1 |   9787 |  12046 |00:00:00.07 |       0 |      0 |      0 |       |       |          |         |
| 108 |        REMOTE                                           | OE_ORDER_HEADERS_ALL        |  82412 |      1 |  82393 |00:00:37.51 |       0 |      0 |      0 |       |       |          |         |
| 109 |       REMOTE                                            | OE_ORDER_HEADERS_ALL        |  82393 |      3 |    111 |00:00:22.73 |       0 |      0 |      0 |       |       |          |         |
|*110 |    TABLE ACCESS FULL                                    | APPLICATION_LOCK            |      1 |     18 |      8 |00:00:00.05 |     252 |      0 |      0 |       |       |          |         |
| 111 |   VIEW                                                  | VW_NSO_1                    |      1 |   1119 |      1 |00:00:00.17 |    3621 |      0 |     31 |       |       |          |         |
|*112 |    FILTER                                               |                             |      1 |        |      1 |00:00:00.17 |    3621 |      0 |     31 |       |       |          |         |
|*113 |     HASH JOIN                                           |                             |      1 |   1119 |      1 |00:00:00.17 |    3621 |      0 |     31 |  2168K|  2168K|  641K (0)|         |
|*114 |      HASH JOIN                                          |                             |      1 |   1129 |      1 |00:00:00.12 |    2171 |      0 |      0 |  2168K|  2168K|  705K (0)|         |
|*115 |       TABLE ACCESS FULL                                 | MONITOR                     |      1 |   1129 |      1 |00:00:00.07 |    1574 |      0 |      0 |       |       |          |         |
|*116 |       TABLE ACCESS FULL                                 | CONTAINER                   |      1 |    110K|    112K|00:00:00.05 |     597 |      0 |      0 |       |       |          |         |
|*117 |      TABLE ACCESS FULL                                  | SHIPMENT                    |      1 |    102K|    104K|00:00:00.04 |    1450 |      0 |      0 |       |       |          |         |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</>

Connor McDonald
September 07, 2017 - 2:59 am UTC

You can see your subquery at line 111. We took your "(N)ested (S)ubquery" and made it a view (VW_NSO_1).

*We* (the optimizer) estimate your subquery will return 1119 rows, and hence our logic will be: "Man, we're not going to take >1000 values and run them through 1 at a time into the main query"

If you *know* that the you'll only ever get one row, you can change the query to be:

where ship_id =

instead of

where ship_id in

and then the optimizer will know we're only expecting 1 row.


few more observations

Rajeshwaran, Jeyabal, September 07, 2017 - 6:37 am UTC

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                               | Name                        | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  | Writes |  OMem |  1Mem | Used-Mem | Used-Tmp|
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|  29 |                             NESTED LOOPS                |                             |      1 |      1 |    114K|00:00:01.31 |   70134 |    270 |    255 |       |       |          |         |
|  30 |                              NESTED LOOPS               |                             |      1 |      1 |    116K|00:00:01.01 |   49267 |    270 |    255 |       |       |          |         |
|* 31 |                               HASH JOIN                 |                             |      1 |      1 |    112K|00:00:00.56 |    3497 |    270 |    255 |  6574K|  2024K| 8741K (0)|         |
|* 32 |                                TABLE ACCESS FULL        | SHIPMENT                    |      1 |    102K|    104K|00:00:00.04 |    1450 |      0 |      0 |       |       |          |         |
|* 33 |                                HASH JOIN                |                             |      1 |    109K|    112K|00:00:00.24 |    2047 |    270 |    255 |  6568K|  2337K| 7954K (1)|    3072 |
|* 34 |                                 TABLE ACCESS FULL       | CONTAINER                   |      1 |    110K|    112K|00:00:00.03 |     597 |      0 |      0 |       |       |          |         |
|  35 |                                 TABLE ACCESS FULL       | SHIPMENT                    |      1 |    102K|    104K|00:00:00.07 |    1450 |      0 |      0 |       |       |          |         |
|* 36 |                               INDEX RANGE SCAN          | MON_CTR_ID_IDX1             |    112K|      1 |    116K|00:00:00.41 |   45770 |      0 |      0 |       |       |          |         |
|* 37 |                              TABLE ACCESS BY INDEX ROWID| MONITOR                     |    116K|      1 |    114K|00:00:00.24 |   20867 |      0 |      0 |       |       |          |         |
|* 38 |                            TABLE ACCESS BY INDEX ROWID  | MONITOR                     |    114K|      1 |    339 |00:00:00.62 |   70347 |      0 |      0 |       |       |     


Don't find "predicate information" in the above plan output.

Can you check what "predicate information" got applied in the line#31 - where Estimated and actual cardinality differs huge and that triggers Nested loops further.

check if extended/expression stats can be applied here to fix this cardinality deviation.


WITH CLAUSE

SeanMacGC, September 07, 2017 - 9:11 am UTC

You could also try converting the subquery, to a subquery factoring clause, ie, WITH CLAUSE:

WITH sub_q as (SELECT s.ship_id
FROM monitor m
INNER JOIN container c
ON c.ctr_id = m.mon_ctr_id
INNER JOIN shipment s
ON s.ship_id = c.ctr_ship_id
WHERE m.deactivated IS NULL
AND c.deactivated IS NULL
AND s.deactivated IS NULL
AND m.deactivated IS NULL
AND LOWER (m.mon_serial_number) =
LOWER ('2703743130'))
SELECT...
...
AND ship_id IN (SELECT ship_id FROM sub_q...)

Just to explore whether that brings the estimate versus actual cardinalities in any.
Connor McDonald
September 09, 2017 - 12:50 am UTC

Agreed, and a technique that is often useful, especially with a MATERIALIZE hint if necessary.

Tried all the 3 suggestions but still the same

Jason Shannon, September 08, 2017 - 7:53 pm UTC

Guys

I tried all 3 suggestions...

1. Replaced IN with = (plan stayed the same and so did the timings).
2. Created extended stats (hopefully correctly) - (plan stayed the same and so did the timings).
3. Changed the subquery to be WITH clause (and also tried ith the MATERIALIZE hint) - plan stayed the same and so did the timings.

Any other thoughts?

Connor McDonald
September 19, 2017 - 2:58 am UTC

Can you send us the full dbms_xplan output (plus predicate sections etc) for the "1" case above.

A Hint

Rajeshwaran, Jeyabal, September 09, 2017 - 2:00 am UTC

Can you add this Hint (/*+ unnest push_subq */) to the subquery and let us know how it goes.

I am not suggesting to go with this Hint, just want to check now it goes after adding the hint.

don't find any create table/insert to play with. hence checking with you to do the same.

AND ship_id IN
  (SELECT /*+ unnest push_subq */ s.ship_id
     FROM monitor m
    INNER JOIN container c
       ON c.ctr_id = m.mon_ctr_id
    INNER JOIN shipment s
       ON s.ship_id = c.ctr_ship_id
    WHERE     m.deactivated IS NULL
    AND c.deactivated IS NULL
    AND s.deactivated IS NULL
    AND m.deactivated IS NULL
    AND LOWER (m.mon_serial_number) =
        LOWER ('2703743130'))

LOWER (m.mon_serial_number)

Arian, September 13, 2017 - 1:14 pm UTC

If the serial_number is returning a unique or low number of rows, you can consider a function based index on LOWER (m.mon_serial_number).
Or if the mon_serial_number is a NUMBER column, remove the LOWER function.

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.