Can I refactor this query to use an index more efficiently?

I have a members table with fields such as id, last name, first name, address, join date, etc.
I have a unique index defined on (last_name, join_date, id).
This query will use the index for a range scan, no sort required since the index will be in order for that range ('Smith'):
SELECT members.*
        FROM members
        WHERE last_name = 'Smith'
        ORDER BY joindate, idIs there any way I can get something like the following to use the index (with no sort) as well:
SELECT members.*
        FROM members
        WHERE last_name like 'S%'
        ORDER BY joindate, idI understand the difficulty is probably; even if it does a range scan on every last name 'S%' (assuming it can?), they're not necessarily in order. Case in point:
Last_Name:  JoinDate:
Smith          2/5/2010
Smuckers     1/10/2010An index range scan of 'S%' would return them in the above order, which is not ordered by joindate.
So is there any way I can refactor this (query or index) such that the index can be range scanned (using LIKE 'x%') and return rows in the correct order without performing a sort? Or is that simply not possible?

xaeryan wrote:
I have a members table with fields such as id, last name, first name, address, join date, etc.
I have a unique index defined on (last_name, join_date, id).
This query will use the index for a range scan, no sort required since the index will be in order for that range ('Smith'):
SELECT members.*
FROM members
WHERE last_name = 'Smith'
ORDER BY joindate, idIs there any way I can get something like the following to use the index (with no sort) as well:
SELECT members.*
FROM members
WHERE last_name like 'S%'
ORDER BY joindate, idI understand the difficulty is probably; even if it does a range scan on every last name 'S%' (assuming it can?), they're not necessarily in order. Case in point:
Last_Name:  JoinDate:
Smith          2/5/2010
Smuckers     1/10/2010An index range scan of 'S%' would return them in the above order, which is not ordered by joindate.
So is there any way I can refactor this (query or index) such that the index can be range scanned (using LIKE 'x%') and return rows in the correct order without performing a sort? Or is that simply not possible?Come on. Index column order does matter. "LIKE 'x%'" actually is full table scan. The db engine accesses contiguous index entries and then uses the ROWID values in the index to retrieve the table rows.

