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.

Similar Messages

  • Need a help to write the query

    Hi,
    I need a small help to write the query.
    I have a table contains couponid,coupon,createdate,expirationdate,assigndate from couponday table
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.
    Thanks in advance

    Hi,
    user586 wrote:
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.If you mean expirationdate (datatype: DATE) is not within 30 days (past or future) of run time, then:
    SELECT  coupon          -- or whatever columns you want
    FROM    table_x
    WHERE   expirationdate  NOT BETWEEN  SYSDATE - 30
                                AND      SYSDATE + 30
    ;

  • How to write the query using Index

    Hi All,
    I have to fetch the records from Database table using Index, how can i write the query using Index. Can any body plz send me the sample code.
    Help Me,
    Balu.

    Hi,
    See the below Example.
    select * from vbak up to 100 rows
    into table t_vbak
    where vbeln > v_vbeln.
    sort t_vbak by vbeln descending.
    read table t_vbak index 1.
    Regards,
    Ram
    Pls reward points if helpful.

  • Need help in modifying mapping parameters of out the box mapping

    Hi There,
    I am a new bee to dac.
    Need help in modifying mapping parameters of out the box mapping, which is invoked by DAC task.
    We got a requirement to edit mapping parameter. When I go and see parameter under mappings tab in a mapping, I could not see any values in it.
    But when I set any value, and validate it. It is successful.
    Is it right way to do it?
    What my concern is, When I initially go and see parameter values under maapings tab in a mapping, they are blank.
    Where is it storing these values?
    Thanks,
    Rag

    If you modify mapping then u have to create new task in dac and dac itself craete parameter file at run time. if you want to add more parameters then do it in dac system parameters tab.
    Thanks
    Jay.

  • I need help in activating apple id to for the find my iphone app

    i need help in activating apple id to for the find my iphone app

    What sort of help are you requesting from us, your fellow users? What exactly is the problem/issue?

  • HT6114 I need help hacking into my ipod i forgot the password and i cant factory reset it because the power button is broken?!

    I need help hacking into my ipod i forgot the password and i cant factory reset it because the power button is broken?!

    If your iPod Touch, iPhone, or iPad is Broken
    Apple does not fix iDevices. Instead, they exchange yours for a refurbished or new replacement depending upon the age of your device and refurbished inventories. On rare occasions when there are no longer refurbished units for your older model, they may replace it with the next newer model.
    ATTN: Beginning July 2013 Apple Stores are now equipped to do screen repairs/replacements in-house on iPhone 5 and 5C. In some cases while you wait. According to Apple this is the beginning of equipping Apple Stores with the resources needed to do most repairs for iPhones, iPads, and iPod Touches that would not require major replacements. Later in the year the services may be extended as Apple Stores become equipped and staffed with the proper repair expertise. So, if you need a screen repaired or a broken screen replaced or have your stuck Home button fixed, call your local Apple Store to see if they are now doing these in-house.
    You may take your device to an Apple retailer for help or you may call Customer Service and arrange to send your device to Apple:
    Apple Store Customer Service at 1-800-676-2775 or visit online Help for more information.
    To contact product and tech support: Contacting Apple for support and service - this includes international calling numbers.
    You will find respective repair costs in the appropriate link:
    iPod Service Support and Costs
    iPhone Service Support and Costs
    iPad Service Support and Costs
    There are third-party firms that do repairs on iDevices, and there are places where you can order parts to DIY if you feel up to the task:
    1. iResq or Google for others.
    2. Buy and replace screen yourself: iFixit
    Locked Out, Forgot Lock or Restrictions Passcode, or Need to Restore Your Device: Several Alternative Solutions
    A
    1. iOS- Forgotten passcode or device disabled after entering wrong passcode
    2. iPhone, iPad, iPod touch: Wrong passcode results in red disabled screen
    3. Restoring iPod touch after forgotten passcode
    4. What to Do If You've Forgotten Your iPhone's Passcode
    5. iOS- Understanding passcodes
    6. iTunes 10 for Mac- Update and restore software on iPod, iPhone, or iPad
    7. iOS - Unable to update or restore
    Forgotten Restrictions Passcode Help
                iPad,iPod,iPod Touch Recovery Mode
    You will need to restore your device as New to remove a Restrictions passcode. Go through the normal process to restore your device, but when you see the options to restore as New or from a backup, be sure to choose New.
    You can restore from a backup if you have one from BEFORE you set the restrictions passcode.
    Also, see iTunes- Restoring iOS software.

  • Hello, my iphone for several times shows me this message "this iphone can't be used because the Apple Mobile Device service not started". I need help because i'm enable do download the new update. Thanks

    Hello, my iphone for several times shows me this message "this iphone can't be used because the Apple Mobile Device service not started".
    I need help, because i'm enable do download the new update.
    Thanks

    Type "Apple Mobile Device service" into the search bar at the top of this age by "Support" and read the resulting help articles.

  • NEED HELP!! i restore backup at the wrong iphone

    NEED HELP!! i restore backup at the wrong iphone
    i have 2 iphone 4s.. i already have backuped the 1st iphone, then the 2nd iphone not yet backuped.
    when i plug the 2nd iphone it restore the backup of 1st iphone.. now the 1st and 2nd iphone has the same files... the original files of 2nd iphone has gone, how to restored the original files of 2nd iphone since i restored the backup of 1st iphone.and i do not have backup for the 2nd iphone. >.< PLEASE HELP!!!!

    With no backup for the 2nd iPhone, you can't.

  • I need help installing my adobe premiere element 13. the soft ware is 3.5 GB and I have a 654.4GB space is this enought space to dowload the software. It appears the software is downloading , but when it gets to the end it gives me the error summary messa

    I need help installing my adobe premiere element 13. the soft ware is 3.5 GB and I have a 654.4GB space is this enought space to dowload the software. It appears the software is downloading , but when it gets to the end it gives me the error summary message. I have been on the phone for hours being transfered from from person to the next with no results.

    reesep
    This is not Adobe. Rather user to user. The Adobe Staff and PRE_help presence in this forum is undefined.
    The replies from here are not instantaneous. But, your fellow users all try to respond as timely as possible.
    What is your computer operating system?
    From where are you downloading Premiere Elements 13....direct from Adobe as the tryout or the purchased product? If the purchased product, do you now have a purchased serial number to go with the installation files?
    Where are you stopped in the installation....at the Sign In or before? Is the faulting module cited in any messages? What are the error messages?
    The usual questions:
    1. Are you working with the antivirus and firewall(s) disabled?
    2. Are you running the computer programs as administrator?
    3. Are you working with a pen and tablet device instead of mouse?
    4. Is your video card/graphics card up to date according to the web site of the manufacturer of the card?
    5. Are you installing the program to the default location on the Local Disc C (if Windows).
    Let us start here and then decide what next based on the details that you post. Subsequent troubleshooting may include:
    deletion of the Adobe Premiere Elements Prefs file and/or uninstall, free ccleaner run through, reinstall with antivirus and firewall(s) disabled.
    Any questions or need clarification, please do not hesitate to ask.
    Thank you.
    ATR

  • Hi, Need help. I am trying to download the latest iTunes for Windows application but on running the program it says that it can not be completed as it can not find the folder location of the file "iTunes.msi" . Any clues would be appreciated

    Hi, Need help. I am trying to download the latest iTunes for Windows application but on running the program it says that it can not be completed as it can not find the folder location of the file "iTunes.msi" . Any clues would be appreciated

    (1) Download the Windows Installer CleanUp utility installer file (msicuu2.exe) from the following Major Geeks page (use one of the links under the "DOWNLOAD LOCATIONS" thingy on the Major Geeks page):
    http://majorgeeks.com/download.php?det=4459
    (2) Doubleclick the msicuu2.exe file and follow the prompts to install the Windows Installer CleanUp utility. (If you're on a Windows Vista or Windows 7 system and you get a Code 800A0046 error message when doubleclicking the msicuu2.exe file, try instead right-clicking on the msicuu2.exe file and selecting "Run as administrator".)
    (3) In your Start menu click All Programs and then click Windows Install Clean Up. The Windows Installer CleanUp utility window appears, listing software that is currently installed on your computer.
    (4) In the list of programs that appears in CleanUp, select any iTunes entries and click "Remove", as per the following screenshot:
    (5) Quit out of CleanUp, restart the PC and try another iTunes install. Does it go through properly this time?

  • I need help with changing my verizon vm to the system voicemail

    I need help with changing my verizon vm to the system voicemail

    Deleting the iTunes account is not the solution as you will very likely lose any and all purchases ever made with the account.
    If you cannot find the option on the site, contact iTunes support.

  • I need help installing Lightroom 5.  I tried the trial and it does not have an uninstall feature.  Thus, I'm trying to activate with my new $9.99 per month subscription and it simply will not work. Very frustrating you already took my money and no results

    I need help installing Lightroom 5.  I tried the trial and it does not have an uninstall feature.  Thus, I'm trying to activate with my new $9.99 per month subscription and it simply will not work. Very frustrating you already took my money and no results, and very difficult to get help.

    Lightroom Trial uninstall wrote:
    Very frustrating you already took my money and no results, and very difficult to get help.
    Just for clarity, "we" haven't taken your money - this is a user-to-user forum, and you're not talking to Adobe.
    Like Rob I'm a Windows user, and - like him - I thought "uninstalling" on Macs was simply a case of trashing the application. Google would seem to concur.
    Not really a Lightroom/Adobe issue, then?

  • All of my photos are displayed as BW because somehow I've saved a quick develop preset and it saves as BW.  This is for all of my photos.  I can individually undo them but need help in how to get rid of the preset so it goes to Default. thanks

    All of my photos are displayed as BW because somehow I've saved a quick develop preset and it saves as BW.  This is for all of my photos.  I can individually undo them but need help in how to get rid of the preset so all photos goes to Default or as shot. thanks

    Go to the develop module and highlight all of the images in the filmstrip at the bottom of the screen. Then activate Auto-sync and click on the Reset button. That should reset all of the images to your camera default settings.

  • I need a help to write the query

    Id
    Child
    Parent
    Is Sub Parent
    Is Parent
    1
    chld1
    y
    2
    chld2
    chld1
    y
    3
    chld3
    chld1
    y
    4
    chld4
    chld1
    5
    chld5
    chld2
    y
    6
    chld6
    chld2
    7
    chld7
    chld5
    8
    chld8
    Q: please check the above table, id no 1-7 are linked with parent and id no 8 is independent child, i need to write a query to display the below result based on the below scenarios
    Scenario 1: if user passes the child 1 the result should be as showed below table all 7 rows need to show.
    Scenario 2: if user passes the child in between child 1 - child 7 then also the result should be as showed below table all 7 rows.
    RESULT
    Id
    Child
    Parent
    1
    chld1
    2
    chld2
    chld1
    3
    chld3
    chld1
    4
    chld4
    chld1
    5
    chld5
    chld2
    6
    chld6
    chld2
    7
    chld7
    chld5
    Thanks in advance

    Hi,
    next time please post sample data as CREATE TABLE and INSERT statements or with clause like this:
    with mychildren as
       select 1 id, 'chld1' chld, null    prnt, null is_sub_parent, 'Y'  is_parent from dual union all 
       select 2 id, 'chld2' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 3 id, 'chld3' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 4 id, 'chld4' chld, 'chld1' prnt, null is_sub_parent, null is_parent from dual union all 
       select 5 id, 'chld5' chld, 'chld2' prnt,'Y'   is_sub_parent, null is_parent from dual union all 
       select 6 id, 'chld6' chld, 'chld2' prnt, null is_sub_parent, null is_parent from dual union all 
       select 7 id, 'chld7' chld, 'chld5' prnt, null is_sub_parent, null is_parent from dual union all 
       select 8 id, 'chld8' chld, null    prnt, null is_sub_parent, null is_parent from dual
    Here a couple of test with chld7 and chld1
    with mychildren as
       select 1 id, 'chld1' chld, null    prnt, null is_sub_parent, 'Y'  is_parent from dual union all 
       select 2 id, 'chld2' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 3 id, 'chld3' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 4 id, 'chld4' chld, 'chld1' prnt, null is_sub_parent, null is_parent from dual union all 
       select 5 id, 'chld5' chld, 'chld2' prnt,'Y'   is_sub_parent, null is_parent from dual union all 
       select 6 id, 'chld6' chld, 'chld2' prnt, null is_sub_parent, null is_parent from dual union all 
       select 7 id, 'chld7' chld, 'chld5' prnt, null is_sub_parent, null is_parent from dual union all 
       select 8 id, 'chld8' chld, null    prnt, null is_sub_parent, null is_parent from dual
    , find_root as
        select chld
          from mychildren
         where prnt is null
         start with chld='chld1'  --- this is your input
       connect by prior prnt=chld
    select id, chld, prnt
       from mychildren     
      start with chld=(select chld from find_root)
    connect by prior chld=prnt
            ID CHLD  PRNT
             1 chld1     
             2 chld2 chld1
             5 chld5 chld2
             7 chld7 chld5
             6 chld6 chld2
             3 chld3 chld1
             4 chld4 chld1
    Changing it to chld7 does not change the output.
    Output is now sorted according hierarchical query. If you need a different sort order you can specify what you want.
    Regards.
    Al

  • Need Help in tuning of the query

    Hi all,
    Can any body help me how we can rewrite this query in
    optimized way.
    SELECT A.C1 AS COLUMN_1, B.C2 AS COLUMN_2
    FROM A,B
    WHERE A.C1 IN (
    SELECT COLUMN_1 FROM(
    (SELECT A.C1 AS COLUMN_1,
    COUNT(C1) AS COUNT,
    RANK() OVER (ORDER BY COUNT(C1) DESC) AS RANK
    FROM A, B, C
    WHERE A.C1 = B.C3
    AND B.C2 = C.C1
    AND <other conditions>
    GROUP BY A.C1)
    WHERE RANK <= 10)
    AND A.C1 = B.C3
    AND <other conditions>
    ORDER BY A.C1
    I am using <other conditions> are same in outer query and inner query also.
    Thanks in Advance,

    Hi,
    Yes exactly I am looking TOP-10 results the query is like this
    SELECT A.C1 AS COLUMN_1, B.C2 AS COLUMN_2
    FROM A,B
    WHERE B.C2 IN (
    SELECT COLUMN_1 FROM(
    (SELECT B.C2 AS COLUMN_1,
    COUNT(A.C1) AS COUNT,
    RANK() OVER (ORDER BY COUNT(C1) DESC) AS RANK
    FROM A, B, C
    WHERE A.C1 = B.C3
    AND B.C2 = C.C1
    AND <other conditions>
    GROUP BY B.C2)
    WHERE RANK <= 10)
    AND A.C1 = B.C3
    AND <other conditions>
    ORDER BY B.C2
    Thanks

Maybe you are looking for

  • F4v encoded with Adobe media encoder CS4 (longer than 71 minutes)

    Hi I don't know if this is the correct place for this post. So apologies me if this is the wrong place. Encoding with Adobe Media Encoder CS4 (4.2.0.006 and 4.0 too) Source: MOV (DVPAL) longer than 71 minutes Dest: F4V (H.264) Result: freezed frames

  • How do I quickly change writing direction in mail?

    How do I quickly change writing direction in mail without going trough 7 different menu options? Is there a quick keyboard shortcut, or maybe an icon I can add to the mail bar? Thank you. Dan

  • Mouse pad Freezes

    I bought the HP probook G1 450 less than a year ago, and i have a problem with the mouse pad , as it sometimes freeze (only moving really slowly but in one line , doesn't go up or down). i read online some ppl have had the same issue with windows 8.1

  • G5 PowerMac posts, but doesn't get past splash screen, fans rev to high

    After installing the recent quicktime and iTunes update my PowerMac hasn't wanted to boot. It posts (chimes) and gets to the white screen with the apple and the rotating spike pinwheel. The pinwheel rotates. After about a minute on this page the fans

  • Can't add field in data source 0CRM_OPPT_I

    Hi,   I have earlier added several fields to this data source but this time I have a peculiar issue, This is what I did 1> RSA6--> DISPLAY THE DATA SOURCE double click on the extract structure> go to append structure --> add new field ZZ... and activ