Help in tuning query

Please help me in tuning sql queries. I am a bit new to this. I have generated an Explain plan(plan table) for a particular sql statement using sql navigator. What do i do with this information exactly in order to tune the statement. It is showing some table access full,merge join cartesian, hash join semi and so on. Can anyone guide me with the general tuning procedure with respect to using the plan table analysis.

Hi,
There is a good metalink note about this one
46234.1 : Interpreting Explain plan
Nicolas.

Similar Messages

  • Help with tuning query

    Hi All,
    I have the following query which seems to be blowing our temp tablespace.
    Can anyone help me in tuning this?
    The query and explian plan are below.
    Thanks for all your help
    select trim(b.pnr_reference) || ',"' || to_char(b.booking_date, 'DD-MON-YY') || '","' || trim(p.title) || '","' ||
    trim(p.forename) || '","' || trim(p.surname) || '
    ","' || trim(p.email) || '","' || trim(b.home_phone) || '","' || trim(b.address_line_1) || '","' ||
    trim(b.address_line_2) || '","' || trim(b.address_line_3)
    || '","' || trim(b.postcode) || '","' || trim(b.country_code) || '","' || null || '","' || -- frequent flyer number
    trim(b.promo_opt_in) || '","' || trim(bsout.flight_number) || '","' || to_char(bsout.departure_date, 'DD-MON-YY') || '","' ||trim(bsret.flight_number) ||
    '","' || to_char(bsret.departure_date, 'DD-MON-YY') || ' ","' || pax_cnt.num_adults || '","' || pax_cnt.num_children || '","' || pax_cnt.num_infants || '","' ||fares_by_booking.gross_fare_adult || '","' || fares_by_booking.gross_fare_child || '","' ||fares_by_booking.gross_fare_infant || '","' || fares_by_booking.total_gross_fare || '","' || trim(ps.fare_basis) || '","' ||trim(ps1.fare_basis) || '","' || null || '","' || -- user_id null || '","' || -- ip_address
    fares_by_booking.currency || '","' || trim(bsout.dep_airport_code) || '","' ||trim(bsout.dest_airport_code) || '","' ||fares_by_booking.gross_fare_adult*ex.exchange_rate || '","' || null || '","' || -- iata_no
    trim(b.other_opt_in) || '","' || c.returned || '","' || c.not_available ||'","' || c.on_request || '","' || c.matched || '"
    ,"' || trim(c.location_code) || '","' || to_char(c.pickup_date, 'DD-MON-YY') || '","' || to_char(c.return_date, 'DD-MON-YY') || '","' || c.period || '","' ||
    trim(cb.book_ref) || '","' || cb.car_num || '","' ||
    c.avail_warn_code || '","' || c.avail_err_code || '",
    "' || c.res_warn_code || '","' || c.res_err_code |
    | '"' from bookings b, car_hire_transactions c, car_hire_bookings cb, pax p, booking_sectors bsout, booking_sectors bsret, pax_sectors ps, pax_sectors ps1,
    exchange_rates ex, (select b2.booking_id, sum(decode(p2.pax_type,'A',1,0)) num_adults, sum(decode(p2.pax_type,'C',1,0)) num_children, sum(decode(p2.pax_type,'I',1
    ,0)) num_infants from pax p2, bookings b2
    where b2.booking_id = p2.booking_id and b2.booking_date
    between TO_DATE('08-08-2005/00:00', 'DD-MM-YYYY/HH24:MI') and TO_DATE('15-08-2005/00:00', 'DD-MM-YYYY/HH24:MI') group by
    b2.booking_id) pax_cnt, (select booking_id, currency, sum(gross_fare_adult) gross_fare_adult,
    sum(gross_fare_child) gross_fare_child, sum(gross_fare_infant) gross_fare_infant,sum(total_gross_fare) total_gross_fare from (select b3.booking_id booking_id, ps3.sector_seq,
    ps3.currency_code currency, max(decode(p3.pax_type,'A',ps3.gross_fare,0)) gross_fare_adult,
    max(decode(p3.pax_type,'C',ps3.gross_fare,0)) gross_fare_child
    , max(decode(p3.pax_type,'I',ps3.gross_fare,0
    )) gross_fare_infant, sum(ps3.gross_fare) total_gross_fare from pax p3,pax_sectors ps3, bookings b3 where b3.booking_id = p3.booking_id and b3.booking_id = ps3.booking_id and p3.pax_seq = ps3.pax_seq and b3.booking_date between TO_DATE('08-08-2005/00:00', 'DD-MM-Y
    YYY/HH24:MI') and TO_DATE('15-08-2005/00:00', 'DD-MM-YYYY/HH24:M
    I') group by b3.booking_id, ps3
    .currency_code, ps3.sector_seq) fares_by_sector group by booking_id, currency) fares_by_booking where
    b.booking_date between TO_DATE('08-08-2005/00:00', 'DD-MM-YYYY/
    HH24:MI') and TO_DATE('15-08-2005/00:00', 'DD-MM-YYYY/HH24:MI')
    and b.car_hire_id = c.car_hire_id(+) and c.car_hire_id = cb.
    car_hire_id(+) and p.booking_id = b.booking_id and p.pax_seq
    = 1 and b.booking_id = bsout.booking_id and bsout.sector_seq = 1 and b.booking_id = bsret.booking_id(+) and bsret.sector_seq(+) = 2 and p.booking_id = ps.booking_id and p.pax_seq
    = ps.pax_seq and ps.sector_seq = 1 and p.booking_id = ps1.booking_id(+) and p.pax_seq = ps1.pax_seq(+) and ps1.sector_seq(+) = 2 and b.booking_id = pax_cnt.booking_id and b.booking_id = fares_by_booking.booking_id and ex.currency_from = fares_by_booking.currency and ex.currency_to = 'GBP' and ex.date_entered = (select max(ex2.date_entered) from exchange_rates ex2 where currency_from = fares_by_booking.currency and currency_to = 'GBP' and ex2.date_entered < b.booking_date)
    Explain plan
    11:34:28 sqlpplus> /
    Elapsed: 00:00:00.00
    Execution Plan
    0
    SELECT STATEMENT Optimizer=CHOOSE (Cost=16 Card=1 Bytes=973)
    1 0
    HASH JOIN (OUTER) (Cost=16 Card=1 Bytes=973)
    2 1
    HASH JOIN (Cost=9 Card=1 Bytes=958)
    3 2
    NESTED LOOPS (OUTER) (Cost=6 Card=1 Bytes=902)
    4 3
    NESTED LOOPS (Cost=6 Card=1 Bytes=871)
    5 4
    NESTED LOOPS (OUTER) (Cost=6 Card=1 Bytes=812)
    6 5
    NESTED LOOPS (Cost=6 Card=1 Bytes=753)
    7 6
    NESTED LOOPS (Cost=6 Card=1 Bytes=533)
    8 7
    NESTED LOOPS (OUTER) (Cost=6 Card=1 Bytes=475)
    9 8
    NESTED LOOPS (Cost=6 Card=1 Bytes=429)
    10 9
    NESTED LOOPS (Cost=6 Card=1 Bytes=412)
    11 10
    VIEW (Cost=6 Card=1 Bytes=75)
    12 11
    SORT (GROUP BY) (Cost=6 Card=1 Bytes=84)
    13 12
    VIEW (Cost=6 Card=1 Bytes=84)
    14 13
    SORT (GROUP BY) (Cost=6 Card=1 Bytes=118)
    15 14
    FILTER
    16 15
    TABLE ACCESS (BY INDEX ROWID) OF 'PAX_SECTORS'
    17 16
    NESTED LOOPS (Cost=4 Card=1 Bytes=118)
    18 17
    NESTED LOOPS (Cost=4 Card=1 Bytes=60)
    19 18
    VIEW OF 'index$_join$_017' (Cost=4 Card=1
    Bytes=31)
    20 19
    HASH JOIN
    21 20
    INDEX (FAST FULL SCAN) OF 'BOO_PK' (UN
    IQUE) (Cost=1 Card=1 Bytes=31)
    22 20
    INDEX (FAST FULL SCAN) OF 'BOO_SHARES_
    UID_UK' (UNIQUE) (Cost=1 Card=1 Bytes=31)
    23 18
    TABLE ACCESS (BY INDEX ROWID) OF 'PAX'
    24 23
    INDEX (RANGE SCAN) OF 'PAX_PK' (UNIQUE)
    25 17
    INDEX (RANGE SCAN) OF 'PXS_PAX_FK_I' (NON-UN
    IQUE)
    26 10
    TABLE ACCESS (BY INDEX ROWID) OF 'BOOKINGS'
    27 26
    INDEX (UNIQUE SCAN) OF 'BOO_PK' (UNIQUE)
    28 9
    TABLE ACCESS (BY INDEX ROWID) OF 'EXCHANGE_RATES'
    29 28
    INDEX (UNIQUE SCAN) OF 'EXR_PK' (UNIQUE)
    30 29
    SORT (AGGREGATE)
    31 30
    FIRST ROW (Cost=2 Card=284 Bytes=3692)
    32 31
    INDEX (RANGE SCAN (MIN/MAX)) OF 'EXR_PK' (UNIQUE) (C
    ost=2 Card=200)
    33 8
    TABLE ACCESS (BY INDEX ROWID) OF 'BOOKING_SECTORS'
    34 33
    INDEX (UNIQUE SCAN) OF 'BKS_PK' (UNIQUE)
    35 7
    TABLE ACCESS (BY INDEX ROWID) OF 'BOOKING_SECTORS'
    36 35
    INDEX (UNIQUE SCAN) OF 'BKS_PK' (UNIQUE)
    37 6
    TABLE ACCESS (BY INDEX ROWID) OF 'PAX'
    38 37
    INDEX (UNIQUE SCAN) OF 'PAX_PK' (UNIQUE)
    39 5
    TABLE ACCESS (BY INDEX ROWID) OF 'PAX_SECTORS'
    40 39
    INDEX (UNIQUE SCAN) OF 'PXS_UK' (UNIQUE)
    41 4
    TABLE ACCESS (BY INDEX ROWID) OF 'PAX_SECTORS'
    42 41
    INDEX (UNIQUE SCAN) OF 'PXS_UK' (UNIQUE)
    43 3
    TABLE ACCESS (BY INDEX ROWID) OF 'CAR_HIRE_TRANSACTIONS'
    44 43
    INDEX (UNIQUE SCAN) OF 'CHT_PK' (UNIQUE)
    45 2
    VIEW (Cost=3 Card=1 Bytes=56)
    46 45
    SORT (GROUP BY) (Cost=3 Card=1 Bytes=47)
    47 46
    FILTER
    48 47
    TABLE ACCESS (BY INDEX ROWID) OF 'PAX'
    49 48
    NESTED LOOPS (Cost=1 Card=1 Bytes=47)
    50 49
    TABLE ACCESS (BY INDEX ROWID) OF 'BOOKINGS' (Cost=1 Card=1 Bytes
    =31)
    51 50
    INDEX (SKIP SCAN) OF 'BOO_SHARES_UID_UK' (UNIQUE)
    52 49
    INDEX (RANGE SCAN) OF 'PAX_PK' (UNIQUE)
    53 1
    TABLE ACCESS (FULL) OF 'CAR_HIRE_BOOKINGS' (Cost=6 Card=7 Bytes=105)

    Hi Guys,
    As requested here is the formatted sql.
    We are using pga_aggregate_target which is set to 25165824. I dont know if this is relevant but the sort_area_size is set to 524288.
    Thanks
    SQL
    SELECT trim(b.pnr_reference) || ',"' || to_char(b.booking_date, 'DD-MON-YY')
    || '","' || trim(p.title) || '","' || trim(p.forename) || '","' ||
    trim(p.surname) || '","' || trim(p.email) || '","' || trim(b.home_phone)
    || '","' || trim(b.address_line_1) || '","' || trim(b.address_line_2)
    || '","' || trim(b.address_line_3) || '","' || trim(b.postcode) ||
    '","' || trim(b.country_code) || '","' || null || '","' || trim(
    b.promo_opt_in) || '","' || trim(bsout.flight_number) || '","' ||
    to_char(bsout.departure_date, 'DD-MON-YY') || '","' || trim(
    bsret.flight_number) || '","' || to_char(bsret.departure_date,
    'DD-MON-YY') || '","' || pax_cnt.num_adults || '","' ||
    pax_cnt.num_children || '","' || pax_cnt.num_infants || '","' ||
    fares_by_booking.gross_fare_adult || '","' ||
    fares_by_booking.gross_fare_child || '","' ||
    fares_by_booking.gross_fare_infant || '","' ||
    fares_by_booking.total_gross_fare || '","' || trim(ps.fare_basis) ||
    '","' || trim(ps1.fare_basis) || '","' || null || '","' || null || '","'
    || fares_by_booking.currency || '","' || trim(bsout.dep_airport_code)
    || '","' || trim(bsout.dest_airport_code) || '","' ||
    fares_by_booking.gross_fare_adult * ex.exchange_rate || '","' || null
    || '","' || trim(b.other_opt_in) || '","' || c.returned || '","' ||
    c.not_available || '","' || c.on_request || '","' || c.matched || '","'
    || trim(c.location_code) || '","' || to_char(c.pickup_date, 'DD-MON-YY'
    ) || '","' || to_char(c.return_date, 'DD-MON-YY') || '","' || c.period
    || '","' || trim(cb.book_ref) || '","' || cb.car_num || '","' ||
    c.avail_warn_code || '","' || c.avail_err_code || '","' ||
    c.res_warn_code || '","' || c.res_err_code || '"'
    FROM bookings b, car_hire_transactions c, car_hire_bookings cb, pax p,
    booking_sectors bsout, booking_sectors bsret, pax_sectors ps,
    pax_sectors ps1, exchange_rates ex, (SELECT b2.booking_id, sum(
    decode(p2.pax_type, 'A', 1,
    0)) num_adults, sum(
    decode(p2.pax_type, 'C', 1,
    0)) num_children,
    sum(decode(p2.pax_type,
    'I', 1, 0)) num_infants
    FROM pax p2, bookings b2
    WHERE b2.booking_id = p2.booking_id
    AND b2.booking_date BETWEEN to_date('08-08-2005/00:00',
    'DD-MM-YYYY/HH24:MI') AND to_date('15-08-2005/00:00',
    'DD-MM-YYYY/HH24:MI')
    GROUP BY b2.booking_id) pax_cnt, (SELECT booking_id, currency,
    sum(gross_fare_adult)
    gross_fare_adult,
    sum(gross_fare_child)
    gross_fare_child,
    sum(gross_fare_infant)
    gross_fare_infant,
    sum(total_gross_fare)
    total_gross_fare
    FROM (SELECT b3.booking_id booking_id, ps3.sector_seq,
    ps3.currency_code currency, max(decode(p3.pax_type,
    'A', ps3.gross_fare, 0)) gross_fare_adult, max(
    decode(p3.pax_type, 'C', ps3.gross_fare, 0))
    gross_fare_child, max(decode(p3.pax_type, 'I',
    ps3.gross_fare, 0)) gross_fare_infant, sum(
    ps3.gross_fare) total_gross_fare
    FROM pax p3, pax_sectors ps3, bookings b3
    WHERE b3.booking_id = p3.booking_id
    AND b3.booking_id = ps3.booking_id
    AND p3.pax_seq = ps3.pax_seq
    AND b3.booking_date BETWEEN to_date(
    '08-08-2005/00:00', 'DD-MM-YYYY/HH24:MI') AND
    to_date('15-08-2005/00:00', 'DD-MM-YYYY/HH24:MI')
    GROUP BY b3.booking_id, ps3.currency_code,
    ps3.sector_seq) fares_by_sector
    GROUP BY booking_id, currency) fares_by_booking
    WHERE b.booking_date BETWEEN to_date('08-08-2005/00:00',
    'DD-MM-YYYY/HH24:MI') AND to_date('15-08-2005/00:00',
    'DD-MM-YYYY/HH24:MI')
    AND b.car_hire_id = c.car_hire_id (+)
    AND c.car_hire_id = cb.car_hire_id (+)
    AND p.booking_id = b.booking_id
    AND p.pax_seq = 1
    AND b.booking_id = bsout.booking_id
    AND bsout.sector_seq = 1
    AND b.booking_id = bsret.booking_id (+)
    AND bsret.sector_seq (+) = 2
    AND p.booking_id = ps.booking_id
    AND p.pax_seq = ps.pax_seq
    AND ps.sector_seq = 1
    AND p.booking_id = ps1.booking_id (+)
    AND p.pax_seq = ps1.pax_seq (+)
    AND ps1.sector_seq (+) = 2
    AND b.booking_id = pax_cnt.booking_id
    AND b.booking_id = fares_by_booking.booking_id
    AND ex.currency_from = fares_by_booking.currency
    AND ex.currency_to = 'GBP'
    AND ex.date_entered = (SELECT max(ex2.date_entered)
    FROM exchange_rates ex2
    WHERE currency_from = fares_by_booking.currency
    AND currency_to = 'GBP'
    AND ex2.date_entered < b.booking_date)
    explain_plan
    Execution Steps:
    Step # Step Name
    41 SELECT STATEMENT
    40 FILTER
    36 NESTED LOOPS [OUTER]
    33 NESTED LOOPS
    30 HASH JOIN [OUTER]
    28 NESTED LOOPS [OUTER]
    25 HASH JOIN
    23 HASH JOIN [OUTER]
    21 HASH JOIN
    15 HASH JOIN
    5 HASH JOIN
    3 HASH JOIN
    1 CAM_OWNER.PAX_SECTORS TABLE ACCESS [FULL]
    2 CAM_OWNER.PAX TABLE ACCESS [FULL]
    4 CAM_OWNER.BOOKINGS TABLE ACCESS [FULL]
    14 . VIEW
    13 SORT [GROUP BY]
    12 . VIEW
    11 SORT [GROUP BY]
    10 HASH JOIN
    8 HASH JOIN
    6 CAM_OWNER.BOOKINGS TABLE ACCESS [FULL]
    7 CAM_OWNER.PAX_SECTORS TABLE ACCESS [FULL]
    9 CAM_OWNER.PAX TABLE ACCESS [FULL]
    20 . VIEW
    19 SORT [GROUP BY]
    18 HASH JOIN
    16 CAM_OWNER.BOOKINGS TABLE ACCESS [FULL]
    17 CAM_OWNER.PAX TABLE ACCESS [FULL]
    22 CAM_OWNER.PAX_SECTORS TABLE ACCESS [FULL]
    24 CAM_OWNER.EXCHANGE_RATES TABLE ACCESS [FULL]
    27 CAM_OWNER.CAR_HIRE_TRANSACTIONS TABLE ACCESS [BY INDEX ROWID]
    26 CAM_OWNER.CHT_PK INDEX [UNIQUE SCAN]
    29 CAM_OWNER.CAR_HIRE_BOOKINGS TABLE ACCESS [FULL]
    32 CAM_OWNER.BOOKING_SECTORS TABLE ACCESS [BY INDEX ROWID]
    31 CAM_OWNER.BKS_PK INDEX [UNIQUE SCAN]
    35 CAM_OWNER.BOOKING_SECTORS TABLE ACCESS [BY INDEX ROWID]
    34 CAM_OWNER.BKS_PK INDEX [UNIQUE SCAN]
    39 SORT [AGGREGATE]
    38 FIRST ROW
    37 CAM_OWNER.EXR_PK INDEX [RANGE SCAN (MIN/MAX)]

  • Need help th tuning query or re write the query--

    Hi,
    Need help to tune the below query or rewrite th query for reducing the execution time Please find the query and explain plan.
    QUERY
    explain plan FOR SELECT consumer_key,product_key,days_in_product,20100201 period_key FROM
    (SELECT consumer_key,
      product_key,
      days_in_product,
      row_number() over ( Partition BY consumer_key order by Days_in_product DESC) row_num
    FROM
      (SELECT consumer_key,
        product_key,
        SUM(no_ofdays) days_in_product
      FROM
        (SELECT pcv.consumer_key,
          pcv.product_key,
          pcv.product_consumer_valid_from,
          pcv.product_consumer_valid_to,
          DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from) period_start,
          DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959) period_end,
          CASE
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) BETWEEN 20100201000000 AND 20100228235959
            AND activation_date > to_Date(product_consumer_valid_to,'YYYYMMDDHH24MISS')
            THEN 0
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) BETWEEN 20100201000000 AND 20100228235959
            AND activation_date BETWEEN to_Date(product_consumer_valid_from,'YYYYMMDDHH24MISS') AND to_Date(product_consumer_valid_to,'YYYYMMDDHH24MISS')
            THEN
              --to_char(activation_date,'MON-YYYY')='PERIOD_ACTIVE'  and activation_date >= to_Date(product_consumer_valid_from,'YYYYMMDDHH24MISS') then
              (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_date(TO_CHAR(activation_date,'YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS') )
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) < 20100201000000
            THEN (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_Date(DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from),'YYYYMMDDHH24MISS') )
            WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) > 20100228235959
            THEN 0
            ELSE
              --unusual situation
              (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_Date(DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from),'YYYYMMDDHH24MISS') )
          END No_ofDays
        FROM cimtran.product_consumer_validity pcv,
          consumer_dimension cd
        WHERE pcv.consumer_key           =cd.consumer_key
        AND product_consumer_valid_to   >= 20100201000000
        AND product_consumer_valid_from <= 20100228235959
          --and product_consumer_valid_from > '20090801000000'
        ORDER BY consumer_key,
          product_key,
          product_consumer_valid_from
        ) a
      GROUP BY consumer_key,
        product_key
      ORDER BY consumer_key,
        product_key
    ) WHERE row_num=1 ;EXPLAIN PLAN
    "PLAN_TABLE_OUTPUT"
    "Plan hash value: 3823907703"
    "| Id  | Operation                | Name                      | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |"
    "|   0 | SELECT STATEMENT         |                           |  4665K|   231M|       |   133K  (1)| 00:31:08 |"
    "|*  1 |  VIEW                    |                           |  4665K|   231M|       |   133K  (1)| 00:31:08 |"
    "|*  2 |   WINDOW SORT PUSHED RANK|                           |  4665K|   173M|   232M|   133K  (1)| 00:31:08 |"
    "|   3 |    VIEW                  |                           |  4665K|   173M|       |   104K  (1)| 00:24:18 |"
    "|   4 |     SORT GROUP BY        |                           |  4665K|   182M|   729M|   104K  (1)| 00:24:18 |"
    "|*  5 |      HASH JOIN           |                           |    13M|   533M|    65M| 44241   (1)| 00:10:20 |"
    "|   6 |       TABLE ACCESS FULL  | CONSUMER_DIMENSION        |  2657K|    35M|       |  4337   (1)| 00:01:01 |"
    "|*  7 |       TABLE ACCESS FULL  | PRODUCT_CONSUMER_VALIDITY |    13M|   351M|       | 15340   (2)| 00:03:35 |"
    "Predicate Information (identified by operation id):"
    "   1 - filter(""ROW_NUM""=1)"
    "   2 - filter(ROW_NUMBER() OVER ( PARTITION BY ""CONSUMER_KEY"" ORDER BY "
    "              INTERNAL_FUNCTION(""DAYS_IN_PRODUCT"") DESC )<=1)"
    "   5 - access(""PCV"".""CONSUMER_KEY""=""CD"".""CONSUMER_KEY"")"
    "   7 - filter(""PRODUCT_CONSUMER_VALID_FROM""<=20100228235959 AND "
    "              ""PRODUCT_CONSUMER_VALID_TO"">=20100201000000)"

    I doubt that this query can be tuned without using indexes. There is a lot of unnecessary work specified in your query, like unnecessary intermediate sorting and selecting unused columns. The cost based optimizer recognized it and skips some of that unnecessary work, it seems. For clarity's sake, I would rewrite your query like below. Note that the query is untested:
    select consumer_key
         , max(product_key) keep (dense_rank last order by days_in_product) product_key
         , max(days_in_product) days_in_product
         , 20100201 period_key
      from ( select pcv.consumer_key
                  , pcv.product_key
                  , sum
                    ( case
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) between 20100201000000 and 20100228235959
                      then
                        case
                        when cd.activation_date > to_date(pcv.product_consumer_valid_to,'yyyymmddhh24miss')
                        then
                          0
                        when cd.activation_date between to_date(pcv.product_consumer_valid_from,'yyyymmddhh24miss') and to_date(product_consumer_valid_to,'yyyymmddhh24miss')
                        then
                          to_date(to_char(pcv.product_consumer_valid_to),'yyyymmddhh24miss'))
                          - to_date(to_char(activation_date,'yyyymmddhh24miss'),'yyyymmddhh24miss')
                        end
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) < 20100201000000
                      then
                        to_date(to_char(pcv.product_consumer_valid_to),'yyyymmddhh24miss'))
                        - to_date(to_char(pcv.product_consumer_valid_from),'yyyymmddhh24miss'))
                      when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) > 20100228235959
                      then
                        0
                      end
                    ) days_in_product
               from cimtran.product_consumer_validity pcv
                  , consumer_dimension cd
              where pcv.consumer_key             = cd.consumer_key
                and product_consumer_valid_to   >= 20100201000000
                and product_consumer_valid_from <= 20100228235959
              group by consumer_key
                  , product_key
    group by consumer_keyRegards,
    Rob.

  • Tuning query with sdo_join in it

    Hello all. I'm new to the forum, but I hope someone can help me tuning a spatial query. I have no DBA experience or education, but I'm writing SQL queries for a few years now, first in Oracle 9i (with Locator), now in Oracle 10g (10.2) and Oracle XE.
    I’ve posted this in the Spatial forum as I suspect it’s mostly a spatial issue.
    I have a table like:
    CREATE TABLE CYCLORAMA (
      DATASETID   VARCHAR2(9 BYTE),
      IMAGEID     VARCHAR2(9 BYTE),
      OPNAMEDATUM CHAR(20 BYTE),
      GEOMETRIE   MDSYS.SDO_GEOMETRY
    )with only a standard spatial index on GEOMETRIE, and containing about 180000 records. IMAGEID is unique, but not defined as such. Geometry is SDO_POINT_TYPE for all records, like: MDSYS.SDO_GEOMETRY(2001,90112,MDSYS.SDO_POINT_TYPE(86039.572,439541.158,'null'),'null','null'). USER_SDO_GEOM_METADATA is filled with values used for all my geometries: SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X',0,300000,,001), SDO_DIM_ELEMENT('Y',300000,600000,,001)) and SRID = 90112.
    What I need to do is to find (and ultimately delete) all records that have another record with a different value for DATASETID, a higher value for OPNAMEDATUM and fall within 3 meters distance. What I came up with so far is:
    select /*+ all_rows */ s.imageid
    , sdo_geom.sdo_distance(s.geometrie,t.geometrie,0.1) dist
    from cyclorama s, cyclorama t
    , table(sdo_join('CYCLORAMA','GEOMETRIE','CYCLORAMA','GEOMETRIE','distance=3')) x
    -- create a crosstable with records that may be within 3 meters distance
    where s.rowid = x.rowid1 and x.rowid2 = t.rowid and not x.rowid1 = x.rowid2
    -- filter 'duplicate matches' sdo_join generates for a self join, and select:
    and s.datasetid != t.datasetid                             -- if in different dataset
    and s.opnamedatum < t.opnamedatum                          -- from a later date
    and sdo_geom.sdo_distance(s.geometrie,t.geometrie,0.1) < 3 -- within 3 meters
    ;This is surprisingly fast (7 seconds for first 50 records), but still takes about 7 minutes (Oracle XE on a 768MB, 2.7GHz VM) for the whole query. This is acceptable, but:
    * Is this solution the best approach?
    * Can this be done faster or more efficient? Clever indexes, hints, smarter query, ...?
    * Is it worth optimizing the SDO_DIM_ARRAY for this table, e.g. narrowing the range of coordinate values)?
    * How to delete the found records most efficiently?
    * Can this be done using an anti-join, to select the records to keep?
    Thank you for reading this, and any answers you may have,
    J-----.
    Jeroen Muris

    Jack,
    Thank you for your response. It helped me to clean up the query. All the changes did not give a much better explain plan - at least not to my inexperienced eyes - but the total execution time for the query is now reduced to under two minutes. The query as it is now:
    select /*+ ordered all_rows */ x.rowid1
    from table(sdo_join('CYCLORAMA','GEOMETRIE','CYCLORAMA','GEOMETRIE','distance=3 mask=ANYINTERACT')) x
    , cyclorama s, cyclorama t
    where not x.rowid1 = x.rowid2
    and s.rowid = x.rowid1 and x.rowid2 = t.rowid
    and s.datasetid != t.datasetid
    and s.opnamedatum < t.opnamedatum;Because the docs state that mask=FILTER is the default, I added an explicit mask=ANYINTERACT to the sdo_join parameters when I removed the sdo_distance from the query. Still, the query returns 205035 records with the sdo_distance, and 205125 without. But this may be the result of the extra 0.001 from sdo_dim. I did not investigate as the 3 meter is not crucial.
    I believe I already had a mechanism in place to reduce the number of self-joins with "not x.rowid1 = x.rowid2" and "s.opnamedatum < t.opnamedatum". I can not guarantee that for all records s.opnamedatum < t.opnamedatum equals x.rowid1 < x.rowid2.
    Based on the [url http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14255/sdo_operat.htm#BGEDJIBF]docs, I finally added an 'ordered' hint and reshuffled the tables in the from clause.
    I'm happy with performance now, and creating a new table with the records to keep should not be a problem. Still I'm curious about the following:
    <li>Is it worth optimizing the SDO_DIM_ARRAY for the original table, e.g. narrowing the range of coordinate values?
    <li>How can sdo_join best be used for an anti-join, to select the records to keep?
    Thank you,
    J-----.

  • Help on tuning SQL

    Hi list ,
    I need your help in tuning this sql.
    Oracle 10g,Windows 2008 64 bit running on virtual machine.
    SELECT t0.RecID, t0.PrtID, t0.RecOrdDt, t0.RecOrdID, t0.RecExtOrdID, t0.RecPocID, t0.PriID, t0.CasID, t0.CasNo, t0.CasClass, t0.CasPocID, t0.CasAdmit, t0.CasNotValid, t0.PatName, t0.PatFName, t0.PatBName, t0.PatBDate, t0.PatSex, t0.PatNotValid, t0.RstID, t0.RstState, t0.RstMeaningI18N, t0.PrtType, t0.PrtSubType, t0.RpxType, t0.RpxPrio, t0.PerID, t0.PerName, t0.PerFName, t0.PerTitle, t0.PerType, t0.AgnExtID
    FROM
              IXSERV.XsvRecPerInfoV t0
    WHERE
                   ((((t0.AgnExtID = '1728454694') AND (t0.PrtID IN (72, 165)))
           AND (t0.RpxType IN (1, 2, 6, 3, 5)))
           AND (t0.RstState IN (202, 208, 210, 203, 206)))
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.04       0.03          0       1999          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        4      5.39      57.42      45408      53080          0          45
    total        6      5.43      57.46      45408      55079          0          45
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: SYS
    Rows     Row Source Operation
         45  NESTED LOOPS  (cr=53080 pr=45408 pw=0 time=2611987 us)
         45   NESTED LOOPS  (cr=52941 pr=45355 pw=0 time=46145568 us)
         45    HASH JOIN  (cr=52802 pr=45314 pw=0 time=45407144 us)
          1     TABLE ACCESS BY INDEX ROWID PERPERSON (cr=3 pr=6 pw=0 time=40510 us)
          1      INDEX RANGE SCAN PERIDXAGNEXTID (cr=2 pr=5 pw=0 time=22311 us)(object id 56817)
      23623     TABLE ACCESS BY INDEX ROWID XSVRECPERXREF (cr=52799 pr=45308 pw=0 time=50046345 us)
      33893      NESTED LOOPS  (cr=47543 pr=42261 pw=0 time=73003406 us)
       5139       HASH JOIN  (cr=37184 pr=40103 pw=0 time=56090695 us)
          5        TABLE ACCESS FULL XMFREPORTSTATE (cr=7 pr=6 pw=0 time=22565 us)
      66900        TABLE ACCESS BY INDEX ROWID XSVPATIENTRECORD (cr=37177 pr=40097 pw=0 time=39887547 us)
      66903         NESTED LOOPS  (cr=146 pr=141 pw=0 time=8095232 us)
          2          INLIST ITERATOR  (cr=4 pr=3 pw=0 time=37200 us)
          2           TABLE ACCESS BY INDEX ROWID XMFPATIENTRECORDTYPE (cr=4 pr=3 pw=0 time=37198 us)
          2            INDEX UNIQUE SCAN PKPRT (cr=2 pr=1 pw=0 time=26746 us)(object id 56143)
      66900          INDEX RANGE SCAN RECIDXPRTID (cr=142 pr=138 pw=0 time=51513 us)(object id 56410)
      28753       INDEX RANGE SCAN RPXIDXRECID (cr=10359 pr=2158 pw=0 time=4518986 us)(object id 56453)
         45    TABLE ACCESS BY INDEX ROWID ADTCASE (cr=139 pr=41 pw=0 time=423483 us)
         45     INDEX UNIQUE SCAN PK_ADTCASE (cr=94 pr=23 pw=0 time=184866 us)(object id 55295)
         45   TABLE ACCESS BY INDEX ROWID ADTPATIENT (cr=139 pr=53 pw=0 time=413067 us)
         45    INDEX UNIQUE SCAN PK_ADTPATIENT (cr=94 pr=21 pw=0 time=195583 us)(object id 55361)
    Elapsed times include waiting on following events:
    Event waited on Times Max. Wait Total Waited
    ---------------------------------------- Waited ---------- ------------
    SQL*Net break/reset to client 2 0.00 0.00
    SQL*Net message to client 5 0.00 0.00
    SQL*Net message from client 5 114.42 128.67
    SQL*Net more data to client 1 0.00 0.00
    db file sequential read 27969 0.13 51.57
    db file scattered read 3271 0.05 4.61
    Some lines from trace fils :
    WAIT #1: nam='db file sequential read' ela= 12778 file#=4 block#=3726132 blocks=1 obj#=56817 tim=249799016372
    WAIT #1: nam='db file scattered read' ela= 9271 file#=4 block#=3726133 blocks=4 obj#=56817 tim=249799025761
    WAIT #1: nam='db file sequential read' ela= 18141 file#=4 block#=587487 blocks=1 obj#=55853 tim=249799043945
    WAIT #1: nam='db file sequential read' ela= 18304 file#=4 block#=752595 blocks=1 obj#=56219 tim=249799062471
    and the view   XsvRecPerInfoV   is  :
    SELECT       d.RecID, d.PrtID, d.RecOrdDt, d.RecOrdID, d.RecExtOrdID, d.PocID, d.PriID, c.CasID, c. CasNo, c.CasClass, c.PocID, c.CasAdmit, c.CasNotValid, p.PatName,            p.PatFName, p.PatBName, p.PatBDate, p.PatSex, p.PatNotValid, r.RstID, r.RstState, r.RstMeaningI18N, prt.PrtType, prt.PrtSubType, x.RpxType, x.RpxPrio, per.PerID,        per.PerName, per.PerFName, per.PerTitle, per.PerType, per.AgnExtID
    FROM    XsvPatientRecord d,
                 AdtCase c,
               AdtPatient p,
               XmfReportState r,
               XmfPatientRecordType prt,
               XsvRecPerXRef  x,
               PerPerson per
       WHERE
               d.RecID =  x.RecID  
    AND  d.RstID  =  r.RstID
    AND  d.PrtID    =  prt.PrtID 
    AND   d.CasID  =  c.CasID
    AND  x.PerID = per.PerID
    AND  c.PatID = p.PatID
    i have two questions :
    -1 The problem in tables XSVPATIENTRECORD and XSVRECPERXREF so how can i speed this query either by rewriting it or indexs?
    -2 is my system i-o is slow ?
    Best wishes
    Edited by: welo on Oct 6, 2010 5:05 AM

    hi ,
    it did not work with dynamic samplying. I am posting here trace 10053 output for the correct plan and wrong plan choosed but the optimizer i need soome one to help me in understanding why the optimizer is choosing wrong join order.
    Sql statement and wrong plan :
    tageCurrent SQL statement for this session:
    SELECT    t0.RecID, t0.PrtID, t0.RecOrdDt, t0.RecOrdID, t0.RecExtOrdID, t0.RecPocID, t0.PriID, t0.CasID, t0.CasNo, t0.CasClass, t0.CasPocID, t0.CasAdmit, t0.CasNotValid, t0.PatName, t0.PatFName, t0.PatBName, t0.PatBDate, t0.PatSex, t0.PatNotValid, t0.RstID, t0.RstState, t0.RstMeaningI18N, t0.PrtType, t0.PrtSubType, t0.RpxType, t0.RpxPrio, t0.PerID, t0.PerName, t0.PerFName, t0.PerTitle, t0.PerType, t0.AgnExtID
    FROM XsvRecPerInfoV t0
    WHERE
    ((((t0.AgnExtID = '1393621382') AND (t0.PrtID IN (72, 165))) AND (t0.RpxType IN (1, 2, 6, 3, 5))) AND (t0.RstState IN (202, 208, 210, 203, 206)))
    --------------------------------------------------------------------+-----------------------------------+
    | Id  | Operation                             | Name                | Rows  | Bytes | Cost  | Time      |
    --------------------------------------------------------------------+-----------------------------------+
    | 0   | SELECT STATEMENT                      |                     |       |       |   842 |           |
    | 1   |  NESTED LOOPS                         |                     |     1 |   204 |   842 |  00:00:11 |
    | 2   |   NESTED LOOPS                        |                     |     1 |   163 |   840 |  00:00:11 |
    | 3   |    HASH JOIN                          |                     |     1 |   129 |   838 |  00:00:11 |
    | 4   |     TABLE ACCESS BY INDEX ROWID       | PERPERSON           |     1 |    29 |     2 |  00:00:01 |
    | 5   |      INDEX RANGE SCAN                 | PERIDXAGNEXTID      |     1 |       |     1 |  00:00:01 |
    | 6   |     TABLE ACCESS BY INDEX ROWID       | XSVRECPERXREF       |     2 |    34 |     3 |  00:00:01 |
    | 7   |      NESTED LOOPS                     |                     |   379 |   37K |   835 |  00:00:11 |
    | 8   |       HASH JOIN                       |                     |   238 |   19K |   342 |  00:00:05 |
    | 9   |        TABLE ACCESS FULL              | XMFREPORTSTATE      |     5 |    65 |     3 |  00:00:01 |
    | 10  |        TABLE ACCESS BY INDEX ROWID    | XSVPATIENTRECORD    |   524 |   30K |   218 |  00:00:03 |
    | 11  |         NESTED LOOPS                  |                     |  1049 |   72K |   338 |  00:00:05 |
    | 12  |          INLIST ITERATOR              |                     |       |       |       |           |
    | 13  |           TABLE ACCESS BY INDEX ROWID | XMFPATIENTRECORDTYPE|     2 |    22 |     2 |  00:00:01 |
    | 14  |            INDEX UNIQUE SCAN          | PKPRT               |     2 |       |     1 |  00:00:01 |
    | 15  |          INDEX RANGE SCAN             | RECIDXPRTID         |   524 |       |   119 |  00:00:02 |
    | 16  |       INDEX RANGE SCAN                | RPXIDXRECID         |     2 |       |     2 |  00:00:01 |
    | 17  |    TABLE ACCESS BY INDEX ROWID        | ADTCASE             |     1 |    34 |     2 |  00:00:01 |
    | 18  |     INDEX UNIQUE SCAN                 | PK_ADTCASE          |     1 |       |     1 |  00:00:01 |
    | 19  |   TABLE ACCESS BY INDEX ROWID         | ADTPATIENT          |     1 |    41 |     2 |  00:00:01 |
    | 20  |    INDEX UNIQUE SCAN                  | PK_ADTPATIENT       |     1 |       |     1 |  00:00:01 |
    --------------------------------------------------------------------+-----------------------------------+
    3 - access("PER"."PERID"="X"."PERID")
    5 - access("PER"."AGNEXTID"='1393621382')
    6 - filter(("X"."RPXTYPE"=1 OR "X"."RPXTYPE"=2 OR "X"."RPXTYPE"=3 OR "X"."RPXTYPE"=5 OR "X"."RPXTYPE"=6))
    8 - access("D"."RSTID"="R"."RSTID")
    9 - filter(("R"."RSTSTATE"=202 OR "R"."RSTSTATE"=203 OR "R"."RSTSTATE"=206 OR "R"."RSTSTATE"=208 OR "R"."RSTSTATE"=210))
    14 - access(("PRT"."PRTID"=72 OR "PRT"."PRTID"=165))
    15 - access("D"."PRTID"="PRT"."PRTID")
    15 - filter(("D"."PRTID"=72 OR "D"."PRTID"=165))
    16 - access("X"."RECID"="D"."RECID")
    18 - access("D"."CASID"="C"."CASID")
    20 - access("C"."PATID"="P"."PATID")
    Join order[7]:  XMFPATIENTRECORDTYPE[PRT]#1  XSVPATIENTRECORD[D]#3  XMFREPORTSTATE[R]#2  XSVRECPERXREF[X]#5  PERPERSON[PER]#0  ADTCASE[C]#6  ADTPATIENT[P]#4
    Now joining: XSVPATIENTRECORD[D]#3
    NL Join
      Outer table: Card: 2.00  Cost: 2.00  Resp: 2.00  Degree: 1  Bytes: 11
      Inner table: XSVPATIENTRECORD  Alias: D
      Access Path: TableScan
        NL Join:  Cost: 53601.79  Resp: 53601.79  Degree: 1
          Cost_io: 53336.00  Cost_cpu: 7967475433
          Resp_io: 53336.00  Resp_cpu: 7967475433
    kkofmx: index filter:
    ("D"."PRTID"=72 OR "D"."PRTID"=165) AND ("X"."RPXTYPE"=1 OR "X"."RPXTYPE"=2 OR "X"."RPXTYPE"=3 OR "X"."RPXTYPE"=5 OR "X"."RPXTYPE"=6) AND ("R"."RSTSTATE"=202 OR "R"."RSTSTATE"=203 OR "R"."RSTSTATE"=206 OR "R"."RSTSTATE"=208 OR "R"."RSTSTATE"=210) AND "PER"."PERID"="X"."PERID" AND "X"."RECID"="D"."RECID" AND "D"."RSTID"="R"."RSTID" AND "D"."PRTID"="PRT"."PRTID" AND "D"."CASID"="C"."CASID" AND "C"."PATID"="P"."PATID" AND ("PRT"."PRTID"=72 OR "PRT"."PRTID"=165)
    kkofmx: index filter:"D"."PRTID"="PRT"."PRTID" AND "D"."CASID"="C"."CASID" AND "C"."PATID"="P"."PATID" AND ("PRT"."PRTID"=72 OR "PRT"."PRTID"=165)
    kkofmx: index filter:
    ("D"."PRTID"=72 OR "D"."PRTID"=165) AND ("X"."RPXTYPE"=1 OR "X"."RPXTYPE"=2 OR "X"."RPXTYPE"=3 OR "X"."RPXTYPE"=5 OR "X"."RPXTYPE"=6) AND ("R"."RSTSTATE"=202 OR "R"."RSTSTATE"=203 OR "R"."RSTSTATE"=206 OR "R"."RSTSTATE"=208 OR "R"."RSTSTATE"=210) AND "PER"."PERID"="X"."PERID" AND "X"."RECID"="D"."RECID" AND "D"."RSTID"="R"."RSTID" AND "D"."PRTID"="PRT"."PRTID" AND "D"."CASID"="C"."CASID" AND "C"."PATID"="P"."PATID" AND ("PRT"."PRTID"=72 OR "PRT"."PRTID"=165)
      Access Path: index (FullScan)
        Index: IRECRECORDIDPRTID
        resc_io: 43172.00  resc_cpu: 2118737558
        ix_sel: 1  ix_sel_with_filters: 6.2392e-005
        NL Join: Cost: 86121.14  Resp: 86121.14  Degree: 1
          Cost_io: 85924.00  Cost_cpu: 5909611610
          Resp_io: 85924.00  Resp_cpu: 5909611610
      Access Path: index (AllEqJoinGuess)
        Index: RECIDXPRTID
        resc_io: 217.00  resc_cpu: 13774732
        ix_sel: 0.0074074  ix_sel_with_filters: 6.2392e-005
        NL Join (ordered): Cost: 338.30  Resp: 338.30  Degree: 1
          Cost_io: 337.00  Cost_cpu: 39079056
          Resp_io: 337.00  Resp_cpu: 39079056
      ****** trying bitmap/domain indexes ******
      ****** finished trying bitmap/domain indexes ******
      Best NL cost: 338.30
              resc: 338.30 resc_io: 337.00 resc_cpu: 39079056
              resp: 338.30 resp_io: 337.00 resp_cpu: 39079056
    Join Card:  1048.85 = outer (2.00) * inner (70797.49) * sel (0.0074074)
    Join Card - Rounded: 1049 Computed: 1048.85
    SM Join
      Outer table:
        resc: 2.00  card 2.00  bytes: 11  deg: 1  resp: 2.00
      Inner table: XSVPATIENTRECORD  Alias: D
        resc: 13387.34  card: 70797.49  bytes: 59  deg: 1  resp: 13387.34
        using dmeth: 2  #groups: 1
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:     651 Row size:           75 Total Rows:          70797
          Initial runs:         2 Merge passes:        1 IO Cost / pass:        354
          Total IO sort cost: 1005      Total CPU sort cost: 97398867
          Total Temp space used: 11969000
      SM join: Resc: 14397.59  Resp: 14397.59  [multiMatchCost=0.00]
      SM cost: 14397.59
         resc: 14397.59 resc_io: 14389.81 resc_cpu: 233098276
         resp: 14397.59 resp_io: 14389.81 resp_cpu: 233098276
    HA Join
      Outer table:
        resc: 2.00  card 2.00  bytes: 11  deg: 1  resp: 2.00
      Inner table: XSVPATIENTRECORD  Alias: D
        resc: 13387.34  card: 70797.49  bytes: 59  deg: 1  resp: 13387.34
        using dmeth: 2  #groups: 1
        Cost per ptn: 0.74  #ptns: 1
        hash_area: 124 (max=19098) buildfrag: 1  probefrag: 614  ppasses: 1
      Hash join: Resc: 13390.08  Resp: 13390.08  [multiMatchCost=0.00]
      HA cost: 13390.08
         resc: 13390.08 resc_io: 13384.81 resc_cpu: 157767585
         resp: 13390.08 resp_io: 13384.81 resp_cpu: 157767585
    Best:: JoinMethod: NestedLoop
           Cost: 338.30  Degree: 1  Resp: 338.30  Card: 1048.85  Bytes: 70
    Now joining: XMFREPORTSTATE[R]#2
    NL Join
      Outer table: Card: 1048.85  Cost: 338.30  Resp: 338.30  Degree: 1  Bytes: 70
      Inner table: XMFREPORTSTATE  Alias: R
      Access Path: TableScan
        NL Join:  Cost: 1489.04  Resp: 1489.04  Degree: 1
          Cost_io: 1486.00  Cost_cpu: 91090703
          Resp_io: 1486.00  Resp_cpu: 91090703
      Access Path: index (UniqueScan)
        Index: PK_XMFREPORTSTATE
        resc_io: 1.00  resc_cpu: 8697
        ix_sel: 0.030303  ix_sel_with_filters: 0.030303
        NL Join: Cost: 1387.61  Resp: 1387.61  Degree: 1
          Cost_io: 1386.00  Cost_cpu: 48201937
          Resp_io: 1386.00  Resp_cpu: 48201937
      Access Path: index (AllEqUnique)
        Index: PK_XMFREPORTSTATE
        resc_io: 1.00  resc_cpu: 8697
        ix_sel: 0.030303  ix_sel_with_filters: 0.030303
        NL Join: Cost: 1387.61  Resp: 1387.61  Degree: 1
          Cost_io: 1386.00  Cost_cpu: 48201937
          Resp_io: 1386.00  Resp_cpu: 48201937
      ****** trying bitmap/domain indexes ******
      ****** finished trying bitmap/domain indexes ******
      Best NL cost: 1387.61
              resc: 1387.61 resc_io: 1386.00 resc_cpu: 48201937
              resp: 1387.61 resp_io: 1386.00 resp_cpu: 48201937
    Join Card:  238.38 = outer (1048.85) * inner (5.00) * sel (0.045455)
    Join Card - Rounded: 238 Computed: 238.38
    SM Join
      Outer table:
        resc: 338.30  card 1048.85  bytes: 70  deg: 1  resp: 338.30
      Inner table: XMFREPORTSTATE  Alias: R
        resc: 3.00  card: 5.00  bytes: 13  deg: 1  resp: 3.00
        using dmeth: 2  #groups: 1
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:      12 Row size:           87 Total Rows:           1049
          Initial runs:         1 Merge passes:        0 IO Cost / pass:          0
          Total IO sort cost: 0      Total CPU sort cost: 30450620
          Total Temp space used: 0
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:       1 Row size:           25 Total Rows:              5
          Initial runs:         1 Merge passes:        0 IO Cost / pass:          0
          Total IO sort cost: 0      Total CPU sort cost: 29976876
          Total Temp space used: 0
      SM join: Resc: 343.32  Resp: 343.32  [multiMatchCost=0.00]
      SM cost: 343.32
         resc: 343.32 resc_io: 340.00 resc_cpu: 99556133
         resp: 343.32 resp_io: 340.00 resp_cpu: 99556133
    HA Join
      Outer table:
        resc: 338.30  card 1048.85  bytes: 70  deg: 1  resp: 338.30
      Inner table: XMFREPORTSTATE  Alias: R
        resc: 3.00  card: 5.00  bytes: 13  deg: 1  resp: 3.00
        using dmeth: 2  #groups: 1
        Cost per ptn: 0.51  #ptns: 1
        hash_area: 124 (max=19098) buildfrag: 11  probefrag: 1  ppasses: 1
      Hash join: Resc: 341.81  Resp: 341.81  [multiMatchCost=0.00]
    HA Join (swap)
      Outer table:
        resc: 3.00  card 5.00  bytes: 13  deg: 1  resp: 3.00
      Inner table: XSVPATIENTRECORD  Alias: D
        resc: 338.30  card: 1048.85  bytes: 70  deg: 1  resp: 338.30
        using dmeth: 2  #groups: 1
        Cost per ptn: 0.50  #ptns: 1
        hash_area: 124 (max=19098) buildfrag: 1  probefrag: 11  ppasses: 1
      Hash join: Resc: 341.81  Resp: 341.81  [multiMatchCost=0.00]
      HA cost: 341.81
         resc: 341.81 resc_io: 340.00 resc_cpu: 54222464
         resp: 341.81 resp_io: 340.00 resp_cpu: 54222464
    Best:: JoinMethod: Hash
           Cost: 341.81  Degree: 1  Resp: 341.81  Card: 238.38  Bytes: 83
    Now joining: XSVRECPERXREF[X]#5
    NL Join
      Outer table: Card: 238.38  Cost: 341.81  Resp: 341.81  Degree: 1  Bytes: 83
      Inner table: XSVRECPERXREF  Alias: X
      Access Path: TableScan
        NL Join:  Cost: 280914.52  Resp: 280914.52  Degree: 1
          Cost_io: 275752.00  Cost_cpu: 154753663635
          Resp_io: 275752.00  Resp_cpu: 154753663635
    kkofmx: index filter:
    ("X"."RPXTYPE"=1 OR "X"."RPXTYPE"=2 OR "X"."RPXTYPE"=3 OR "X"."RPXTYPE"=5 OR "X"."RPXTYPE"=6) AND ("R"."RSTSTATE"=202 OR "R"."RSTSTATE"=203 OR "R"."RSTSTATE"=206 OR "R"."RSTSTATE"=208 OR "R"."RSTSTATE"=210) AND "PER"."PERID"="X"."PERID" AND "X"."RECID"="D"."RECID" AND "D"."RSTID"="R"."RSTID" AND "D"."PRTID"="PRT"."PRTID" AND "D"."CASID"="C"."CASID" AND "C"."PATID"="P"."PATID" AND ("PRT"."PRTID"=72 OR "PRT"."PRTID"=165)
      Access Path: index (AllEqJoinGuess)
        Index: RPXIDXRECID
        resc_io: 3.00  resc_cpu: 23984
        ix_sel: 1.6577e-006  ix_sel_with_filters: 1.6577e-006
        NL Join: Cost: 835.02  Resp: 835.02  Degree: 1
          Cost_io: 833.08  Cost_cpu: 58098995
          Resp_io: 833.08  Resp_cpu: 58098995
      Access Path: index (RangeScan)
        Index: URPXRECPERTYPE
        resc_io: 3.00  resc_cpu: 23274
        ix_sel: 1.6577e-006  ix_sel_with_filters: 9.4822e-007
        NL Join: Cost: 835.02  Resp: 835.02  Degree: 1
          Cost_io: 833.08  Cost_cpu: 58181005
          Resp_io: 833.08  Resp_cpu: 58181005
      ****** trying bitmap/domain indexes ******
      ****** finished trying bitmap/domain indexes ******
      Best NL cost: 835.02
              resc: 835.02 resc_io: 833.08 resc_cpu: 58098995
              resp: 835.02 resp_io: 833.08 resp_cpu: 58098995
    Join Card:  378.67 = outer (238.38) * inner (835508.80) * sel (1.9013e-006)
    Join Card - Rounded: 379 Computed: 378.67
    SM Join
      Outer table:
        resc: 341.81  card 238.38  bytes: 83  deg: 1  resp: 341.81
      Inner table: XSVRECPERXREF  Alias: X
        resc: 1180.68  card: 835508.80  bytes: 17  deg: 1  resp: 1180.68
        using dmeth: 2  #groups: 1
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:       3 Row size:          102 Total Rows:            238
          Initial runs:         1 Merge passes:        0 IO Cost / pass:          0
          Total IO sort cost: 0      Total CPU sort cost: 30061009
          Total Temp space used: 0
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:    2967 Row size:           29 Total Rows:         835509
          Initial runs:         2 Merge passes:        1 IO Cost / pass:       1608
          Total IO sort cost: 4575      Total CPU sort cost: 843575684
          Total Temp space used: 53699000
      SM join: Resc: 6126.64  Resp: 6126.64  [multiMatchCost=0.00]
      SM cost: 6126.64
         resc: 6126.64 resc_io: 6074.00 resc_cpu: 1577856809
         resp: 6126.64 resp_io: 6074.00 resp_cpu: 1577856809
    HA Join
      Outer table:
        resc: 341.81  card 238.38  bytes: 83  deg: 1  resp: 341.81
      Inner table: XSVRECPERXREF  Alias: X
        resc: 1180.68  card: 835508.80  bytes: 17  deg: 1  resp: 1180.68
        using dmeth: 2  #groups: 1
        Cost per ptn: 3.29  #ptns: 1
        hash_area: 124 (max=19098) buildfrag: 3  probefrag: 2958  ppasses: 1
      Hash join: Resc: 1525.78  Resp: 1525.78  [multiMatchCost=0.00]
      HA cost: 1525.78
         resc: 1525.78 resc_io: 1499.00 resc_cpu: 802794893
         resp: 1525.78 resp_io: 1499.00 resp_cpu: 802794893
    Best:: JoinMethod: NestedLoop
           Cost: 835.02  Degree: 1  Resp: 835.02  Card: 378.67  Bytes: 100
    Now joining: PERPERSON[PER]#0
    NL Join
      Outer table: Card: 378.67  Cost: 835.02  Resp: 835.02  Degree: 1  Bytes: 100
      Inner table: PERPERSON  Alias: PER
      Access Path: TableScan
        NL Join:  Cost: 409701.78  Resp: 409701.78  Degree: 1
          Cost_io: 408070.08  Cost_cpu: 48912477468
          Resp_io: 408070.08  Resp_cpu: 48912477468
      Access Path: index (UniqueScan)
        Index: PKPER
        resc_io: 2.00  resc_cpu: 16603
        ix_sel: 2.7682e-006  ix_sel_with_filters: 2.7682e-006
        NL Join: Cost: 1593.23  Resp: 1593.23  Degree: 1
          Cost_io: 1591.08  Cost_cpu: 64391486
          Resp_io: 1591.08  Resp_cpu: 64391486
      Access Path: index (AllEqJoin)
        Index: PERIDXAGNEXTID
        resc_io: 2.00  resc_cpu: 15703
        ix_sel: 0.001065  ix_sel_with_filters: 0.001065
        NL Join: Cost: 1593.22  Resp: 1593.22  Degree: 1
          Cost_io: 1591.08  Cost_cpu: 64050386
          Resp_io: 1591.08  Resp_cpu: 64050386
      Access Path: index (AllEqUnique)
        Index: PKPER
        resc_io: 2.00  resc_cpu: 16603
        ix_sel: 2.7682e-006  ix_sel_with_filters: 2.7682e-006
        NL Join: Cost: 1593.23  Resp: 1593.23  Degree: 1
          Cost_io: 1591.08  Cost_cpu: 64391486
          Resp_io: 1591.08  Resp_cpu: 64391486
      ****** trying bitmap/domain indexes ******
      Access Path: index (AllEqJoin)
        Index: PERIDXAGNEXTID
        resc_io: 1.00  resc_cpu: 8171
        ix_sel: 0.001065  ix_sel_with_filters: 0.001065
        NL Join: Cost: 1214.12  Resp: 1214.12  Degree: 1
          Cost_io: 1212.08  Cost_cpu: 61195971
          Resp_io: 1212.08  Resp_cpu: 61195971
      Access Path: index (AllEqUnique)
        Index: PKPER
        resc_io: 1.00  resc_cpu: 9021
        ix_sel: 2.7682e-006  ix_sel_with_filters: 2.7682e-006
        NL Join: Cost: 1214.13  Resp: 1214.13  Degree: 1
          Cost_io: 1212.08  Cost_cpu: 61518121
          Resp_io: 1212.08  Resp_cpu: 61518121
      Access path: Bitmap index - rejected
        Cost: 3293.30 Cost_io: 3287.25 Cost_cpu: 181295210 Sel: 9.5086e-007
        Not believed to be index-only
      ****** finished trying bitmap/domain indexes ******
      Best NL cost: 1593.22
              resc: 1593.22 resc_io: 1591.08 resc_cpu: 64050386
              resp: 1593.22 resp_io: 1591.08 resp_cpu: 64050386
    Join Card:  0.34 = outer (378.67) * inner (1.00) * sel (8.9286e-004)
    Join Card - Rounded: 1 Computed: 0.34
    SM Join
      Outer table:
        resc: 835.02  card 378.67  bytes: 100  deg: 1  resp: 835.02
      Inner table: PERPERSON  Alias: PER
        resc: 2.00  card: 1.00  bytes: 29  deg: 1  resp: 2.00
        using dmeth: 2  #groups: 1
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:       6 Row size:          120 Total Rows:            379
          Initial runs:         1 Merge passes:        0 IO Cost / pass:          0
          Total IO sort cost: 0      Total CPU sort cost: 30122624
          Total Temp space used: 0
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:       1 Row size:           42 Total Rows:              1
          Initial runs:         1 Merge passes:        0 IO Cost / pass:          0
          Total IO sort cost: 0      Total CPU sort cost: 29976353
          Total Temp space used: 0
      SM join: Resc: 839.02  Resp: 839.02  [multiMatchCost=0.00]
      SM cost: 839.02
         resc: 839.02 resc_io: 835.08 resc_cpu: 118213674
         resp: 839.02 resp_io: 835.08 resp_cpu: 118213674
    HA Join
      Outer table:
        resc: 835.02  card 378.67  bytes: 100  deg: 1  resp: 835.02
      Inner table: PERPERSON  Alias: PER
        resc: 2.00  card: 1.00  bytes: 29  deg: 1  resp: 2.00
        using dmeth: 2  #groups: 1
        Cost per ptn: 0.50  #ptns: 1
        hash_area: 124 (max=19098) buildfrag: 6  probefrag: 1  ppasses: 1
      Hash join: Resc: 837.52  Resp: 837.52  [multiMatchCost=0.00]
    HA Join (swap)
      Outer table:
        resc: 2.00  card 1.00  bytes: 29  deg: 1  resp: 2.00
      Inner table: XSVRECPERXREF  Alias: X
        resc: 835.02  card: 378.67  bytes: 100  deg: 1  resp: 835.02
        using dmeth: 2  #groups: 1
        Cost per ptn: 0.50  #ptns: 1
        hash_area: 124 (max=19098) buildfrag: 1  probefrag: 6  ppasses: 1
      Hash join: Resc: 837.52  Resp: 837.52  [multiMatchCost=0.00]
      HA cost: 837.52
         resc: 837.52 resc_io: 835.08 resc_cpu: 73140924
         resp: 837.52 resp_io: 835.08 resp_cpu: 73140924
    Best:: JoinMethod: Hash
           Cost: 837.52  Degree: 1  Resp: 837.52  Card: 0.34  Bytes: 129
    Now joining: ADTCASE[C]#6
    NL Join
      Outer table: Card: 0.34  Cost: 837.52  Resp: 837.52  Degree: 1  Bytes: 129
      Inner table: ADTCASE  Alias: C
      Access Path: TableScan
        NL Join:  Cost: 6458.89  Resp: 6458.89  Degree: 1
          Cost_io: 6414.08  Cost_cpu: 1343349671
          Resp_io: 6414.08  Resp_cpu: 1343349671
      Access Path: index (UniqueScan)
        Index: PK_ADTCASE
        resc_io: 2.00  resc_cpu: 16573
        ix_sel: 3.9499e-007  ix_sel_with_filters: 3.9499e-007
        NL Join: Cost: 839.52  Resp: 839.52  Degree: 1
          Cost_io: 837.08  Cost_cpu: 73157497
          Resp_io: 837.08  Resp_cpu: 73157497
      Access Path: index (AllEqUnique)
        Index: PK_ADTCASE
        resc_io: 2.00  resc_cpu: 16573
        ix_sel: 3.9478e-007  ix_sel_with_filters: 3.9478e-007
        NL Join: Cost: 839.52  Resp: 839.52  Degree: 1
          Cost_io: 837.08  Cost_cpu: 73157497
          Resp_io: 837.08  Resp_cpu: 73157497
      ****** trying bitmap/domain indexes ******
      ****** finished trying bitmap/domain indexes ******
      Best NL cost: 839.52
              resc: 839.52 resc_io: 837.08 resc_cpu: 73157497
              resp: 839.52 resp_io: 837.08 resp_cpu: 73157497
    Join Card:  0.34 = outer (0.34) * inner (2531689.00) * sel (3.9478e-007)
    Join Card - Rounded: 1 Computed: 0.34
    SM Join
      Outer table:
        resc: 837.52  card 0.34  bytes: 129  deg: 1  resp: 837.52
      Inner table: ADTCASE  Alias: C
        resc: 5621.37  card: 2531689.00  bytes: 34  deg: 1  resp: 5621.37
        using dmeth: 2  #groups: 1
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:       1 Row size:          152 Total Rows:              1
          Initial runs:         1 Merge passes:        0 IO Cost / pass:          0
          Total IO sort cost: 0      Total CPU sort cost: 29976353
          Total Temp space used: 0
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:   14878 Row size:           48 Total Rows:        2531689
          Initial runs:         2 Merge passes:        1 IO Cost / pass:       8060
          Total IO sort cost: 22938      Total CPU sort cost: 2822696818
          Total Temp space used: 264217000
      SM join: Resc: 29492.06  Resp: 29492.06  [multiMatchCost=0.00]
      SM cost: 29492.06
         resc: 29492.06 resc_io: 29352.08 resc_cpu: 4196022842
         resp: 29492.06 resp_io: 29352.08 resp_cpu: 4196022842
    HA Join
      Outer table:
        resc: 837.52  card 0.34  bytes: 129  deg: 1  resp: 837.52
      Inner table: ADTCASE  Alias: C
        resc: 5621.37  card: 2531689.00  bytes: 34  deg: 1  resp: 5621.37
        using dmeth: 2  #groups: 1
        Cost per ptn: 8.95  #ptns: 1
        hash_area: 124 (max=19098) buildfrag: 1  probefrag: 14217  ppasses: 1
      Hash join: Resc: 6467.84  Resp: 6467.84  [multiMatchCost=0.00]
      HA cost: 6467.84
         resc: 6467.84 resc_io: 6414.08 resc_cpu: 1611506897
         resp: 6467.84 resp_io: 6414.08 resp_cpu: 1611506897
    Best:: JoinMethod: NestedLoop
           Cost: 839.52  Degree: 1  Resp: 839.52  Card: 0.34  Bytes: 163
    Now joining: ADTPATIENT[P]#4
    NL Join
      Outer table: Card: 0.34  Cost: 839.52  Resp: 839.52  Degree: 1  Bytes: 163
      Inner table: ADTPATIENT  Alias: P
      Access Path: TableScan
        NL Join:  Cost: 2172.07  Resp: 2172.07  Degree: 1
          Cost_io: 2161.08  Cost_cpu: 329395913
          Resp_io: 2161.08  Resp_cpu: 329395913
      Access Path: index (UniqueScan)
        Index: PK_ADTPATIENT
        resc_io: 2.00  resc_cpu: 16533
        ix_sel: 1.8294e-006  ix_sel_with_filters: 1.8294e-006
        NL Join: Cost: 841.52  Resp: 841.52  Degree: 1
          Cost_io: 839.08  Cost_cpu: 73174030
          Resp_io: 839.08  Resp_cpu: 73174030
      Access Path: index (AllEqUnique)
        Index: PK_ADTPATIENT
        resc_io: 2.00  resc_cpu: 16533
        ix_sel: 1.8294e-006  ix_sel_with_filters: 1.8294e-006
        NL Join: Cost: 841.52  Resp: 841.52  Degree: 1
          Cost_io: 839.08  Cost_cpu: 73174030
          Resp_io: 839.08  Resp_cpu: 73174030
      ****** trying bitmap/domain indexes ******
      ****** finished trying bitmap/domain indexes ******
      Best NL cost: 841.52
              resc: 841.52 resc_io: 839.08 resc_cpu: 73174030
              resp: 841.52 resp_io: 839.08 resp_cpu: 73174030
    Join Card:  0.34 = outer (0.34) * inner (546621.00) * sel (1.8294e-006)
    Join Card - Rounded: 1 Computed: 0.34
    SM Join
      Outer table:
        resc: 839.52  card 0.34  bytes: 163  deg: 1  resp: 839.52
      Inner table: ADTPATIENT  Alias: P
        resc: 1332.55  card: 546621.00  bytes: 41  deg: 1  resp: 1332.55
        using dmeth: 2  #groups: 1
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:       1 Row size:          190 Total Rows:              1
          Initial runs:         1 Merge passes:        0 IO Cost / pass:          0
          Total IO sort cost: 0      Total CPU sort cost: 29976353
          Total Temp space used: 0
        SORT resource      Sort statistics
          Sort width:         445 Area size:      390144 Max Area size:    78223360
          Degree:               1
          Blocks to Sort:    3748 Row size:           56 Total Rows:         546621
          Initial runs:         2 Merge passes:        1 IO Cost / pass:       2032
          Total IO sort cost: 5780      Total CPU sort cost: 591684408
          Total Temp space used: 65872000
      SM join: Resc: 7972.81  Resp: 7972.81  [multiMatchCost=0.00]
      SM cost: 7972.81
         resc: 7972.81 resc_io: 7941.08 resc_cpu: 951056673
         resp: 7972.81 resp_io: 7941.08 resp_cpu: 951056673
    HA Join
      Outer table:
        resc: 839.52  card 0.34  bytes: 163  deg: 1  resp: 839.52
      Inner table: ADTPATIENT  Alias: P
        resc: 1332.55  card: 546621.00  bytes: 41  deg: 1  resp: 1332.55
        using dmeth: 2  #groups: 1
        Cost per ptn: 2.32  #ptns: 1
        hash_area: 124 (max=19098) buildfrag: 1  probefrag: 3537  ppasses: 1
      Hash join: Resc: 2174.39  Resp: 2174.39  [multiMatchCost=0.00]
      HA cost: 2174.39
         resc: 2174.39 resc_io: 2161.08 resc_cpu: 399046339
         resp: 2174.39 resp_io: 2161.08 resp_cpu: 399046339
    Plan cardinality mismatch: best card= 0.33796097408   curr card= 0.33791727990
    Best:: JoinMethod: NestedLoop
           Cost: 841.52  Degree: 1  Resp: 841.52  Card: 0.34  Bytes: 204
    Best so far: Table#: 1  cost: 2.0007  card: 2.0000  bytes: 22
                 Table#: 3  cost: 338.3037  card: 1048.8517  bytes: 73430
                 Table#: 2  cost: 341.8088  card: 238.3754  bytes: 19754
                 Table#: 5  cost: 835.0191  card: 378.6697  bytes: 37900
                 Table#: 0  cost: 837.5209  card: 0.3381  bytes: 129
                 Table#: 6  cost: 839.5214  card: 0.3379  bytes: 163
                 Table#: 4  cost: 841.5220  card: 0.3379  bytes: 204
    tage

  • Request for help in tuning the server which is running opmn process.

    Hi Folks,
    I request for an help in tuning the server which is running oracle app server opmn process , It is chewing arround 40% of the CPU resource,and our sysadmin is back of me to resolve this issues.
    any feedback on this is highly appriciated...
    Thanks in advance

    Re: How to Achieve Performance Tuning
    I hope you may find answers here..
    Sharma

  • Help in Joining query

    Hi, How I can get desired result. I am joining two tables but if date timeline does't exist in #two table then should show hyphen (-). Please help on this query. Thanks.
    create table #one (code_p char(4), code_h char(2), code_date datetime)
    insert into #one values ('DEHG','2','2010-01-01')
    insert into #one values ('DEHG','2','2011-01-01')
    insert into #one values ('DEHC','2','2009-01-01')
    insert into #one values ('DEHG','2','2012-01-01')
    create table #two (code_p char(4), code_h char(2), code_date datetime)
    insert into #two values ('DEHG','2','2010-01-01')
    insert into #two values ('DEHC','2','2009-01-01')
    select p.code_p code_p_one, p.code_h code_h_one, p.code_date code_date_one,
    p.code_p code_p_two, p.code_h code_h_two, p.code_date code_date_two from #one p join #two a on p.code_p = a.code_p
    --Result from the above query
    code_p_one code_h_one code_date_one code_p_two code_h_two code_date_two
    DEHG     2    2010-01-01     DEHG     2     2010-01-01
    DEHG     2    2011-01-01     DEHG     2     2010-01-01
    DEHC     2    2009-01-01     DEHG     2     2009-01-01
    DEHC     2    2012-01-01     DEHG     2     2009-01-01
    --Desired result
    code_p_one code_h_one code_date_one code_p_two code_h_two code_date_two
    DEHG     2    2010-01-01     DEHG     2     2010-01-01
    DEHG     2    2011-01-01     DEHG     2     -
    DEHC     2    2009-01-01     DEHG     2     2009-01-01
    DEHC     2    2012-01-01     DEHG     2     -

    Try this:
    select p.code_p code_p_one, p.code_h code_h_one, p.code_date code_date_one,
    p.code_p code_p_two, p.code_h code_h_two, ISNULL(CONVERT(varchar(50),a.code_date,121),'-') code_date_two
    from #one p
    left join #two a on p.code_p = a.code_p and p.code_date=a.code_date
    If this post answers your query, please click "Mark As Answer" or "Vote as Helpful".

  • Help with my query

    Hello all,
    Total newbie to this pl/sql stuff. So, deseperately need help in my query.
    BOOKING_ID     BOOKING_STATUS     BOOKING_DATE     BOOKING_TIME     BOOKING_DATE_TIME
    1234567     CANCELLED     20090301     37252     5/1/2010 10:20
    1234567     CANCELLED 20090301     44229     5/1/2010 12:17
    1234567     BOOKED     20090301     39462     5/1/2010 10:57
    1234567     CANCELLED     20090301     43549     5/1/2010 12:05
    9671111     BOOKED     20090301     68124     5/1/2010 12:57
    9671111     CANCELLED     20090301     45001     5/1/2010 12:05
    How do I write my query such that I would get the following results:
    BOOKING_ID     BOOKING_STATUS     BOOKING_DATE     BOOKING_TIME     BOOKING_DATE_TIME
    9671111     BOOKED     20090301     68124     2/4/2010 12:17
    Basically, I am looking at the latest BOOKING_TIME and making sure the BOOKING_STATUS=BOOKED, if not, don't even bother bring back the result. Hence, you see that BOOKING_ID=1234567 is not required since at the latest BOOKING_TIME=44229, the BOOKING_STATUS=CANCELLED.
    Any help is greatly appreciated.
    Thank you in advance for your help.
    Stanley Ho

    Hi, Stanley,
    Welcome to the forum!
    Whenever you have a question, please post your sample data in a form that people can actually use. CREATE TABLE and INSERT statements are perfect.
    For example:
    CREATE TABLE     booking
    (     booking_id          NUMBER (8)
    ,     booking_status          VARCHAR2 (10)
    ,     booking_date_time     DATE
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (1234567,        'CANCELLED',        TO_DATE ('5/1/2010 10:20', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (1234567,        'CANCELLED',        TO_DATE ('5/1/2010 12:17', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (1234567,        'BOOKED',        TO_DATE ('5/1/2010 10:57', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (1234567,        'CANCELLED',        TO_DATE ('5/1/2010 12:05', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (9671111,        'BOOKED',        TO_DATE ('5/1/2010 12:57', 'MM/DD/YYYY HH24:MI'));
    INSERT INTO  booking (booking_id, booking_status, booking_date_time)
                  VALUES (9671111,        'CANCELLED',        TO_DATE ('5/1/2010 12:05', 'MM/DD/YYYY HH24:MI'));What you want is called a Top-N Query .
    Here's one way to do it:
    WITH     got_rnum  AS
         SELECT     booking.*
         ,     ROW_NUMBER () OVER ( PARTITION BY  booking_id
                                   ORDER BY          booking_date_time     DESC
                           ) AS rnum
         FROM    booking
    SELECT     booking_id
    ,     booking_status
    ,     TO_CHAR (booking_date_time, 'YYYYMMDD')               AS booking_date
    ,     TO_CHAR (booking_date_time, 'SSSSS')               AS booking_time
    ,     TO_CHAR (booking_date_time, 'MM/DD/YYYY HH24:MI')     AS booking_date_time
    FROM     got_rnum
    WHERE     rnum          = 1
    AND     booking_status     = 'BOOKED'
    ;Notice that you don't need PL/SQL to do this; plain old SQL is good enough.
    Of course, if you're using PL/SQL for other reasons, you can use a query like this within PL/SQL.
    Dates (including time of day) should always be stored in DATE columns.
    If you have a DATE column, like booking_date_time, then there's no need for redundant date and time columns.
    You can always display just the year-month-day, or just the time, in any format, as I did above.
    The output from the query above, with the data above, is:
    BOOKING_ID BOOKING_ST BOOKING_ BOOKI BOOKING_DATE_TIM
       9671111 BOOKED     20100501 46620 05/01/2010 12:57I realize the booking_date and booking_time columns aren't quite what you posted. If they are not derivable from booking_date_time, then you probably do need separate columns for them, and those columns can easily be added to the query above.
    Edited by: Frank Kulash on Feb 5, 2010 4:41 PM
    KEEP (DENSE_RANK ...) , like Max used below, is a great tool to have in your kit. The problem with it is that you have to repeat a lot of stuff for every column, so the more columns you have in your output, the more tedious it gets. ROW_NUMBER sclaes much better, and is adaptable to more situations. I suggest you master ROW_NUMBER first, and look into KEEP (DENSE_RANK ...) later.

  • Please help with tricky query

    I need help with SQL query (if it can be accomplished with query at all).
    I'm going to create a table with structure similar to:
    Article_Name varchar2(30), Author_Name varchar2(30), Position varchar2(2). Position field is basicly position of an article author in the author list, e.g. if there is one author, his/her position is 0, if 2, then 1st author is 0, second is 1, etc.
    Article_Name Author_Name Position
    Outer Space Smith 0
    Outer Space Blake 1
    How can I automate creation of Position, based on number of authors on the fly? Let's say I have original table without Position, but I want to create a new table that will have this information.
    Regards

    If you have an existing table whose structure doesn't tell you what position the author is in, what's the algorithm you'd use to determine who was the first author, the second author, etc? If you issue a select query on a table without providing an "order by" clause, Oracle makes no guarantees about the order in which it retrieves rows.
    As an aside, why would you store position number in a varchar2 field? If it's a number, it ought to be stored as a number.
    Justin

  • Need help with SQL Query with Inline View + Group by

    Hello Gurus,
    I would really appreciate your time and effort regarding this query. I have the following data set.
    Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
    1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
    1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
    7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
    4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
    Please Ignore '----', added it for clarity
    I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
    The query should return the following data set
    Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
    1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
    7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
    4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
    The following is my query. I am kind of lost.
    select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
    from (
    select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
    from INVOICE
    group by sequence_id,check_date, check_number, invoice_number, vendor_number
    ) A, INVOICE B
    where A.sequence_id = B.sequence_id
    Thanks,
    Nick

    It looks like it is a duplicate thread - correct me if i'm wrong in this case ->
    Need help with SQL Query with Inline View + Group by
    Regards.
    Satyaki De.

  • Need Help on Sql Query

    Hi,
    I have a client requirement to show a report on the device availability. The report should show the output as
    Node Availability%
    Formula for Availability = (Total No. of Failed/Total No. rows) * 100
    My Oracle Table has the following data
    NODE     SUMMARY
    172.16.10.55     Default Interface Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.55     Default Interface Ping restore for 172.16.10.55
    172.16.10.55     Default Chassis Ping restore for 172.16.10.55
    172.16.10.55     Default Chassis Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.55     Default Chassis Ping restore for 172.16.10.55
    172.16.10.55     Default Chassis Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.55     Default Chassis Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.55     Default Interface Ping restore for 172.16.10.55
    172.16.10.55     Default Interface Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.56     Default Chassis Ping restore for 172.16.10.56
    172.16.10.56     Default Interface Ping fail for 172.16.10.56: ICMP timeout
    172.16.10.56     Default Chassis Ping fail for 172.16.10.56: ICMP timeout
    172.16.10.56     Default Chassis Ping restore for 172.16.10.56
    172.16.10.56     Default Chassis Ping fail for 172.16.10.56: ICMP timeout
    172.16.10.56     Default Chassis Ping restore for 172.16.10.56
    172.16.10.56     Default Chassis Ping restore for 172.16.10.56
    172.16.10.56     Default Interface Ping fail for 172.16.10.56: ICMP timeout
    In the above table the Summary column has the details like 'Ping fail' , 'Ping restore' for each Node. So, for each Node I have to compute the Total Ping Fail / (Total Ping Fail + Ping Restore) * 100 to compute the availability %.
    My output should be like the below
    Node Availability%
    172.16.10.55 55.55
    172.16.10.56 54
    Can someone please help me with query.
    I appreciate your help in advance.
    Thanks.
    Regards,
    RaviShankar.

    My Oracle Table has the following dataThat's great, but if you want maximum response to your question, then post CREATE TABLE + INSERT INTO statements.
    I currently do not have the time to turn your data into them.
    And always post the database version you're using.
    http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html
    Also use the {noformat}{noformat} tag in order to post examples that benefit from staying formatted and thus readable when posted on the forum.
    Simply put the tag before and after your examples.
    For example, when you type:
    {noformat}select *
    from dual;{noformat}
    it will appear as:select *
    from dual;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Need helping in writing query for finding percentage of duration

    Can any one please help in writing query for this.
    The table is like this :-
    ID     Region     Month     Duration
    I1 R1     Jan     80
    I2     R2     Jan     70
    I3     R1     Jan     70
    I4     R3     Jan     40
    I5     R1     Feb     80
    I6     R2     Feb     30
    I7     R3     Mar     100
    I want to write a query to find
    % of duration for each and every region against each and every month.
    Please help in solving this query. I am in urgent need of this.
    Thanks in advance.

    I also have to do in MS Access 2003You also have to ask into an other forum since here it's an Oracle forum, to try to find Oracle solution.
    Nicolas.

  • Can we Definine Input Help in a Query

    Hi
    Is it possible to define input help or search help in a query the same way as it is done in R3?? I will like to load specific value from a psa.
    Regards

    Hi Jacques,
    Providing Customized Input help in query directly is not possible. Instead we can provide Input help in WAD layouts for a query with the help of another Query.
    This can be made available for the users to choose with using controls such as Drop Down, Check boxes, Buttons and so on..
    Hope this helps.
    Regards.
    Shafi.

  • Help wit the query

    I need help with the query
    Here is what I need
    For a particular comm record if there is no Salary record where comm:Date = Salary:Date, then
    • Find maximum dated Salary record as of comm:Date.
    • Clone this record and set Salary:Date = comm:Date
    • Set Salary:rate = comm:rate
    Like wise for a particular Salary record If there is no comm record where Salary:Date = comm:Date then
    • Find maximum effective dated comm record as of Salary:Date
    • Apply Rate 2 amount from this maximum effective dated record to Salary record i.e. Set Salary:rate = comm:rate
    Example
    Salary Table :
    ID Sal_Date Rate Hours
    1 07/01/2011 400.00 40
    2 02/15/2011 200.00 40
    3 01/01/2011 160.00 40
    Sal_comm Table:
    Sal_Date comm_Rate
    1 07/01/2011 10.00
    4 03/01/2011 7.50
    3 01/01/2011 4.00
    I need to merge comm_Rate column in Salary table, since there is no salary record as off 03/01/2011, I need to find the maximum dated salary record as of 03/01/2011
    i.e. the record dated 02/15/2011. Now I need to clone that salary record, set the SAL_date as 03/01/2011 and update Rate2 amount. So the record set will be like:
    Sal_Date:
    id sal_Date Rate Hours comm_Rate
    1 07/01/2011 400.00 40 10.00
    4 03/01/2011 200.00 40 7.50
    2 02/15/2011 200.00 40 4.00
    3 01/01/2011 160.00 40 4.00

    So you need all used dates as the "driving" dataset. And you need the according data for each of these.
    WITH salary_table as
    (select 1 id,to_date('07/01/2011','MM/DD/YYYY')sal_date,400 rate,40 hours from dual union all
    select 2 id,to_date('02/15/2011','MM/DD/YYYY')sal_date,200 rate,40 hours from dual union all
    select 3 id,to_date('01/01/2011','MM/DD/YYYY')sal_date,160 rate,40 hours from dual),
    sal_comm as
    (select 1 id,to_date('07/01/2011','MM/DD/YYYY')sal_date,10 comm_Rate from dual union all
    select 4 id,to_date('03/01/2011','MM/DD/YYYY')sal_date,7.5 comm_Rate from dual union all
    select 3 id,to_date('01/01/2011','MM/DD/YYYY')sal_date,4 comm_Rate from dual)
    select to_char(all_dates.sal_date,'MM/DD/YYYY') sal_date,sal.rate,sal.hours,com.comm_rate
    from (select sal_date from salary_table
          union
          select sal_date from sal_comm) all_dates
    inner join (select s1.*,lead(sal_date-1,1,to_date('31/12/9999','DD/MM/YYYY')) over (order by sal_date) next_sal_date
               from salary_table s1) sal
      on (all_dates.sal_date between sal.sal_date and sal.next_sal_date)
    inner join (select s1.*,lead(sal_date-1,1,to_date('31/12/9999','DD/MM/YYYY')) over (order by sal_date) next_sal_date
               from sal_comm s1) com
      on (all_dates.sal_date between com.sal_date and com.next_sal_date)
    order by   all_dates.sal_date desc;
    SAL_DATE   RATE                   HOURS                  COMM_RATE             
    07/01/2011 400                    40                     10                    
    03/01/2011 200                    40                     7.5                   
    02/15/2011 200                    40                     4                     
    01/01/2011 160                    40                     4                     
         

  • Need urgent help with the query - Beginer

    Hello - I need help with a query to populate data in a table.Here is the scenario.
    Source1
    MnthID BranchCod CustID SegCode FXStatus ProfStatus Profit
    200712 B1 C1 20 Y Y 100
    Source2
    MnthID BranchCod CustID ProdCode ProdIndex
    200712 B1 C1 12 1
    200712 B1 C2 12 0
    Destination
    MnthID BranchCod SegCode ProdCode CountSegCust CountProdCust ProfitProdCust
    Condition and Calculations:
    1)Source1 customer are base customers.If Source2 has customers who is not in source1 then that customer's record should not be fetched.
    2)SegCode, FX Status, ProfStatus is one variable in destination table. [ SegCode = SegCode+ FXStatus (if FXStatus = Y)+ ProfStatus (if FXStatus = Y) ]
    3)CountSegCust = CountCustID Groupby MnthID,BranchCod,SegCode Only.
    4)CountProdCust = CountCustID Groupby MnthID,BranchCod,SegCode,ProdCode (when ProdIndex = 1)
    5)ProfitProdCust = Sum of Profit of Customers Groupby MnthID,BranchCod,SegCode,ProdCode (when ProdIndex = 1)
    Apologies for bad formatting.
    Thanks in advance!!

    A total guess indeed.
    It's not clear whether some aggregation can be done (summing counts of grouped data might cause some customers being counted more than once)
    insert into destination
    select mnthid,branchcod,segcode,prodcode,countsegcust,countprodcust,profitprodcust
      from (select s1.mnthid,
                   s1.branchcod,
                   s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end segcode,
                   s2.prodcode,
                   count(s1.custid) over (partition by s1.mnthid,
                                                       s1.branchcod,
                                                       s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
                                              order by null
                                         ) countsegcust,
                   count(case proindex when 1
                                       then custid
                         end
                        ) over (partition by s1.mnthid,
                                             s1.branchcod,
                                             s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
                                             s2.prodcode
                                    order by null
                               ) countprodcust,
                   sum(case proindex when 1
                                     then profit
                       end
                      ) over (partition by s1.mnthid,
                                           s1.branchcod,
                                           s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
                                           s2.prodcode
                                  order by null
                             ) profitprodcust,
                   row_number() over (partition by s1.mnthid,
                                                   s1.branchcod,
                                                   s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
                                                   s2.prodcode
                                          order by null
                                     ) the_row
              from source1 s1,source2 s2
             where s1.mnthid = s2.mnthid
               and s1.branchcod = s2.branchcod
               and s1.custid = s2.custid
    where the_row = 1Regards
    Etbin

Maybe you are looking for