Similar Messages

  • Why is this query not using the index?

    check out this query:-
    SELECT CUST_PO_NUMBER, HEADER_ID, ORDER_TYPE, PO_DATE
    FROM TABLE1
    WHERE STATUS = 'N'
    and here's the explain plan:-
    1     
    2     -------------------------------------------------------------------------------------
    3     | Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
    4     -------------------------------------------------------------------------------------
    5     | 0 | SELECT STATEMENT | | 2735K| 140M| 81036 (2)|
    6     |* 1 | TABLE ACCESS FULL| TABLE1 | 2735K| 140M| 81036 (2)|
    7     -------------------------------------------------------------------------------------
    8     
    9     Predicate Information (identified by operation id):
    10     ---------------------------------------------------
    11     
    12     1 - filter("STATUS"='N')
    There is already an index on this column, as is shown below:-
         INDEX_NAME INDEX_TYPE     UNIQUENESS     TABLE_NAME     COLUMN_NAME     COLUMN_POSITION
    1     TABLE1_IDX2 NORMAL     NONUNIQUE     TABLE1      STATUS     1
    2     TABLE1_IDX NORMAL     NONUNIQUE     TABLE1     HEADER_ID     1
    So why is this query not using the index on the 'STATUS' Column?
    I've already tried using optimizer hints and regathering the stats on the table, but the execution plan still remains the same, i.e. it still uses a FTS.
    I have tried this command also:-
    exec dbms_stats.gather_table_stats('GECS','GEPS_CS_SALES_ORDER_HEADER',method_opt=>'for all indexed columns size auto',cascade=>true,degree=>4);
    inspite of this, the query is still using a full table scan.
    The table has around 55 Lakh records, across 60 columns. And because of the FTS, the query is taking a long time to execute. How do i make it use the index?
    Please help.
    Edited by: user10047779 on Mar 16, 2010 6:55 AM

    If the cardinality is really as skewed as that, you may want to look at putting a histogram on the column (sounds like it would be in order, and that you don't have one).
    create table skewed_a_lot
    as
       select
          case when mod(level, 1000) = 0 then 'N' else 'Y' end as Flag,
          level as col1
       from dual connect by level <= 1000000;
    create index skewed_a_lot_i01 on skewed_a_lot (flag);
    exec dbms_stats.gather_table_stats(user, 'SKEWED_A_LOT', cascade => true, method_opt => 'for all indexed columns size auto');Is an example.

  • Why is this query not using my index ?

    hey
    i have the following situation
    (i'm sorry but i can't add the acctual script or output)
    create table x as
    select      level high_card ,
         mod(level,1000) medium_card ,
         mod(level,10) low_card ,
         '***' padding
    from     dual
    connect by level < 100001;
    create index x_med_low_ix on x(low_card,medium_card);
    create index x_high_ix on x(high_card);
    exec dbms_stats.gather_table_stats(user,'x',cascade=>true,method_opt=>'for all indexed columns size auto');
    i'm running the following query
    select      *
    from      x
    where      low_card = 70
    and     medium_card = 70
    and     high_card = 70;
    i'm expecting a range scan on the x_high_ix index. the optimizer expects only one row. the stats on the high_card columns shows that (num_rows * density = 1).
    when i run the query the optimizer uses the x_med_low_ix and does 14 cr.
    when i force the use of x_high_ix the cr goes down to 4.
    i don't want to declare x_high_ix as unique.
    so, why isn't he using my index ?

    It's using that index for me:
    SQL> create table x as
      2  select level high_card ,
      3  mod(level,1000) medium_card ,
      4  mod(level,10) low_card ,
      5  '***' padding
      6  from dual
      7  connect by level < 100001;
    Table created.
    SQL>
    SQL> create index x_med_low_ix on x(low_card,medium_card);
    Index created.
    SQL>
    SQL> create index x_high_ix on x(high_card);
    Index created.
    SQL>
    SQL> exec dbms_stats.gather_table_stats(user,'x',cascade=>true,method_opt=>'for all indexed columns
    size auto');
    PL/SQL procedure successfully completed.
    SQL> ed
    Wrote file afiedt.buf
      1   select *
      2   from x
      3   where low_card = 70
      4   and medium_card = 70
      5*  and high_card = 70
    SQL>
    SQL> /
    no rows selected
    Execution Plan
    Plan hash value: 775193209
    | Id  | Operation                   | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT            |           |     1 |    15 |     2   (0)| 00:00:01 |
    |*  1 |  TABLE ACCESS BY INDEX ROWID| X         |     1 |    15 |     2   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN          | X_HIGH_IX |     1 |       |     1   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - filter("LOW_CARD"=70 AND "MEDIUM_CARD"=70)
       2 - access("HIGH_CARD"=70)
    Statistics
              0  recursive calls
              0  db block gets
              3  consistent gets
              0  physical reads
              0  redo size
            454  bytes sent via SQL*Net to client
            370  bytes received via SQL*Net from client
              1  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              0  rows processed
    SQL> set autot off
    SQL> select * from v$version;
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Productionwhat is your version?
    By the way, when i force optimizer to use index X_MED_LOW_IX, it does 2 CR for me.

  • How can i construct this query without using CASE statement?

    I've a following code. I'm using this script in Hibernet. So, i cannot use CASE there. Because, hibernet doesn't support case in select statement. How can i construct the same thing which will give me the same result without using CASE?
    SELECT ofc.FLT_LCL_ORIG_DT
         , ofc.CARR_IATA_CD
         , ofc.FLT_NBR
         , ofc.ORIG_ARPT_CD
         , ofc.DEST_ARPT_CD
         , sum( ofc.CNCT_PSGR_CNT) AS BOOKED_CNCT_PSGR_CNT
         , sum( CASE WHEN o.fsdr_mrkt_cd = 'D' AND d.fsdr_mrkt_cd = 'D'           THEN '0'
            ELSE to_char(ofc.CNCT_PSGR_CNT,'99')                             END ) AS BOOKED_INTL_CNCT_PSGR_CNT
         , sum(CASE WHEN o.fsdr_mrkt_cd||d.fsdr_mrkt_cd = 'DD'
                    THEN '0'
            ELSE to_char(ofc.CNCT_PSGR_CNT,'99')
            END) AS NEW_BCNT
    FROM OPS_FLT_CNCT ofc
                        , STN o
                        , STN d
                    WHERE ofc.CNCT_ORIG_ARPT_CD = o.STN_CD
                      AND ofc.CNCT_DEST_ARPT_CD = d.STN_CD
                     -- AND TRUNC(ofc.FLT_LCL_ORIG_DT) = trunc(to_date('22-MAY-2007','DD-MON-YYYY'))
                      AND ofc.CARR_IATA_CD = 'UA'
                      AND ofc.FLT_NBR = '1218'
                      AND ofc.ORIG_ARPT_CD = upper('DEN')                AND ofc.DEST_ARPT_CD = upper('IAD')                  GROUP BY ofc.FLT_LCL_ORIG_DT
                           , ofc.CARR_IATA_CD
                           , ofc.FLT_NBR
                           , ofc.ORIG_ARPT_CD
                           , ofc.DEST_ARPT_CD ;And, the output look like this --
    FLT_LCL_O CARR FLT_N ORI DES BOOKED_CNCT_PSGR_CNT BOOKED_INTL_CNCT_PSGR_CNT   NEW_BCNT
    22-MAY-07 UA   1218  DEN IAD                    9                         0          0
    23-MAY-07 UA   1218  DEN IAD                    1                         0          0
    24-MAY-07 UA   1218  DEN IAD                    2                         1          1
    25-MAY-07 UA   1218  DEN IAD                    1                         0          0Thnaks in advance for reply.
    Regards.
    Satyaki De.
    #####

    2 ideas:
    1. Inline function to perform the CASE funcionaltity for you
    2. Piplelined function to generate the entire dataset
    Both will be slower than just using CASE in a query, but we're working around big constraints

  • Can anyone explain this query.?

    Can anyone explain this query.?
    select nvl(s.p_id,q.p_id),nvl(s.p_type,q.p_type),nvl(s.p_line,q.p_line),
    nvl(s.sales2004,0),nvl(s.sales2005,0),nvl(q.quota2004,0),nvl(q.quota2005,0)
    from sales s
    full outer join quota q on(s.p_id = q.p_id and s.p_type=q.p_type and s.p_line=q.p_line)

    from sales s full outer join quota qTable quota is outer joined to table sales , if there are no matching records in table quota then also the query retuns the sales record with null values for the corresponding quota record columns
    NVL() has been used to handle such cases

  • How can i know if my query is using the index ?

    Hello...
    How can i know if my query is using the index of the table or not?
    im using set autotrace on...but is there another way to do it?
    thanks!
    Alessandro Falanque.

    Hi,
    You can use Explain Plan for checking that your query is using proper index or not. First you need to check that Plan_table is installed in your database or not. If it is not there THEN THE SCRIPT WILL BE LIKE THIS:
    CREATE TABLE PLAN_TABLE (
    STATEMENT_ID VARCHAR2 (30),
    TIMESTAMP DATE,
    REMARKS VARCHAR2 (80),
    OPERATION VARCHAR2 (30),
    OPTIONS VARCHAR2 (30),
    OBJECT_NODE VARCHAR2 (128),
    OBJECT_OWNER VARCHAR2 (30),
    OBJECT_NAME VARCHAR2 (30),
    OBJECT_INSTANCE NUMBER,
    OBJECT_TYPE VARCHAR2 (30),
    OPTIMIZER VARCHAR2 (255),
    SEARCH_COLUMNS NUMBER,
    ID NUMBER,
    PARENT_ID NUMBER,
    POSITION NUMBER,
    COST NUMBER,
    CARDINALITY NUMBER,
    BYTES NUMBER,
    OTHER_TAG VARCHAR2 (255),
    PARTITION_START VARCHAR2 (255),
    PARTITION_STOP VARCHAR2 (255),
    PARTITION_ID NUMBER,
    OTHER LONG,
    DISTRIBUTION VARCHAR2 (30))
    TABLESPACE SYSTEM NOLOGGING
    PCTFREE 10
    PCTUSED 40
    INITRANS 1
    MAXTRANS 255
    STORAGE (
    INITIAL 10240
    NEXT 10240
    PCTINCREASE 50
    MINEXTENTS 1
    MAXEXTENTS 121
    FREELISTS 1 FREELIST GROUPS 1 )
    NOCACHE;
    After that write the following command in the SQL prompt.
    Explain plan for (Select statement);
    Select level, SubStr( lpad(' ',2*(Level-1)) || operation || ' ' ||
    object_name || ' ' || options || ' ' ||
    decode(id, null , ' ', decode(position, null,' ', 'Cost = ' || position) ),1,100)
    || ' ' || nvl(other_tag, ' ') Operation
    from PLAN_TABLE
    start with id = 0
    connect by
    prior id = parent_id;
    This will show how the query is getting executed . What are all the indexes it is using etc.
    Cheers.
    Samujjwal Basu

  • 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).

  • I deleted the font section but everything in Photoshop CS5 works.  How can I fixed this because I use fonts all the time? I don't want to purchase another one since this cost me $699:(

    I purchased PhotoshopCS5 Sept 2010 and still use the same MAC. I invertly deleted the font section but everything else works.  How can I fixed this because I use fonts all the time? I don't want to purchase another one since this cost me $699:(

    Which version of mac os x are you using?
    Could you clarify what you mean by "I invertly deleted the font section"

  • HT201441 what if the previous owner doesn't know the email and password for its account and is not present, i bought this ipod from gamestop but it shows this screen, can i dlete this account to use my ipod ?

    help
    what if the previous owner doesn't know the email and password for its account and is not present, i bought this ipod from gamestop but it shows this screen, can i dlete this account to use my ipod ?

    The device is useless. Send it back and have them return your money.

  • Can anyone tell me how can i optimize this query...

    Can anyone tell me how can i optimize this query ??? :
    Select Distinct eopersona.numident From rscompeten , rscompet , rscv , eopersona , rscurso , rseduca , rsexplab , rsinteres
    Where ( ( (LOWER (rscompeten.nombre LIKE '%caracas%') AND ( rscompeten.id = rscompet.idcompeten ) AND ( rscv.id = rscompet.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscurso.nombre) LIKE '%caracas%') AND ( rscv.id = rscurso.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscurso.lugar) LIKE '%caracas%') AND ( rscv.id = rscurso.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rseduca.univinst) LIKE '%caracas%)' AND ( rscv.id = rseduca.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rsexplab.nombempre) LIKE '%caracas%' AND ( rscv.id = rsexplab.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rsinteres.descrip) LIKE '%caracas%' AND ( rscv.id = rsinteres.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscv.cargoasp) LIKE '%caracas%' AND ( eopersona.id = rscv.idpersona ) )
    OR ( LOWER (eopersona.ciudad) LIKE '%caracas%' AND ( eopersona.id = rscv.idpersona )
    PLEASE IF YOU FIND SOMETHING WRONG.. PLEASE HELP ME.. this query takes me aproximatelly 10 minutes and the database is really small ( with only 200 records on each table )

    You are querying eight tables, however in any of your OR predicates you're only restricting 3 or 4 of those tables. That means that the remaining 4 or 5 tables are generating cartesian products. (n.b. the cartesian product of 5 tables with 200 rows each results in g 200^5 = 320,000,000,000 rows) Then you casually hide this behind "distinct".
    A simple restatement of your requirements looks like this:
    Select eopersona.numident
      From rscompeten,
           rscompet,
           rscv,
           eopersona
    Where LOWER (rscompeten.nombre) LIKE '%caracas%'
       AND rscompeten.id = rscompet.idcompeten
       AND rscv.id = rscompet.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    Select eopersona.numident
      From rscurso ,
           rscv,
           eopersona
    Where LOWER (rscurso.nombre) LIKE '%caracas%'
       AND rscv.id = rscurso.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    Select eopersona.numident
      From rscurso ,
           rscv,
           eopersona
    Where LOWER (rscurso.lugar) LIKE '%caracas%'
       AND rscv.id = rscurso.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    ...From there you can eliminate redundancies as desired, but I imagine that the above will perform admirably with the data volumes you describe.

  • My ipod nano gen 4 is stolen! He is my first love with ipod. Can I lock this device by use serial number of the device? I don't want any bad guys use or do anything with my love one. Please let me know if have the way to do thanks.

    My ipod nano gen 4 is stolen! He is my first love with ipod. Can I lock this device by use serial number of the device? I don't want any bad guys use or do anything with my love one. Please let me know if have the way to do thanks.

    zenith_nm wrote:
    My ipod nano gen 4 is stolen! He is my first love with ipod. Can I lock this device by use serial number of the device? I don't want any bad guys use or do anything with my love one. Please let me know if have the way to do thanks.
    Nope!
    http://support.apple.com/kb/HT2526 How to report lost or stolen Apple product

  • Firewall stops Firefox from loading any websites... had to switch off security to send this message... How can I fix this? Am using Symantec Client Firewall

    Firewall stops Firefox from loading any websites... had to switch off security to send this message... How can I fix this? Am using Symantec Client Firewall

    This forum is only for discussions on the forums themselves. You should post your question in the apropriate product forum,
    http://forums.adobe.com/community/shockwave
    if your ptoblem is with Shockwave, or
    http://forums.adobe.com/community/flashplayer
    if it affects Flash Player.

  • Query with using single index

    I have what seems like a pretty straight forward problem. I have a table, say TAB_A that has a NAME, PHONE, SSN along with some other values (no primary key...I know, don't start with me. That's just the way it is.) I want to retrieve records where the NAME or the PHONE, or the SSN match a static value. I realize I could write this as:
    WHERE name = 'Joe' OR phone = '5556667777' or ssn = '012345678'
    However, when you do this (assuming there is a concatenated index on name, phone, ssn), you end up doing an index full scan. If you create three individual indexes on each column then you can write a union query that utilizes the index for each value...eg.
    select...
    where name = 'Joe'
    union
    select...
    where phone = '555667777'
    union
    etc.
    However, here you are writing three separate queries that each do an index range scan. I'm just wondering if there is some way to do this...say with some sort of function based index and a hashing algorithm applied to the data where I can do this in a single index range scan? Any ideas?

    I don't see how a single index entry can point to a row that doesn't exist (in all likelihood).
    Without testing it, I don't see how even a concatenated index could help.
    The rows returned by your query are completely unrelated (unless you find the one person who has all three).
    By the way, 7 posts in 10 years. Now that's what I call a lurker.

  • Can we make this query shorter....?

    hi there
    Can any body help me in reducing the number of lines in this query ! I am using union to concat 3 queries. Select fields in every query are same and in where condition also most of the joins are same in every query. but in where clause only two conditions are different. Can we make this 3 queries into one becoze entire select statement fields are same and most of the where conditions are same !
    Below is the query:
    SELECT
    au1.bu_id,
    au1.first_nam,
    au1.last_nam,
    c.id,
    c.first_nam_d,
    c.last_nam_d,
    c.birth_dte_d,
    c.ssn_num_d,
    se.id schd_event_id,
    seu.dsc,
    ssc.dsc,
    sea.access_typ,
    sea.ATTENDEE_TYP
    FROM
    CLIENT c,
    APP_USER au1,
    SCHD_STAT_CD ssc,
    schd_event_url seu,
    schd_evnt se,
    SCHD_evnt_stat ses,
    SCHD_EVNT_ATTENDEE sea,
    CLIENT_APPT ca
    WHERE
    c.id = ca.client_id_k AND
    au1.bu_id = se.APP_USER_BU_ID_PRIME_K AND
    ssc.code = ses.SCHD_STAT_CD_CODE_K AND
    seu.code = se.SCHD_EVNT_URL_CODE_K AND
    se.id = ses.SCHD_EVNT_ID_K AND
    se.id = sea.SCHD_EVNT_ID_K(+) AND
    se.id = ca.SCHD_EVNT_ID_K AND
    sea.ATTENDEE_TYP(+) = 'WORKER' AND
    ses.create_ts = (select max(create_ts) FROM SCHD_evnt_stat ses1 WHERE ses1.SCHD_EVNT_ID_K = ses.SCHD_EVNT_ID_K)
    UNION
    SELECT
    au1.bu_id,
    au1.first_nam,
    au1.last_nam,
    c.id,
    c.first_nam_d,
    c.last_nam_d,
    c.birth_dte_d,
    c.ssn_num_d,
    se.id schd_event_id,
    seu.dsc,
    ssc.dsc,
    FROM
    CLIENT c,
    APP_USER au1,
    SCHD_STAT_CD ssc,
    schd_event_url seu,
    schd_evnt se,
    SCHD_evnt_stat ses,
    SCHD_EVNT_ATTENDEE sea,
    CLIENT_APPT ca
    WHERE
    c.id = ca.client_id_k AND
    au1.bu_id = se.APP_USER_BU_ID_PRIME_K AND
    ssc.code = ses.SCHD_STAT_CD_CODE_K AND
    seu.code = se.SCHD_EVNT_URL_CODE_K AND
    se.id = ses.SCHD_EVNT_ID_K AND
    se.id = sea.SCHD_EVNT_ID_K AND
    se.id = ca.SCHD_EVNT_ID_K AND
    sea.ATTENDEE_TYP = 'ROLE' AND
    ses.create_ts = (select max(create_ts) FROM SCHD_evnt_stat ses1 WHERE ses1.SCHD_EVNT_ID_K = ses.SCHD_EVNT_ID_K)
    UNION
    SELECT
    au1.bu_id,
    au1.first_nam,
    au1.last_nam,
    c.id,
    c.first_nam_d,
    c.last_nam_d,
    c.birth_dte_d,
    c.ssn_num_d,
    se.id schd_event_id,
    seu.dsc,
    ssc.dsc,
    sea.access_typ,
    sea.ATTENDEE_TYP
    FROM
    CLIENT c,
    APP_USER au1,
    SCHD_STAT_CD ssc,
    schd_event_url seu,
    schd_evnt se,
    SCHD_evnt_stat ses,
    SCHD_EVNT_ATTENDEE sea,
    CLIENT_APPT ca
    WHERE
    c.id = ca.client_id_k AND
    au1.APP_ROLE_NAM_DESKTOP_K = sea.ATTENDEE_ID_K AND
    ssc.code = ses.SCHD_STAT_CD_CODE_K AND
    seu.code = se.SCHD_EVNT_URL_CODE_K AND
    se.id = ses.SCHD_EVNT_ID_K AND
    se.id = sea.SCHD_EVNT_ID_K AND
    se.id = ca.SCHD_EVNT_ID_K AND
    sea.ATTENDEE_TYP = 'ROLE' AND
    ses.create_ts = (select max(create_ts) FROM SCHD_evnt_stat ses1 WHERE ses1.SCHD_EVNT_ID_K = ses.SCHD_EVNT_ID_K)
    Thanks in advance
    prasanth a.s.

    Hi,
    it is difficult to change this query without the possibility to test it in a worksheet. But try the following query.
    I think it should be the same result as your 3 queries.
    SELECT au1.bu_id,
         au1.first_nam,
         au1.last_nam,
         c.id,
         c.first_nam_d,
         c.last_nam_d,
         c.birth_dte_d,
         c.ssn_num_d,
         se.id schd_event_id,
         seu.dsc,ssc.dsc,
         decode(sea.access_typ,NULL,' ',sea.access_typ) AS sea.access_typ,
         decode(sea.ATTENDEE_TYP,NULL,' ',sea.ATTENDEE_TYP) AS sea.ATTENDEE_TYP
    FROM CLIENT c,
         APP_USER au1,
         SCHD_STAT_CD ssc,
         schd_event_url seu,
         schd_evnt se,
         SCHD_evnt_stat ses,
         SCHD_EVNT_ATTENDEE sea,
         CLIENT_APPT ca
    WHERE c.id = ca.client_id_k
    AND au1.bu_id = se.APP_USER_BU_ID_PRIME_K
    AND ssc.code = ses.SCHD_STAT_CD_CODE_K
    AND seu.code = se.SCHD_EVNT_URL_CODE_K
    AND se.id = ses.SCHD_EVNT_ID_K
    AND se.id = ca.SCHD_EVNT_ID_K
    AND ses.create_ts = (select max(create_ts)
    FROM SCHD_evnt_stat ses1
                             WHERE ses1.SCHD_EVNT_ID_K = ses.SCHD_EVNT_ID_K)
    AND
    (sea.ATTENDEE_TYP(+) = 'WORKER' AND se.id = sea.SCHD_EVNT_ID_K(+)) --clause of the 1st query
    OR
    (sea.ATTENDEE_TYP = 'ROLE' AND se.id = sea.SCHD_EVNT_ID_K)               --clause of the 2nd query
    OR
    (sea.ATTENDEE_TYP = 'ROLE' AND se.id = sea.SCHD_EVNT_ID_K AND au1.APP_ROLE_NAM_DESKTOP_K = sea.ATTENDEE_ID_K) --clause of the 3rd query
    );

  • How can i speedup this query ?

    Hi,
    have a look at this query:
    SELECT DISTINCT element_short_description
               FROM sample_test_report, test_group, test_element_master
              WHERE str_test_group_code = tgr_test_group_code
                AND str_element_code = element_code
                AND tgr_group_description = 'SINTER_CHEMICAL_ANALYSIS'
           ORDER BY element_short_descriptionThing is that total number of rows present in "sample_test_report" tables are in lakh ...around 50 lakh.Other two tables have a few rows. This query is taking around 15 seconds for completion, can this query be made faster ?
    Note that proper indexing have been done already.
    Thanks.

    SELECT DISTINCT element_short_description
    FROM sample_test_report, test_group, test_element_master
    WHERE str_test_group_code = tgr_test_group_code
    AND str_element_code = element_code
    AND tgr_group_description = 'SINTER_CHEMICAL_ANALYSIS'
    ORDER BY element_short_description
    Can you provide us with explain plan?
    I suggest you use table alias every time:
    The alias is specified in the FROM clause after each table name.
    Table aliases make your queries more readable.
    Then gather statistics your tables:
    BEGIN
    DBMS_STATS.GATHER_TABLE_STATS('OWNER','TABLE',estimate_percent=>NULL,method_opt=>'FOR ALL INDEXED COLUMNS SIZE AUTO',DEGREE=>10,CASCADE=>TRUE,granularity=>'ALL');
    END;
    if your db version < 10g
    ANALYZE TABLE employees COMPUTE STATISTICS;
    ANALYZE TABLE employees ESTIMATE STATISTICS;
    If necessary rebuild index, check indexes are use.
    Unusable indexes are made valid by rebuilding them to recalculate the pointers.
    Rebuilding an unusable index re-creates the index in a new location, and then drops the unusable index. This can be done either by using Enterprise Manager or through SQL commands:
    ALTER INDEX HR.emp_empid_pk REBUILD;
    ALTER INDEX HR.emp_empid_pk REBUILD ONLINE;
    ALTER INDEX HR.email REBUILD TABLESPACE USERS;
    Note: Rebuilding an index requires that free space be available for the rebuild. Verify that there is sufficient space before attempting the rebuild. Enterprise Manager checks space requirements automatically.
    At the end, try join table separately and then concatenate them.
    Your query will be work good.
    Look at execution plan, if indexes are not used, use HINTs : There are many Oracle hints available to the developer for use in tuning SQL statements that are embedded in PL/SQL.
    please refer to http://www.dba-oracle.com/t_sql_hints_tuning.htm and http://www.adp-gmbh.ch/ora/sql/hints/index.html
    Good luck

Maybe you are looking for