How to improve this query speed ?....help me

How to improve the query speed. Any hints can u suggest in the query or any correction. Here i am using sample tables for checking purpose, When i am trying with my original values, this type of query taking longer time to run
select ename,sal,comm from emp where(comm is null and &status='ok') or (comm is not null and &status='failed');
Thanx in advance
prasanth a.s.

What about
select ename,sal,comm from emp where comm is null and &status='ok'
union all
select ename,sal,comm from emp where comm is not null and &status='failed';
Regards
Vaishnavi

Similar Messages

  • How to improve this query?

    Hi Friends,
    The table ITEM_COST_STG_TBL contains the following details of the items.
    CREATE TABLE ITEM_COST_STG_TBL(item_org,item_name,item_cost)
    AS
    SELECT 1234 item_org,'I1' item_name,5 item_cost FROM DUAL UNION ALL
    SELECT 1235 item_org,'I1' item_name,5 item_cost FROM DUAL UNION ALL
    SELECT 1236 item_org,'I1' item_name,10 item_cost FROM DUAL UNION ALL
    SELECT 1234 item_org,'I2' item_name,5 item_cost FROM DUAL UNION ALL
    SELECT 1235 item_org,'I2' item_name,5 item_cost FROM DUAL UNION ALL
    SELECT 1236 item_org,'I2' item_name,10 item_cost FROM DUAL
    ) As you can see from the data above,an item may belong to more than one organization.
    For each item, i want to pick all the records of that item whose cost is greater than the minimum cost of that item.
    For this i have wrriten the following query
    SELECT *
    FROM
    SELECT item_org
              ,item_name
              ,item_cost
              ,MIN(item_cost) OVER (PARTITION BY item_name ORDER BY item_cost) MIN_COST
    FROM ITEM_COST_STG_TBL
    WHERE item_cost > min_costIn the above query,the analytic version of the MIN function executes for all the records of the same item even though the MIN value is same.
    Is there a way such that MIN() value is executed once for an item and then comparing the item price with the MIN price?
    The reason why i am looking for this,currently i am executing this query on a table which has very few records.But in the real time the table might be containing 11-12 lakhs of records.

    What about this:
    SELECT     ITEM_ORG
    ,     ITEM_NAME
    ,     ITEM_COST
    FROM     ITEM_COST_STG_TBL ICS
    WHERE      ITEM_COST >      (
                        SELECT      MIN(ITEM_COST)
                        FROM      ITEM_COST_STG_TBL ICSI
                        WHERE      ICSI.ITEM_NAME = ICS.ITEM_NAME
                        GROUP BY ITEM_NAME
                   )HTH!
    *:EDIT:*
    Your query might be the most efficient, based on the current explain plans:
    Explain plan your query:
    | Id  | Operation           | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT    |                   |     6 |   258 |     3  (34)| 00:00:01 |
    |*  1 |  VIEW               |                   |     6 |   258 |     3  (34)| 00:00:01 |
    |   2 |   WINDOW SORT       |                   |     6 |   180 |     3  (34)| 00:00:01 |
    |   3 |    TABLE ACCESS FULL| ITEM_COST_STG_TBL |     6 |   180 |     2   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - filter("ITEM_COST">"MIN_COST")Explain plan my query:
    | Id  | Operation             | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT      |                   |     1 |    30 |     4   (0)| 00:00:01 |
    |*  1 |  FILTER               |                   |       |       |            |          |
    |   2 |   TABLE ACCESS FULL   | ITEM_COST_STG_TBL |     6 |   180 |     2   (0)| 00:00:01 |
    |   3 |   SORT GROUP BY NOSORT|                   |     1 |    17 |     2   (0)| 00:00:01 |
    |*  4 |    TABLE ACCESS FULL  | ITEM_COST_STG_TBL |     1 |    17 |     2   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - filter("ITEM_COST"> (SELECT /*+ */ MIN("ITEM_COST") FROM
                  "ITEM_COST_STG_TBL" "ICSI" WHERE "ICSI"."ITEM_NAME"=:B1 GROUP BY "ITEM_NAME"))
       4 - filter("ICSI"."ITEM_NAME"=:B1)Edited by: Centinul on Jun 26, 2009 7:34 AM

  • How can i improve this query.

    Hi guys i am beginner , just wanted to know some info , how can i improve this query ..
    select *
    from tableA A, viewB B,
    where A.key = B.key
    and a.criteria1 = '111'
    and a.criteria2 = some_funtion(a.key)
    one more thing should function should be on left side of equal sign.
    will a join make it better or something else is needed more than that .

    952936 wrote:
    Hi guys i am beginner , just wanted to know some info , how can i improve this query ..
    select *
    from tableA A, viewB B,
    where A.key = B.key
    and a.criteria1 = '111'
    and a.criteria2 = some_funtion(a.key)
    one more thing should function should be on left side of equal sign.
    will a join make it better or something else is needed more than that .If you are a beginner try to learn the ANSI Syntax. This will help you a lot to write better queries.
    Your select would look like this in ANSI.
    select *
    from tableA A
    JOIN viewB B ON A.key = B.key
    WHERE a.criteria1 = '111'
    and a.criteria2 = some_function(a.key);The good thing here is that this separates the typical joining part of the select from the typical filter criteria.
    The other syntax very often let you forget one join. Just because there are so many tables and so many filters, that you just don't notice correctly anymore what was join and what not.
    If you notice that the number of column is not what you expect, you can easiely modify the query and compare the results.
    example A
    Remove View B from the query (temporarily comment it out).
    select *
    from tableA A
    --JOIN viewB B ON A.key = B.key
    WHERE a.criteria1 = '111'
    and a.criteria2 = some_funtion(a.key)
    example B
    You notice, that values from A are missing. Maybe because there is no matching key in ViewB? Then change the join to an outer join.
    select *
    from tableA A
    LEFT OUTER JOIN viewB B ON A.key = B.key
    WHERE a.criteria1 = '111'
    and a.criteria2 = some_funtion(a.key)(The outer keyword is optional, left join would be enough).

  • How to rewrite this query without sub query please help me

    Hello All Good Evening,
    Could you please help me with this query, how can i write this query without sub query, or how can write this query another ways
    please help me
    select planno, status1, count(*) Counts from
    select a.ValetNO PlanNo  ,
    case 
         when JoinCode in ('00', '01', '02') then 'Actcess'
         when JoinCode in ('20', '21', '22', '23','38', '39') then
         'Secured' else 'Other' end Status1 ---, COUNT (*)
       from  dbo.ppt a(NOLOCK)  left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO  = b.ValetNO
    --group by a.ValetNO
      a group by planno, status1
    order by 2
    Thank you in Advance
    Milan

    Whats your objective here? Sorry, am not able to understand the reason for this change. 
    Try the below:(Not tested)
    ;With cte
    As
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 ---, COUNT (*)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    select planno, status1, count(*) Counts from cte
    a group by planno, status1
    order by 2
    Even below:
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 , COUNT (1)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    Group by a.ValetNO ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end

  • How to tune this query for the improve performance ?

    Hi All,
    How to tune this query for the improve performance ?
    select a.claim_number,a.pay_cd,a.claim_occurrence_number,
    case
    when sum(case
    when a.payment_status_cd ='0'
    then a.payment_est_amt
    else 0
    end
    )=0
    then 0
    else (sum(case
    when a.payment_status_cd='0'and a.payment_est_amt > 0
    then a.payment_est_amt
    else 0
    end)
    - sum(case
    when a.payment_status_cd<>'0'
    then a.payment_amt
    else 0
    end))
    end as estimate
    from ins_claim_payment a
    where a.as_of_date between '31-jan-03' and '30-aug-06'
    and ( a.data_source = '25' or (a.data_source between '27' and '29'))
    and substr(a.pay_cd,1,1) IN ('2','3','4','8','9')
    group by a.claim_number, a.pay_cd, a.claim_occurrence_number
    Thank you,
    Mcka

    Mcka
    As well as EXPLAIN PLAN, let us know what proportion of rows are visited by this query. It may be that it is not using a full table scan when it should (or vice versa).
    And of course we'd need to know what indexes are available, and how selective they are for the predicated you have in this query ...
    Regards Nigel

  • How do i improve this query

    hi all... hre is the following qurey .. that retrieves close to 310 records.. how to improve this further? i definitely need distinct clause here if i remove i willget duplicates
    query
    SELECT DISTINCT usr_id, pvlg_dim_nb
                          FROM hds01.usr_xref ux, hds01.hds_fct hf
                         WHERE ux.sys_id = 'ACCESS'
                           AND ux.usr_dim_nb = hf.usr_dim_nb
                           AND hf.pvlg_dim_nb <> 0
                           AND hf.prod_dim_nb <> 0
                           AND hf.cust_dim_nb <> 0
                           AND hf.sbj_gp_dim_nb = 0
                           AND hf.obj_gp_dim_nb = 0
                          -- AND hf.rsrc_dim_nb = 0
                           AND hf.obj_cstr_dim_nb = 0
                           AND hf.role_dim_nb = 0
                           AND hf.srv_dim_nb = 0
                          and ux.usr_dim_nb=2598
                                plan
    | Id  | Operation                      | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT               |              |     1 |    51 |   612   (1)| 00:00:08 |
    |   1 |  HASH UNIQUE                   |              |     1 |    51 |   612   (1)| 00:00:08 |
    |   2 |   MERGE JOIN CARTESIAN         |              |  9738 |   484K|   611   (1)| 00:00:08 |
    |*  3 |    TABLE ACCESS BY INDEX ROWID | USR_XREF     |     1 |    25 |     2   (0)| 00:00:01 |
    |*  4 |     INDEX RANGE SCAN           | USR_XREF_FK1 |     2 |       |     1   (0)| 00:00:01 |
    |   5 |    BUFFER SORT                 |              |  9738 |   247K|   609   (1)| 00:00:08 |
    |*  6 |     TABLE ACCESS BY INDEX ROWID| HDS_FCT      |  9738 |   247K|   609   (1)| 00:00:08 |
    |*  7 |      INDEX RANGE SCAN          | HDS_FCT_FK4  | 10253 |       |    23   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       3 - filter("UX"."SYS_ID"='ACCESS')
       4 - access("UX"."USR_DIM_NB"=259)
       6 - filter("HF"."PROD_DIM_NB"<>0 AND "HF"."PVLG_DIM_NB"<>0 AND
                  "HF"."CUST_DIM_NB"<>0 AND "HF"."SBJ_GP_DIM_NB"=0 AND "HF"."OBJ_GP_DIM_NB"=0 AND
                  "HF"."OBJ_CSTR_DIM_NB"=0 AND "HF"."ROLE_DIM_NB"=0 AND "HF"."SRV_DIM_NB"=0)
       7 - access("HF"."USR_DIM_NB"=259)

    Can you provide some more information regarding:
    -Database version?
    -Data selectivity?
    -Indexes?
    -Table statistics?

  • How to improve the response speed

    Dear Consultants,
    In BI 7.0 EP Environment,  displaying 40000 detailed records wasted  3 minutes, this speed is slowly.
    How to improve the response speed ?
    Please give me some proposals and methods to solve this question.
    Thanks a lot & Best Regards
    Ricky

    Hello,
    3min. is not so bad for 40000 rows of data. Firstly you can analyze where the time spent is it olap or DB. you can use RSRT for analysis.
    Then,
    1. Create aggregates
    2. Use caching and precalculation
    3. check 'Use Selection of Structure Elements' from properties of query in RSRT
    4. Remove unnecessary calculations from query
    5. remove unnecessary rows. try to make them free characteristics.
    6. If there is unused data in infocube, you can archive this data.
    regards,

  • How to modify this query to get the desired output format

    I hv written a Query to display all the parent table names and their primary key columns(relevant to this foreign key of the child table).The query is given below...
    SELECT DISTINCT(TABLE_NAME) AS PARENT_TABLE,COLUMN_NAME AS PARENT_COLUMN
    FROM ALL_CONS_COLUMNS
    WHERE CONSTRAINT_NAME IN (SELECT AC.R_CONSTRAINT_NAME
    FROM ALL_CONSTRAINTS AC
    WHERE AC.TABLE_NAME=TABLE_NAME
    AND AC.TABLE_NAME='&TABLE'
    AND AC.R_CONSTRAINT_NAME IS NOT NULL);
    This query will display all the parent tables and their primary key columns.Now my problem is that how to modify this query to also display the foreign key column name of the child table.
    I want the query result in the following format.The query should display the following columns.1)child table's name,2)child table's foreign key column name,3)the corresponding parent table's name,4)the parent table's primary key column name(which is the foreign key in the child table).
    For Example I want the output as follows...
    TAKE THE CASE OF SCOTT.EMP(AS INPUT TO YOUR QUERY)
    CHILD_TABLE CHILD_COLUMN PARENT_TABLE PARENT_COLUMN
    EMP DEPTNO DEPT DEPTNO
    In this result I hv used alias name for the columns.The query should display this only for the foreign keys in the child table.In the query which I sent to you earlier will give the parent table and the parent column names,But I also want to append the child table and child column names there.
    any help on how to tackle would be appreciated.

    Try this query
    SELECT c.table_name child_table,
         c.column_name child_column,
         p.table_name parent_table,
         p.column_name parent_column
    FROM user_constraints a,user_constraints b,user_cons_columns c,
         user_cons_columns p
    WHERE a.r_constraint_name=b.constraint_name and
          a.constraint_name=c.constraint_name and
          b.constraint_name=p.constraint_name and
          c.position=p.position
    ORDER BY c.constraint_name,c.position
    Anwar

  • How to solve this query

    hi,
    i have a product table like
    product month1 month2 month3 .................
    soap 1200 1256 1895 ............
    i want use a query where i can select column name with a parameter.
    like
    select month||:num from product;
    in num variable it cud be 1 to 10 of value that is dependent on my program.
    so how to make this query .
    thxs

    Hi,
    Here is an example that i am helpful.
    In the example , I am using a table 'table_name' which contains columns like
    assign_attribute1
    assign_attribute2
    assign_attribute15
    Now I will pass any number from 1 to 15 to the function.
    create or replace procedure pass_col_number(v_number varchar2) as
    v_sql varchar2(2000);
    v_assign_attribute1   varchar2(150);
    begin
    v_sql := 'select  assign_attribute'||v_number||'  from  table_name where person_id = 1345';
    execute immediate v_sql into v_assign_attribute1;
    dbms_output.put_line('v_assign_attribute1='||v_assign_attribute1);
    end;Edited by: Sreekanth Munagala on Dec 18, 2008 1:18 AM

  • How to combine this query so that i can display the ouput together

    I have no idea how to combine this query together.Someone please help.I want the ouput to display oni 1 result combining all together.
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('6910','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('6912','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('7344','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('8344','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    Edited by: 965547 on Nov 5, 2012 12:55 AM

    The only difference which i am seeing in your queries is the Operation ('6910', '6912','7344','8344')
    Then why don't you put all values in One like this
    Select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in *('6910', '6912','7344','8344','7976')* AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    Hope this will resolve your problem.
    Oracle-911

  • How to improve the query performance in to report level and designer level

    How to improve the query performance in to report level and designer level......?
    Plz let me know the detail view......

    first its all based on the design of the database, universe and the report.
    at the universe Level, you have to check your Contexts very well to get the optimal performance of the universe and also your joins, keep your joins with key fields, will give you the best performance.
    at the report level, try to make the reports dynamic as much as you can, (Parameters) and so on.
    and when you create a paremeter try to get it match with the key fields in the database.
    good luck
    Amr

  • I can't forcequit firefox and shutdown my computer, nor can I open up any other programs or applications. Does anyone know how to fix this? please help this poor soul.

    I can't forcequit firefox and shutdown my computer, nor can I open up any other programs or applications. Does anyone know how to fix this? please help this poor soul.

    You can force quit applications
    >Force quit
    if that does not work you can force quit a computer shut down by hold the power button for an extended period.

  • After upgrading my iPhone 5 to iOS 7 iTunes will not stay open once the app launches...does anyone know how to fix this? Please Help :)

    After upgrading my iPhone 5 to iOS 7 iTunes will not stay open once the app launches...does anyone know how to fix this? Please Help :)

    I have exactly the same problem. I too have tried everything that's been suggested, but still not working. Don't really what to do next? I have an iPhone 4S.

  • Hi my iphone 4 is disabled how can solve this problem? help me please

    hi my iphone 4 is disabled how can solve this problem? help me please

    What do you mean by "disabled"? Disabled by entering the wrong passcode? If so, you'll have to force the phone into recovery mode & restore it to get it working again & remove the passcode:
    Turn your phone off, then force it into recovery mode & restore it:
    Leave the USB cable connected to your computer, but NOT your phone, iTunes running, press & hold the home button while connecting the USB cable to your dock connector, continue holding the home button until you see “Connect to iTunes” on the screen. You may now release the home button. iTunes should now display that it has detected your phone in recovery mode, if not quit and reopen iTunes. If you still don’t see the recovery message repeat these steps again. iTunes will give you the option to restore from a backup or set up as new.

  • How to use this query in R12

    the query below , i am using in valueset :
    select * from AR_LOCATION_VALUES
    Where ar_location_values.location_segment_qualifier =
    'COUNTRY' and ar_location_values.location_structure_id in ( select location_structure_id from ar_system_parameters )
    how to use this query to set in R12?
    Thanks

    hi
    i am using the following query in 11i and i want to use the same in R12 :
    SELECT ar_location_values.location_segment_description,ar_location_values.location_segment_value,location_segment_id
    FROM AR_LOCATION_VALUES
    WHERE ar_location_values.location_segment_qualifier = 'COUNTRY'
    AND ar_location_values.location_structure_id IN
    (SELECT location_structure_id FROM ar_system_parameters)
    note: the table ar_location_values is obsolette in R12 so what is the replacement table in R12 for this?

Maybe you are looking for