How to enforce index in oracle query

Hi all
how to enforce index in oracle query
Regards

Use INDEX hint to force Optimizer to use the specfied index.
You really need to investigate why Optimizer doesn't choose the index. Remember, INDEX SCAN are not always GOOD.
Jaffar

Similar Messages

  • How to enforce index

    Hi Guys,
    How to enforce index ?
    Say for example,
    select max(dt) from emp
    emp table has an index called 'id1'.
    I want to enforce this index for the above query. How ?
    Inputs are welcome !

    Your index on ID is worthless for this query.
    You would have to add an index on your DT column. Even then, the index will only be used if you have a NOT NULL constraint on the column (or add "and dt is not null" to your query):
    SQL> explain plan for
      2  select min(dt), max(dt) from emp;
    Explained.
    | Id  | Operation            |  Name       | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT     |             |     1 |     8 |   194 |
    |   1 |  SORT AGGREGATE      |             |     1 |     8 |       |
    |   2 |   TABLE ACCESS FULL  | EMP         | 91000 |   710K|   194 |
    SQL> alter table emp modify (dt not null);
    Table altered.
    SQL> explain plan for
      2  select min(dt), max(dt) from emp;
    Explained.
    | Id  | Operation             |  Name       | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT      |             |     1 |     8 |    39 |
    |   1 |  SORT AGGREGATE       |             |     1 |     8 |       |
    |   2 |   INDEX FAST FULL SCAN| EMP_DT      | 91000 |   710K|    39 |
    ---------------------------------------------------------------------Still, even without an index a full table scan of 91000 rows takes < 1 second even on my laptop. How are you getting 21 seconds?

  • How to re-index a modified query?

    Hi community,
    Due to a change to a BO I had also to adjust a already created query used to support an advanced search pane.
    Now every time I open my OWL, the OWL is empty and a message is raised saying "The index is temporary not available;index=.c016:1fs:jdx_zby003542_iptfrmv1yxsig7".
    Even though the message uses the word "temporary" this problem did not solve itself over time so I ask here:
    Is it possible/how can I force the re-indexing of a query?
    Best regards,
    Ludger

    Hi everyone
    This issue still persists in spite of my second message - i am pretty helpless.
    First I thought a clean solves the issue but it only prevents the "index temporary not available" message as long as no BO is present.
    As soon as a new BO is created, the situation occurs again.
    What can I do?

  • How to mention index in select query?

    Hi All,
       If I am using fields of two diffrent secondary index in a single select query on a table how I can mention that data should be picked according to first index used in the query?
    Regards
    Deepak

    Please search before asking basic questions.
    Thread locked.
    Rob

  • How to use index of Oracle Table in EJB??

    Hello I have a Table Department and it's index on Dpt_Name column.
    I have created ejb - cmp in OC4J for Department Table.
    I am getting all the data but it is not in index on department, Where I have to specify index of table in Oc4j.
    Reply appriciated.
    Rajiv

    Rajive,
    An index is used automatically by the Database for faster retrieval of data. The query optimizer in the database determines whether to use an index and which index to use for a SQL statement and you don't have to specify the index in OC4J.
    If you have an index on deptname, then the index will be used when the SQL has a where clause like "where deptname = 'Personal' " or
    "where deptname like 'P%' ", etc. In case of CMP, if you have a finder method called findByDeptName and then this index will be used.
    For details on deciding on your indexing strategies please look at http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/appdev.920/a96590/adg06idx.htm#11977
    regards
    Debu Panda
    oracle

  • Oracle query export into the Excel file

    Hi,
    How can i automatically export oracle query result into the excel file in oracle?

    SQL and PL/SQL FAQ
    Re: Excel query to Oracle Database
    http://blog.mclaughlinsoftware.com/microsoft-excel/how-to-query-oracle-from-excel-2007/
    Edited by: swapnil kambli on May 28, 2013 11:07 PM

  • Oracle Query to MYSQL query

    Hi,
    How to convert the below oracle query to MYSQL query
    Oracle Query:
    SELECT b.* FROM (SELECT t2.*, MIN(t2.id) over(PARTITION BY t2.rid)ord_num
    FROM (SELECT t1.id, dense_rank() over(ORDER BY nvl(t1.CLI_ID_CLIENT_EXT, 'a'||t1.id)) rid
    FROM CANGEN_MATRICE_BKUP t1) t2) t, (SELECT ROWNUM , b.* FROM CANGEN_MATRICE_BKUP b) b
    WHERE t.id = b.id ORDER BY t.ord_num, b.id
    i dont know howto convert the MYSQL query?
    Please help on this.
    Regards
    Sudhakar P.

    No MySQL haven't analytic functions. But every analytic function can be achieved with simple SQL, just one needs to write more it :)
    So here is a starting point how to emulate analytic functions in MySQL
    http://www.oreillynet.com/pub/a/mysql/2007/03/29/emulating-analytic-aka-ranking-functions-with-mysql.html
    And MySQL haven;t rowids as well, but actually I dont see any sense of it in this particular query.
    Gints Plivna
    http://www.gplivna.eu

  • How to make sql to use index/make to query to perform better

    Hi,
    I have 2 sql query which results the same.
    But both has difference in SQL trace.
    create table test_table
    (u_id number(10),
    u_no number(4),
    s_id number(10),
    s_no number(4),
    o_id number(10),
    o_no number(4),
    constraint pk_test primary key(u_id, u_no));
    insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
    values (2007030301, 1, 1001, 1, 2001, 1);
    insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
    values (2007030302, 1, 1001, 1, 2001, 2);
    insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
    values (2007030303, 1, 1001, 1, 2001, 3);
    insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
    values (2007030304, 1, 1001, 1, 2001, 4);
    insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
    values (2007030305, 1, 1002, 1, 1001, 2);
    insert into test_table(u_id, u_no, s_id, s_no, o_id, o_no)
    values (2007030306, 1, 1002, 1, 1002, 1);
    commit;
    CREATE INDEX idx_test_s_id ON test_table(s_id, s_no);
    set autotrace on
    select s_id, s_no, o_id, o_no
    from test_table
    where s_id <> o_id
    and s_no <> o_no
    union all
    select o_id, o_no, s_id, s_no
    from test_table
    where s_id <> o_id
    and s_no <> o_no;
    Execution Plan
    0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=8 Bytes=416)
    1 0 UNION-ALL
    2 1 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=4 Bytes=208)
    3 1 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=4 Bytes=208)
    Statistics
    223 recursive calls
    2 db block gets
    84 consistent gets
    0 physical reads
    0 redo size
    701 bytes sent via SQL*Net to client
    508 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    5 sorts (memory)
    0 sorts (disk)
    8 rows processed
    -- i didnt understand why the above query is not using the index idx_test_s_id.
    -- But still it is faster
    select s_id, s_no, o_id, o_no
    from test_table
    where (u_id, u_no) in
    (select u_id, u_no from test_table
    minus
    select u_id, u_no from test_table
    where s_id = o_id
    or s_no = o_no)
    union all
    select o_id, o_no, s_id, s_no
    from test_table
    where (u_id, u_no) in
    (select u_id, u_no from test_table
    minus
    select u_id, u_no from test_table
    where s_id = o_id
    or s_no = o_no);
    Execution Plan
    0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=16 Card=2 Bytes=156)
    1 0 UNION-ALL
    2 1 FILTER
    3 2 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=6 Bytes=468)
    4 2 MINUS
    5 4 INDEX (UNIQUE SCAN) OF 'PK_TEST' (INDEX (UNIQUE)) (Cost=1 Card=1 Bytes=26)
    6 4 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_TABLE' (TABLE) (Cost=2 Card=1 Bytes=78)
    7 6 INDEX (UNIQUE SCAN) OF 'PK_TEST' (INDEX (UNIQUE)) (Cost=1 Card=1)
    8 1 FILTER
    9 8 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=6 Bytes=468)
    10 8 MINUS
    11 10 INDEX (UNIQUE SCAN) OF 'PK_TEST' (INDEX (UNIQUE)) (Cost=1 Card=1 Bytes=26)
    12 10 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_TABLE' (TABLE) (Cost=2 Card=1 Bytes=78)
    13 12 INDEX (UNIQUE SCAN) OF 'PK_TEST' (INDEX (UNIQUE)) (Cost=1 Card=1)
    Statistics
    53 recursive calls
    8 db block gets
    187 consistent gets
    0 physical reads
    0 redo size
    701 bytes sent via SQL*Net to client
    508 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    4 sorts (memory)
    0 sorts (disk)
    8 rows processed
    -- The above query is using index PK_TEST. But still it has FULL SCAN to the
    -- table two times it has the more cost.
    1st query --> SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=8 Bytes=416)
    2nd query --> SELECT STATEMENT Optimizer=ALL_ROWS (Cost=16 Card=2 Bytes=156)
    My queries are:
    1) performance wise which query is better?
    2) how do i make the 1st query to use an index
    3) is there any other method to get the same result by using any index
    Appreciate your immediate help.
    Best regards
    Muthu

    Hi William
    Nice...it works.. I have added "o_id" and "o_no" are in part of the index
    and now the query uses the index
    Execution Plan
    0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=2 Card=8 Bytes=416)
    1 0 UNION-ALL
    2 1 INDEX (FULL SCAN) OF 'IDX_TEST_S_ID' (INDEX) (Cost=1 Card=4 Bytes=208)
    3 1 INDEX (FULL SCAN) OF 'IDX_TEST_S_ID' (INDEX) (Cost=1 Card=4 Bytes=208)
    Statistics
    7 recursive calls
    0 db block gets
    21 consistent gets
    0 physical reads
    0 redo size
    701 bytes sent via SQL*Net to client
    507 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    8 rows processed
    But my questions are:
    1) In a where clause, if "<>" condition is used, then, whether the system will use the index. Because I have observed in several situations even though the column in where clause is indexed, since the where condition is "like" or "is null/is not null"
    then the index is not used. Same as like this, i assumed, if we use <> then indexes will not be used. Is it true?
    2) Now, after adding "o_id" and "o_no" columns to the index, the Execution plan is:
    Execution Plan
    0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=2 Card=8 Bytes=416)
    1 0 UNION-ALL
    2 1 INDEX (FULL SCAN) OF 'IDX_TEST_S_ID' (INDEX) (Cost=1 Card=4 Bytes=208)
    3 1 INDEX (FULL SCAN) OF 'IDX_TEST_S_ID' (INDEX) (Cost=1 Card=4 Bytes=208)
    Before it was :
    Execution Plan
    0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6 Card=8 Bytes=416)
    1 0 UNION-ALL
    2 1 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=4 Bytes=208)
    3 1 TABLE ACCESS (FULL) OF 'TEST_TABLE' (TABLE) (Cost=3 Card=4 Bytes=208)
    Difference only in Cost (reduced), not in Card, Bytes.
    Can you explain, how can i decide which makes the performace better (Cost / Card / Bytes). Full Scan / Range Scan?
    On statistics also:
    Before:
    Statistics
    52 recursive calls
    0 db block gets
    43 consistent gets
    0 physical reads
    0 redo size
    701 bytes sent via SQL*Net to client
    507 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    8 rows processed
    After:
    Statistics
    7 recursive calls
    0 db block gets
    21 consistent gets
    0 physical reads
    0 redo size
    701 bytes sent via SQL*Net to client
    507 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    0 sorts (memory)
    0 sorts (disk)
    8 rows processed
    Difference in recursive calls & consistent gets.
    Which one shows the query with better performance?
    Please explain..
    Regards
    Muthu

  • How to find unused indexes in oracle 10g r2

    Hi all,
    db:oracle 10.2.0.3
    os:solaris
    i want rebuilt the some of the indexes (due poor performence of db)
    how to find the unused indexes in oracle 10gr2 database.?
    can any one help me out plz.

    kk001 wrote:
    Hi all,
    db:oracle 10.2.0.3
    os:solaris
    i want rebuilt the some of the indexes (due poor performence of db)
    how to find the unused indexes in oracle 10gr2 database.?
    can any one help me out plz.You can use V$OBJECT_USAGE.
    But how you decide need rebuilding indexes?
    How you decide problem related indexes?
    What is exactly your mean "due poor performence of db"? some queries hang/long running or whole system hang or has poor performance?
    In generally do not need rebuilding index(unless specially cases),first give we above questions`s answers.

  • Weird result of oracle query before and after function base index creation

    Hi All,
    Here is the unique situation we are facing after creating just an index.
    The query result before the index and the query result after the index do not match.
    This is very illogical situation. Shidhar and me have done lot of R&D, also tried to get lots info from Google but we couldn't decipher the reason for that.
    I am giving you all the details about the query, index and tables with following steps.
    Please let us know if anything is going wrong from our side or is it a bug at oracle level which is a rarest possibility but a possibility.
    Step 1 :- Create table
    create table TEMP_COMP
    ID VARCHAR2(10),
    GROUP_ID VARCHAR2(10),
    TRAN_DATE DATE,
    AMT_1 NUMBER,
    AMT_2 NUMBER,
    AMT_3 NUMBER
    Step 2 :- Insert Sample data
    set feedback off
    set define off
    prompt Deleting TEMP_COMP...
    delete from TEMP_COMP;
    commit;
    prompt Loading TEMP_COMP...
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('01', 'G01', to_date('01-03-2007', 'dd-mm-yyyy'), 1, 11, 111);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('02', 'G01', to_date('02-03-2007', 'dd-mm-yyyy'), 2, 22, 222);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('03', 'G01', to_date('03-03-2007', 'dd-mm-yyyy'), 3, 33, 333);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('04', 'G01', to_date('04-03-2007', 'dd-mm-yyyy'), 4, 44, 444);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('05', 'G01', to_date('05-03-2007', 'dd-mm-yyyy'), 5, 55, 555);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('06', 'G01', to_date('01-03-2008', 'dd-mm-yyyy'), 6, 66, 666);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('07', 'G01', to_date('02-03-2008', 'dd-mm-yyyy'), 7, 77, 777);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('08', 'G01', to_date('03-03-2008', 'dd-mm-yyyy'), 8, 88, 888);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('09', 'G01', to_date('04-03-2008', 'dd-mm-yyyy'), 9, 99, 999);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('10', 'G01', to_date('05-03-2008', 'dd-mm-yyyy'), 10, 100, 1000);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('01', 'G01', to_date('01-03-2007', 'dd-mm-yyyy'), 1, 11, 111);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('02', 'G01', to_date('02-03-2007', 'dd-mm-yyyy'), 2, 22, 222);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('03', 'G01', to_date('03-03-2007', 'dd-mm-yyyy'), 3, 33, 333);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('04', 'G01', to_date('04-03-2007', 'dd-mm-yyyy'), 4, 44, 444);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('05', 'G01', to_date('05-03-2007', 'dd-mm-yyyy'), 5, 55, 555);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('06', 'G01', to_date('01-03-2008', 'dd-mm-yyyy'), 6, 66, 666);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('07', 'G01', to_date('02-03-2008', 'dd-mm-yyyy'), 7, 77, 777);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('08', 'G01', to_date('03-03-2008', 'dd-mm-yyyy'), 8, 88, 888);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('09', 'G01', to_date('04-03-2008', 'dd-mm-yyyy'), 9, 99, 999);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('10', 'G01', to_date('05-03-2008', 'dd-mm-yyyy'), 10, 100, 1000);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('01', 'G02', to_date('01-03-2007', 'dd-mm-yyyy'), 1, 11, 111);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('02', 'G02', to_date('02-03-2007', 'dd-mm-yyyy'), 2, 22, 222);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('03', 'G02', to_date('03-03-2007', 'dd-mm-yyyy'), 3, 33, 333);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('04', 'G02', to_date('04-03-2007', 'dd-mm-yyyy'), 4, 44, 444);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('05', 'G02', to_date('05-03-2007', 'dd-mm-yyyy'), 5, 55, 555);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('06', 'G02', to_date('01-03-2008', 'dd-mm-yyyy'), 6, 66, 666);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('07', 'G02', to_date('02-03-2008', 'dd-mm-yyyy'), 7, 77, 777);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('08', 'G02', to_date('03-03-2008', 'dd-mm-yyyy'), 8, 88, 888);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('09', 'G02', to_date('04-03-2008', 'dd-mm-yyyy'), 9, 99, 999);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('10', 'G02', to_date('05-03-2008', 'dd-mm-yyyy'), 10, 100, 1000);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('01', 'G03', to_date('01-03-2007', 'dd-mm-yyyy'), 1, 11, 111);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('02', 'G03', to_date('02-03-2007', 'dd-mm-yyyy'), 2, 22, 222);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('03', 'G03', to_date('03-03-2007', 'dd-mm-yyyy'), 3, 33, 333);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('04', 'G03', to_date('04-03-2007', 'dd-mm-yyyy'), 4, 44, 444);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('05', 'G03', to_date('05-03-2007', 'dd-mm-yyyy'), 5, 55, 555);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('06', 'G03', to_date('01-03-2008', 'dd-mm-yyyy'), 6, 66, 666);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('07', 'G03', to_date('02-03-2008', 'dd-mm-yyyy'), 7, 77, 777);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('08', 'G03', to_date('03-03-2008', 'dd-mm-yyyy'), 8, 88, 888);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('09', 'G03', to_date('04-03-2008', 'dd-mm-yyyy'), 9, 99, 999);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('10', 'G03', to_date('05-03-2008', 'dd-mm-yyyy'), 10, 100, 1000);
    insert into TEMP_COMP (ID, GROUP_ID, TRAN_DATE, AMT_1, AMT_2, AMT_3)
    values ('11', 'G01', to_date('03-03-2008', 'dd-mm-yyyy'), 100, 200, 300);
    commit;
    prompt 41 records loaded
    set feedback on
    set define on
    prompt Done.
    Step 3 :- Execute the query.
    SELECT GROUP_ID
    , SUM(LAST_YR_REV) as "Year_2007_Amt"
    , SUM(CURR_YR_REV) as "Year_2008_Amt"
    FROM (
    SELECT GROUP_ID,
    CASE WHEN TO_CHAR(TRAN_DATE,'YYYYMM') BETWEEN'200701'AND'200712'THEN SUM(AMT_1) ELSE 0 END AS LAST_YR_REV ,
    CASE WHEN TO_CHAR(TRAN_DATE,'YYYYMM') BETWEEN'200801'AND'200812'THEN SUM(AMT_1) ELSE 0 END AS CURR_YR_REV
    FROM TEMP_COMP t
    WHERE GROUP_ID ='G01'
    AND TO_CHAR(TRAN_DATE,'YYYYMM') BETWEEN'200601'AND'200912'
    GROUP BY GROUP_ID, TRAN_DATE
    GROUP BY GROUP_ID
    The result of above query
    GROUP_ID Year_2007_Amt Year_2008_Amt
    G01 30 180
    Step 4 : Create composite index
    create index GROUP_ID_TRAN_DATE_IDX on TEMP_COMP (GROUP_ID, TO_CHAR(TRAN_DATE,'YYYYMM'))
    Step 5 : Execute once again query from step 3.
    SELECT GROUP_ID
    , SUM(LAST_YR_REV) as "Year_2007_Amt"
    , SUM(CURR_YR_REV) as "Year_2008_Amt"
    FROM (
    SELECT GROUP_ID,
    CASE WHEN TO_CHAR(TRAN_DATE,'YYYYMM') BETWEEN'200701'AND'200712'THEN SUM(AMT_1) ELSE 0 END AS LAST_YR_REV ,
    CASE WHEN TO_CHAR(TRAN_DATE,'YYYYMM') BETWEEN'200801'AND'200812'THEN SUM(AMT_1) ELSE 0 END AS CURR_YR_REV
    FROM TEMP_COMP t
    WHERE GROUP_ID ='G01'
    AND TO_CHAR(TRAN_DATE,'YYYYMM') BETWEEN'200601'AND'200912'
    GROUP BY GROUP_ID, TRAN_DATE
    GROUP BY GROUP_ID
    The result of above query
    GROUP_ID Year_2007_Amt Year_2008_Amt
    G01 0 210
    Thanks
    Sunil

    I just wanted to make a comment. The predicates in both your queries are flawed I believe. You convert a date column to a character and then you say only pick the converted result between two sets of characters.
    TO_CHAR(TRAN_DATE,'YYYYMM') BETWEEN'200601'AND'200912'It should be coded like this for a proper date range comparison:
    TRAN_DATE BETWEEN TO_DATE('200601','YYYYMM') AND TO_DATE('200912','YYYYMM')That will eliminate the need for you to create FBI that you created. You should now be able to create a regular (B*Tree) index on the TRAN_DATE column.
    Also, now that we have the structure of your tables and sample data, what business question are you trying to answer? I think there is a better query that can be written if we know the requirements.
    Hope this helps!

  • How to implement "Filtered Indexes" in Oracle?

    I know that function-based index can (kind-of) create index for only a portion of all the rows in a table,
    create index idx_abc_01 on t_abc (case when end_dt > '01-APR-2008' then end_dt else null end);
    /* to use the index, you have to use the same express in the filter clause */
    select * from t_abc where xyz in ('O', 'S', 'X') and
         case when end_dt > '01-APR-2008' then end_dt else null end
              between '12-JUL-2008' and '15-JUL-2008';But this approach is very inflexible because the expression in the where clause has to exactly match the index creation, otherwise that filtered index will not be used.
    MS SQL Server 2008 has a new feature called - Filtered Indexes
    CREATE NONCLUSTERED INDEX fidx_abc_01
        ON t_abc ( EndDate )
    WHERE EndDate > '20040401';
    select * from t_abc where EndDate between '20050121' and '20050130';[url http://msdn.microsoft.com/en-us/library/cc280372.aspx]Filtered Indexes can be very helpful in both OLTP and DW. I'm wondering how to get the same thing done in Oracle.
    Any suggestion beyond [url http://erturkdiriksoy.wordpress.com/2008/06/30/filtered-indexes-on-oracle/]Dunyada?

    In 11.2, if you're building a data warehouse (and assuming that implies you have the partitioning option), you can declare that certain partitions of a local index are unusable which prevents Oracle from building them. If T_ABC were partitioned on END_DT, that would allow arbitrary queries where Oracle could perform partition pruning to make use of the index if it was available for those partitions without the need for an exact expression match.
    Beyond that, if you are creating a function-based index, you would often want to create (or modify) the view that your application queries to specify the condition (i.e. a VW_RECENT_ABC that only has post-April, 2008 data) so that queries didn't have to specify the condition exactly.
    Justin

  • How to find out if your query uses the indexes?

    How can one tell if the query being issued is using your indexes or doing full table scans?
    Thank you.

    Thank you.
    Ok, let me see if I understand it. So, having an
    index may not speed things up.True
    Full table scans are not bad at all -- as I have it
    in my head. The query I ran before was: select *
    from table1;In that case an index would only slow things down; you are asking it to get all the information from the table - it has to read all of the table ( a full scan).
    >
    So full table scans and index have to do with the db
    block size and the size of the row correct?
    Block size and row size don't have a huge amount to do with it, but they do play a role.
    Let me ask: How does one know the size of a row and
    then the best option for the db blocks? Block size is a global setting (at the tablespace level I think). You would not likely change the block size based on the average row length in any one table. It would be about the last thing you might look at in terms of tuning (though you might consider it up-front if you had a huge amount of very predictable data).
    >
    And if, I create indexes and queries have the where
    clause and the database uses full table scans then
    does it means that either:
    The database believes that the best execution plan is
    to either do FULL SCANS OR USE INDEXES -- ALL UPTO
    THE DATABASE?
    No. There is another piece of information that the database needs to make good decisions. If for example you have a WHERE clause "WHERE not_paid = 1" and you have an index on not_paid. To make a good decision the database needs to know about how many of the rows are likely to be not_paid =1. If it's 90% then a full table scan will be cheaper than looking up the addresses of 90% of the rows and then getting the data. If it's 10% using the index will be cheaper. You need to use Analyze Tables to get the database to store this information. Looks like you need to use a bit of time with the manuals.
    Jon
    -J

  • How to handle goup sub total for each group in oracle query ?

    hi,
    i want to handle one complex thing in oracle query.
    i have a grouping in my oracle query.
    i want to do sub total for each group in query and after that i want to subtract one group sub total from previous group subtotal ?
    so how can i handle this in oracle query ?
    any help is greatly appreciated.
    thanks.

    Hello
    Interesting requirement.
    I wonder why are you not using these calculation during the display time. If it is acceptable then you can try using the custom formula with the running total combination in crystral report to achieve to get the required results.
    Best regards
    Ali Hadi

  • How to convert SQL Server hierarchical query (CTE) to Oracle?

    How to convert SQL Server hierarchical query (CTE) to Oracle?
    WITH cte (col1, col2) AS
    SELECT col1, col2
    FROM dbo.[tb1]
    WHERE col1 = 12
    UNION ALL
    SELECT c.col1, c.col2
    FROM dbo.[tb1] AS c INNER JOIN cte AS p ON c.col2 = p.col1
    DELETE a
    FROM dbo.[tb1] AS a INNER JOIN cte AS b
    ON a.col1 = b.col1

    See: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2129904
    Ah, just spotted it's a delete statement - you won't be able to do it using subquery factoring in Oracle.
    I had a go at trying to convert for you, but notice that you reference the cte from inside the cte... I can only assume you have a table called cte too...
    DELETE FROM dbo.tb1
    WHERE col1 = 12
    OR col2 IN (SELECT col1 FROM cte)
    Edited by: Boneist on 22-Jun-2009 09:19

  • 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

Maybe you are looking for

  • DB02 in ECC60 on V5R4 takes a lot of time to open!!!

    Hi Guys, We recently installed an ECC60 system(SR2) on V5R4M0. The installation went pretty well. We installed a double stack system with both ABAP+JAVA. After the install, did the post install including creation of my new clients and SGEN. Scheduled

  • Search text in PDF file

    I would like to text search in pdf file, through java (VJ++), is it possible through java.io, i'm getting junk text. also tried to add COM wrapper through VJ++, but file is not getting loaded ?? any examples ?? Thank you

  • Acrobat produces a PDF with bold text redacted

    Printing a Crystal Report from a Citrix server to Acrobat Pro XI on a Windows 7 desktop. Bold text in the report is redacted (black box). If I print the same report from my Windows 7 desktop to Acrobat Pro XI then the bold text appears as it should.

  • Delete non-empty directory: better solution than traversal?

    So far, I'm only able to delete a directory via File.delete() when I delete each single file in this directory and recursively in any subdirectories. I wonder if there is a better/easier way to delete non-empty directories?

  • Opening pages in new tabs is slow and switches to the new tab and back. How can I stop this?

    I work with a lot of tabs and most of the time, I need pages in new tabs to open fast. Recently, whenever I open a page in a new tab, Firefox goes to the new tab for a second, then back to the tab I was on previously, although I have it set to not au