Query not using index when using 'or' clause

I have a problem with something I can't understand about indexes in Oracle 11g.
We can create test data with:
create table test2(field1 varchar2(100),field2 varchar2(100),field3 number,field4 varchar2(100));
create index test2_idx1 on test2(upper(field1));
create index test2_idx1b on test2(field1);
create index test2_idx2 on test2(field3);
DECLARE
j NUMBER :=1;
BEGIN
FOR i IN 1..500000
LOOP
INSERT
INTO test2
(field1,field2, field3, field4)
VALUES
('field1='||i,'a', j, 'i' );
IF (i mod 1000)=0 THEN
j := j+1;
END IF;
END LOOP;
COMMIT;
END;
EXEC DBMS_STATS.GATHER_TABLE_STATS ('system', 'test2');
Then I make some explain plans which result I can't understand
Query 1:
SELECT * FROM test2 WHERE field3=1;
Explain plan:
Explain plan for query 01
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1003 | 28084 | 10 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST2 | 1003 | 28084 | 10 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TEST2_IDX2 | 1003 | | 5 (0)| 00:00:01 |
Everything OK here. Index is used.
Query 2:
SELECT * FROM test2 WHERE upper(field1)='FIELD1=1';
Explain plan
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 28 | 4 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST2 | 1 | 28 | 4 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TEST2_IDX1 | 1 | | 3 (0)| 00:00:01 |
Everything OK again. Index is used.
Query 3:
SELECT /*+ USE_CONCAT */ * FROM test2 WHERE field1='FIELD1=1' OR field3=1;
Explain plan
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1004 | 28112 | 14 (0)| 00:00:01 |
| 1 | CONCATENATION | | | | | |
| 2 | TABLE ACCESS BY INDEX ROWID| TEST2 | 1 | 28 | 4 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | TEST2_IDX1B | 1 | | 3 (0)| 00:00:01 |
|* 4 | TABLE ACCESS BY INDEX ROWID| TEST2 | 1003 | 28084 | 10 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | TEST2_IDX2 | 1003 | | 5 (0)| 00:00:01 |
Indenxes are used in concatenation. No problem again.
Query 4:
SELECT /*+ USE_CONCAT */ * FROM test2 WHERE upper(field1)='FIELD1=1' OR field3=1;
Explain plan
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1004 | 28112 | 641 (4)| 00:00:08 |
| 1 | CONCATENATION | | | | | |
|* 2 | TABLE ACCESS FULL | TEST2 | 1 | 28 | 631 (4)| 00:00:08 |
|* 3 | TABLE ACCESS BY INDEX ROWID| TEST2 | 1003 | 28084 | 10 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | TEST2_IDX2 | 1003 | | 5 (0)| 00:00:01 |
Here my problem arises. Why is test2_idx1 not being used? Is it because it is a function index? Is there any workaround in this cases?
Thanks a lot in advance.

Interesting. A "workaround" which I thought first was:
SELECT /*+ USE_CONCAT */ * FROM test2 WHERE upper(field1)='FIELD1=1'
UNION ALL
SELECT /*+ USE_CONCAT */ * FROM test2 WHERE field3=1;
| Id  | Operation                    | Name       | Rows  | Bytes | Cost (%CPU)| Time     |                                                                                                                                                                                                                 
|   0 | SELECT STATEMENT             |            |  1001 | 28042 |    15  (74)| 00:00:01 |                                                                                                                                                                                                                 
|   1 |  UNION-ALL                   |            |       |       |            |          |                                                                                                                                                                                                                 
|   2 |   TABLE ACCESS BY INDEX ROWID| TEST2      |     1 |    42 |     4   (0)| 00:00:01 |                                                                                                                                                                                                                 
|*  3 |    INDEX RANGE SCAN          | TEST2_IDX1 |     1 |       |     3   (0)| 00:00:01 |                                                                                                                                                                                                                 
|   4 |   TABLE ACCESS BY INDEX ROWID| TEST2      |  1000 | 28000 |    11   (0)| 00:00:01 |                                                                                                                                                                                                                 
|*  5 |    INDEX RANGE SCAN          | TEST2_IDX2 |  1000 |       |     5   (0)| 00:00:01 |                                                                                                                                                                                                                 
-------------------------------------------------------------------------------------------   But I do not like using UNION for such tricks. So I thought, what would ORACLE do without hint?
SELECT  * FROM test2 WHERE upper(field1)='FIELD1=1' or field3=1;
| Id  | Operation                        | Name       | Rows  | Bytes | Cost (%CPU)| Time     |                                                                                                                                                                                                             
|   0 | SELECT STATEMENT                 |            |  1001 | 42042 |   176   (0)| 00:00:03 |                                                                                                                                                                                                             
|   1 |  TABLE ACCESS BY INDEX ROWID     | TEST2      |  1001 | 42042 |   176   (0)| 00:00:03 |                                                                                                                                                                                                             
|   2 |   BITMAP CONVERSION TO ROWIDS    |            |       |       |            |          |                                                                                                                                                                                                             
|   3 |    BITMAP OR                     |            |       |       |            |          |                                                                                                                                                                                                             
|   4 |     BITMAP CONVERSION FROM ROWIDS|            |       |       |            |          |                                                                                                                                                                                                             
|*  5 |      INDEX RANGE SCAN            | TEST2_IDX2 |       |       |     5   (0)| 00:00:01 |                                                                                                                                                                                                             
|   6 |     BITMAP CONVERSION FROM ROWIDS|            |       |       |            |          |                                                                                                                                                                                                             
|*  7 |      INDEX RANGE SCAN            | TEST2_IDX1 |       |       |     3   (0)| 00:00:01 |                                                                                                                                                                                                             
----------------------------------------------------------------------------------------------- Thist looks perfect to me. (Please ignore the "higher" cost on the second plan. On your real data, it should be faster. Is it?)

Similar Messages

  • Using indexes when using group by clause

    How can I make use of indexes when there is a group by clause in the sql/ pl/sql. Heard that when ever there is a group by clause , sql bypasses use of indexes. Is it true?
    Thanks in advance

    Hi,
    Depending on the query containing the group by, indexes will still be used.
    For example, create a big table based on the all_objects table. Then create an index on the object_type column. Then run this query:
    select count(*), object_type from big_table where object_type != 'PACKAGE' group by object_type
    The execution plan for this query will show the index to be used, the group by does not prevent the optimizer from using an index. Then run this query:
    select count(*), object_type from big_table group by object_type
    The execution plan for this query will show the index not to be used because the whole table - no restrictions - needs to be read to get the result.
    Hope this helps!
    Regards,
    Marco Stuijvenberg
    =
    www.marcostuijvenberg.nl

  • Content Query not producing results when using [Me] filter

    Hi
    I'm using a Content Query web part and I'm trying to show the most recent document modified by each site user by applying it across the site collection and using the Filter, Modified By [_Hidden] equals [Me]. However, this doesn't seem to work for any user
    - the web part is always blank. I have tried different combinations such changing equals to contains, and using the Modified By field etc - nothing gives me the correct results. If I instead take the [Me] out and instead use the "people chooser"
    and set to a specific person then I seem to get the results I would expect - however I cannot hard code a given user.
    Does anyone know how to use [Me] correctly?
    Thanks

    Hi  sjb500,
    According to your description, please take steps as below to meet your demand:
    With the page layout open in Design view, double-click the Content Query Web Part.
    In the Content Query Web Part dialog box, click the plus sign (+) next to Query to expand it.
    In the Source section, click Show items from all sites in this site collection.
    In List Type section, in the Show items from this list type, click Document Library.
    In the Content Type section, in the Show items of this content type group list, click Document
    Content Types.
    In the Content Type section, in the Show items of this content type list, click Document.
    In the Additional Filters section, under Show Items When, click Modified By [_Hidden] in
    the first box, click Is Equal To in the second box, and then click [Me].
    In the Content Query Web Part dialog box, click the plus sign (+) next to Presentation to expand it.
    In the Grouping and Sorting section, in the Sort items by list, click Modified
    and Select Show items in descending order.
    Click OK.
    Reference:http://office.microsoft.com/en-us/sharepoint-designer-help/display-data-from-multiple-lists-with-the-content-query-web-part-HA010174134.aspx
    [Me] is a variable that stands for the user who is currently viewing the page that contains the Content Query Web Part in the browser.
    Here is some scenarios using [Me] in Content Query Web Part:
    http://social.technet.microsoft.com/Forums/en-US/346ffbe6-d7ba-467e-b7f5-6d6e289677a1/user-tasks-web-part
    Please inform me freely if you have any questions.
    Thanks

  • How to use index when use analytic functions?

    Oracle 11gR2, I have a an indexed snapshot log table (sequence$$ and PK indexed), and I use a query to access it...
    query looks like:
    SELECT "M_ROW$$", "SNAPTIME$$", "DMLTYPE$$", "OLD_NEW$$", "CHANGE_VECTOR$$", "SEQUENCE$$", "ID", "SITEID", "WEBPAGEID", "WEBFOLDERID"
    FROM mlog$_table1
    WHERE dmltype$$ = 'D'
    AND sequence$$ IN
    (SELECT DISTINCT
    LAST_VALUE (sequence$$)
    OVER (PARTITION BY siteid ORDER BY siteid)
    FROM mlog$_table1);
    how could I access mlog$_table1
    explain plan looks like:
    Plan
    SELECT STATEMENT ALL_ROWSCost: 53 Bytes: 605.864 Cardinality: 1.736                          
         6 HASH JOIN Cost: 53 Bytes: 605.864 Cardinality: 1.736                     
              4 VIEW VIEW SYS.VW_NSO_1 Cost: 28 Bytes: 22.555 Cardinality: 1.735                
                   3 HASH UNIQUE Cost: 28 Bytes: 45.110 Cardinality: 1.735           
                        2 WINDOW SORT Cost: 28 Bytes: 45.110 Cardinality: 1.735      
                             1 TABLE ACCESS FULL TABLE BONGO.MLOG$_PDSI1 Cost: 25 Bytes: 45.110 Cardinality: 1.735
              5 TABLE ACCESS FULL TABLE BONGO.MLOG$_PDSI1 Cost: 25 Bytes: 582.960 Cardinality: 1.735

    ok, I think I feel like a newbie :)
    explain plan for your query is:
    Plan
    SELECT STATEMENT ALL_ROWSCost: 22,1 Bytes: 416,678 Cardinality: 2,654           
         2 SORT GROUP BY Cost: 22,1 Bytes: 416,678 Cardinality: 2,654      
              1 TABLE ACCESS FULL TABLE IBIS.MLOG$_PARTIJA Cost: 22,098 Bytes: 416,678 Cardinality: 2,654
    but it runs for about 3-4 sec..
    1st query:
    create or replace view x as
    select * from mlog$_partija v
    where not (DMLTYPE$$='D' and update_type=2)
    and sequence$$=(select max(sequence$$) from mlog$_partija t
    where t.par_sifpar=v.par_sifpar);
    runs about 17 sec...
    wich is expected since there's a max in subselect...
    but, it's explain plan is:
    Plan
    SELECT STATEMENT ALL_ROWSCost: 108,957 Bytes: 1,84 Cardinality: 1                     
         5 HASH JOIN Cost: 108,957 Bytes: 1,84 Cardinality: 1                
              3 VIEW VIEW SYS.VW_SQ_1 Cost: 22,564 Bytes: 68.634.228 Cardinality: 2.639.778           
                   2 HASH GROUP BY Cost: 22,564 Bytes: 68.634.228 Cardinality: 2.639.778      
                        1 TABLE ACCESS FULL TABLE IBIS.MLOG$_PARTIJA Cost: 22,05 Bytes: 68.634.228 Cardinality: 2.639.778
              4 TABLE ACCESS FULL TABLE IBIS.MLOG$_PARTIJA Cost: 22,145 Bytes: 4.788.557.292 Cardinality: 2.639.778           
    but, I made a third solution wich is something like:
    create table mlog_table1_last as
    select siteid, max(sequence$$) max_seq from mlog$_table1 group by siteid
    I run a query on that temp table:
    select * from mlog$_table1 v
    where not (DMLTYPE$$='D' and update_type=2)
    and sequence$$=(select max_seq from mlog_table1_last t
    where t.siteid=v.siteid);
    it's explain plan is bad:
    Plan
    SELECT STATEMENT ALL_ROWSCost: 2.588.886 Bytes: 1,814 Cardinality: 1           
         3 FILTER      
              1 TABLE ACCESS FULL TABLE IBIS.MLOG$_PARTIJA Cost: 22,148 Bytes: 4.788.557.292 Cardinality: 2.639.778
              2 INDEX RANGE SCAN INDEX IBIS.MLOG_PARIJA_LAST_NDX2 Cost: 1 Bytes: 111,02 Cardinality: 4,27
    but it runs for 1-2 sec..
    why?

  • Can not print report when using HTML or DHTML

    Can not print report when using HTML or DHTML. When I open the report and click the print icon I get a small blank dialog box and then nothing. If I change to activex it works and I can print.
    Any Ideas?
    Thanks
    Jeff

    Hi,
    I did not come across this situation before. Did you apply any fix packs. Try to check whether its a issue which can be fixed by applying fix pack( try to check release notes of fix pack and see if they mentioned any thing about this).
    Thanks,
    SK.

  • After updating my Macbook Pro retina display to os x yosemite 10.10.2, the mause and track pad locks, and do not respond especially when using the Mac for a long period, please help, how can I solve this, I do not like feel like in windows, so I paid

    after updating my Macbook Pro retina display to os x yosemite 10.10.2, the mause and track pad locks, and do not respond especially when using the Mac for a long period, please help, how can I solve this, I do not like feel like in windows, so I paid good money for this mack, I feel calm

    Hi Buterem,
    I'm sorry to hear you are having issues with your MacBook Pro since your recent Yosemite update. I also apologize, I'm a bit unclear on the exact nature of the issue you are describing. If you are having intermittent but persistent responsiveness issues with your mouse or trackpad, you may want to try using Activity Monitor to see if these incidents correspond to occupied system resources, especially system memory or CPU. You may find the following article helpful:
    How to use Activity Monitor - Apple Support
    If the entire system hangs or locks up (for example, if the system clock freezes and stops counting up), you may also be experiencing some variety of Kernel Panic. If that is the case, you may also find this article useful:
    OS X: When your computer spontaneously restarts or displays "Your computer restarted because of a problem." - Apple Support
    Regards,
    - Brenden

  • RPC Server not Available error when using Windows Fax & Scan under Windows 7

    RPC Server not Available error when using Windows Fax & Scan under Windows 7

    Hi,
    On current situation, please remove the old fax account and re-add a new fax account in Windows Fax and Scan, then check if this issue sill exist. For more details, please refer to following similar thread and artilces.
    RPC Server not Available error when using Windows Fax & Scan
    under Windows
    The local fax account is inaccessible after you rename a Windows Vista-based computer
    Cannot send fax from Windows Vista/Windows 7 PC with “RPC Server not Available” error message
    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please
    make sure that you completely understand the risk before retrieving any suggestions from the above link.
    If any update, please feel free to let me know.
    Best regards,
    Justin Gu
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact [email protected]

  • I have a HP610-1150f. not recording sound when using webcam in touchsmart screen.

    I have a HP610-1150f. not recording sound when using webcam in touchsmart screen.  I have looked thru this forum and I don't see anyone else having this problem. (unless I missed it).  The recording works in Notes, but not when I open Webcam and try to record from there. video recording works fine. I have had this computer less than a month and I am having a couple of problems with it. Still loving it though.

    Hello schae731: Welcom to HP Forum and your new Touchsmart. You need to contact HP Direct Support on line your Touchsmart comes  with one year warranty so use it why you can. You can contact them through your HP Support Assistant or on line. They will help you with trouble shoot the web-cam software. Goto your computers model and make support web-site. They will also help you with your other issues as well.
             I am using the older version software so I won't be much help. Please repost if HP direct support is un able to help you resolve your issues. While you are chatting with them have them check out your computer do it before your 1 year warranty expires. Have fun with your new computer.

  • "access restrictions" did not work sometimes when using 3-tier DeskI.

    My customer found that "access restrictions" did not work sometimes when using 3-tier DeskI.
    But this issue can be solved by logging on from another machine, or restarting the DeskI.
    For I can reproduce this issue, so I just want to know that:
    1.What is it probably related to?
    2.If this issue happens again, what can I suggest my customer for tracking it?
      For example, get some log files from servers etc.
    Thanks!

    Hi Sarah,
    Also you can try the following solution.
    1. Import the universe.
    2. Go to manage access restrictions
    3. Remove the restriction .
    4. Again create the rescrition and unchecked the " limit size of result set to"
    5. Now assign it to the unlimited results group ( this is the name of
    the group we have given to those users who should be able to retrieve
    more than X rows)
    6. Now we save the universe. (Dont export the universe).
    I hope this will help you.
    Regards,
    Sarbhjeet Kaur

  • Post Moved Galaxy-Note-dropping-Wifi-when-using-...

    Pot moved http://community.bt.com/t5/Other-BB-Queries/Galaxy-Note-dropping-Wifi-when-using-HH3/td-p/671308
    If you want to say thanks for a helpful answer,please click on the Ratings star on the left-hand side If the reply answers your question then please mark as ’Mark as Accepted Solution’

    I think I found the answer to my own question (no thanks to Verizon)!
    I found this article online which was written recently (2012) and explains the WIFI - Bluetooth interference pretty well. Here is the link:
    http://www.ecnmag.com/articles/2012/03/wi-fi-and-bluetooth-coexistence
    The way it has been explained is that if the WIFI & BT radios are on the same chip and using the same antena to send and receive, they WILL interfere no matter what. Having them on two different chips is the best way. But Frequency Isolation (Adaptive Frequency Hopping (AFH)) is a way to reduce interference when they are both on the same chip (as it is in Galaxy Note 2 & S3 - they have Broadcom BCM4330 for wifi/bluethooth/FM) which I guess needs to be addressed in a software update (or may be it already has and is not working well). The last solution is to use 5GHz band for WIFI.
    There you have it. But please let me know what you think and if you have found out anything else.
    Thanks

  • Is there a known issue for using IE11 when using business catalyst cms

    Is there a known issue for using IE11 when using business catalyst cms?  I have a client trying to update pages on her site using IE11 and the it says it updates successfully, but the new content is not saved.

    I concur with Barry; I see no significant difference in using the Home button vs. the gestures. But the "fade" effect is part of iOS 7.1 and there's no way to disable it that I know of.
    Regards.

  • Question about using email when using computer at internet cafe

    I am confused about using email when using my computer at an internet cafe.
    Of course - I use the internet cafe's wifi connection/network.
    How does this effect my email?
    what do I have to do to use my email when logged on at an internet cafe?

    zxcvbnm1 wrote:
    I am confused about using email when using my computer at an internet cafe.
    Of course - I use the internet cafe's wifi connection/network.
    How does this effect my email?
    what do I have to do to use my email when logged on at an internet cafe?
    You don't give enough information to do more than discuss the issue in general terms.  
    You should be able to read your email wherever you can get an internet connection.
    To send email, it depends on the email provider you are using.  Gmail and other web based mail should not have any issues.  However, if you are attempting to use an account that uses your ISP's mail servers, you will probably not be able to send mail.  You may want to check with your ISP in that case, to see if they offer webmail.  
    Hope this may have answered your question.

  • Query don't use the right index when using bind variables

    Hi people !
    I need some help because I have an issue with a query that don t use the right Indexes as it should
    First of all, I have mainly three tables :
    ORDER : Table that contains description for each Order (approximately 1 000 000 Records)
    ORDER_MVTS : Table that contains the tasks made (called movements) to set up each Orders
    with quantity of packages prepared for each product (approximately 10 000 000 Records)
    PRODUCT : Tables that contains the products (approximately 50 000 Records)
    When I launch the query with hard coded values, it brings back response very fast
    because it uses the right index (ORDER_DHR_VALID) which represent the date and hour of the order
    (with format 'DD/MM/YYYY HH24:MI:SS'). The selectivity for this index is good.
    NB 1: I have to use the trick " >= Trunc(date) and < trunc(date) +1 " to filter on a simple date because
    the index contains hour and minutes (I know it wasn't probably a bright idea at conception time).
    NB 2: The index on ORDER_MVTS.PRODUCT_CODE is'nt discriminating enough because there is'nt enough different products.
    It's the same for index on CUSTOMER_CODE and on MVT_TYPE so only the index on ORDER.DHR_VALID is good.
    Here is the correct explain plan when I execute the query with hard coded values :
    SELECT SUM(ORDER_MVTS.NB_PACKAGE)
    FROM ORDER_MVTS, PRODUCT, ORDER
    WHERE ORDER.DHR_VALID >= TRUNC(to_date('14/11/2008 10:04:56','DD/MM/YYYY HH24:MI:SS'))
    AND ORDER.DHR_VALID < TRUNC(to_date('14/11/2008 10:04:56','DD/MM/YYYY HH24:MI:SS')) + 1
    AND ORDER_MVTS.MVT_TYPE = 'DELIVERY'
    AND PRODUCT.CODE = ORDER_MVTS.PRODUCT_CODE
    AND ORDER_MVTS.ORDER_CODE = ORDER.CODE
    AND ORDER.CUSTOMER_CODE = 'ADIDAS'
    AND PRODUCT.CODE = 1234
    Rows Row Source Operation
    1 SORT AGGREGATE
    2 NESTED LOOPS
    4 NESTED LOOPS
    2 INDEX UNIQUE SCAN (object id 378548) --> PRODUCT_PK
    4 TABLE ACCESS BY INDEX ROWID ORDER
    777 INDEX RANGE SCAN (object id 378119) --> ORDER_DHR_VALID
    2 TABLE ACCESS BY INDEX ROWID ORDER_MVTS
    30 INDEX RANGE SCAN (object id 377784) --> ORDER_MVTS_ORDER_FK
    Now the problem is when the query is used in a Cursor with bind variables.
    It seems like Oracle don't use index on ORDER.DHR_VALID because he can't figure out that he have
    to actually filter on a short period of time (only one day).
    So Oracle uses the index on ORDER_MVTS.PRODUCT_CODE which is'nt a bright idea (it takes 10 secondes instead of just one)
    Here is the bad explain plan :
    Rows Row Source Operation
    1 SORT AGGREGATE
    2 NESTED LOOPS
    722 NESTED LOOPS
    2 INDEX UNIQUE SCAN (object id 378548) --> PRODUCT_PK
    722 TABLE ACCESS BY INDEX ROWID ORDER_MVTS
    1790 INDEX RANGE SCAN (object id 377777) --> ORDER_MVTS_PRODUCT_FK
    2 TABLE ACCESS BY INDEX ROWID ORDER
    1442 INDEX UNIQUE SCAN (object id 378439) --> ORDER_PK
    Now I have found two solutions to this problem :
    1) using a Hint to force the use of index on ORDER.DHR_VALID (with /*+ INDEX(ORDER ORDER_DHR_VALID) */ )
    2) Using Dynamic SQL and keeping the date hard coded (but not the other values except mvt_type)
    For example :
    QUERY :=
    'SELECT SUM(ORDER_MVTS.NB_PACKAGE)
    FROM ORDER_MVTS, PRODUCT, ORDER
    WHERE ORDER.DHR_VALID >= TRUNC(TO_DATE('''||To_char(P_DTE_VAL,'DD/MM/YYYY')||''',''DD/MM/YYYY'')) '||
    AND ORDER.DHR_VALID < TRUNC(TO_DATE('''||To_char(P_DTE_VAL,'DD/MM/YYYY')||''',''DD/MM/YYYY'')) + 1 '||
    AND ORDER_MVTS.MVT_TYPE = 'DELIVERY'
    AND PRODUCT.CODE = ORDER_MVTS.PRODUCT_CODE
    AND ORDER_MVTS.ORDER_CODE = ORDER.CODE
    AND ORDER.CUSTOMER_CODE = :CUSTOMER
    AND PRODUCT.CODE = :CODE ';
    These two solutions work but Number 1 is bad in theory because it uses a Hint
    and Number 2 may be difficult to code.
    So my question is : Does someone knows another solution to force the use of index ORDER_DHR_VALID that can be simple and reliable.
    Thank you very much for support
    Edited by: remaï on Apr 1, 2009 4:08 PM

    What version of oracle you have? CBO work is different in 9i and 10g.
    Usually cost based optimizer do not want to use index for >< condition with binding variables because optimizer can not use statistic to determine selectivity, and by default selectivity of <> operators is low.
    (As I remember '>' selectivity by default is 5%, you have two conditions > and <, therefore resulting selectivity will be 0.05*0.05=0.0025 as two independent events, but selectivity of other conditions
    ORDER_MVTS.MVT_TYPE = 'DELIVERY' or ORDER.CUSTOMER_CODE = 'ADIDAS' looks much better for CBO)
    The best solution I see is do not use binding variables. Actually your query looks as searching query, which executes not so often, therefore you will not have perfomance win along of skipping execution plan creation.
    Edited by: JustasVred on Apr 1, 2009 10:10 AM

  • Query takes 5 min when using Indexes and 1 Second without Indexes !!

    Hi,
    We have been using indexes on all tables until recently when we faced problems with queries like the one below:
    SELECT a.std_id FROM students a, student_degree b, student_course c, course d
    WHERE b.std_id = a.std_id
    AND c.std_id = a.std_id
    AND d.crn = c.crn
    AND b.in_progress = 'Y'
    AND b.major_code = 'ABTC'
    AND a.program_code = 'DP'
    AND a.level_code = 'S2'
    AND a.campus_code = '05'
    AND a.termcode = '200702';
    This query takes more than 5 minutes to return a result, but as soon as we remove indexes on the columns termcode and campus_code,it shows result in 1 second.
    What could be the problem ?, Is there an attribute that need to be set when creating these indexes ?
    Thanks in advance
    Madani

    Thank you Karthik for your reply.Here are the explain plan report (as shown on Oracle 9i Enterprise Manager)
    *1-Explain plan without Indexes:*
    Execution Steps:
    Step # Step Name
    11 SELECT STATEMENT
    10 MERGE JOIN
    7 SORT [JOIN]
    6 NESTED LOOPS
    4 NESTED LOOPS
    1 ERMS.STUDENT_DEGREE TABLE ACCESS [FULL]
    3 ERMS.STUDENTS TABLE ACCESS [BY INDEX ROWID]
    2 ERMS.SYS_C006642 INDEX [UNIQUE SCAN]
    5 ERMS.SYS_C007065 INDEX [RANGE SCAN]
    9 SORT [JOIN]
    8 ERMS.COURSE TABLE ACCESS [FULL]
    Step # Description
    1 This plan step retrieves all rows from table STUDENT_DEGREE.
    2 This plan step retrieves a single ROWID from the B*-tree index SYS_C006642.
    3 This plan step retrieves rows from table STUDENTS through ROWID(s) returned by an index.
    4 This plan step joins two sets of rows by iterating over the driving, or outer, row set (the first child of the join) and, for each row, carrying out the steps of the inner row set (the second child). Corresponding pairs of rows are tested against the join condition specified in the query's WHERE clause.
    5 This plan step retrieves one or more ROWIDs in ascending order by scanning the B*-tree index SYS_C007065.
    6 This plan step joins two sets of rows by iterating over the driving, or outer, row set (the first child of the join) and, for each row, carrying out the steps of the inner row set (the second child). Corresponding pairs of rows are tested against the join condition specified in the query's WHERE clause.
    7 This plan step accepts a row set (its only child) and sorts it in preparation for a merge-join operation.
    8 This plan step retrieves all rows from table COURSE.
    9 This plan step accepts a row set (its only child) and sorts it in preparation for a merge-join operation.
    10 This plan step accepts two sets of rows sorted on the join key. By walking both sets of rows in the order of the join key, every distinct pair of rows satisfying the join condition in the WHERE clause is found through a single pass of the row sets.
    11 This plan step designates this statement as a SELECT statement.
    *2-Explain plan with Indexes:* (I added index on column campus_code which is a varchar2 column)
    Execution Steps:
    Step # Step Name
    11 SELECT STATEMENT
    10 MERGE JOIN
    7 SORT [JOIN]
    6 NESTED LOOPS
    4 NESTED LOOPS
    1 ERMS.COURSE TABLE ACCESS [FULL]
    3 ERMS.STUDENTS TABLE ACCESS [BY INDEX ROWID]
    2 ERMS.INDEX_STUDENTS_CAMPUS_CODE INDEX [RANGE SCAN]
    5 ERMS.SYS_C007065 INDEX [RANGE SCAN]
    9 SORT [JOIN]
    8 ERMS.STUDENT_DEGREE TABLE ACCESS [FULL]
    Step # Description
    1 This plan step retrieves all rows from table COURSE.
    2 This plan step retrieves one or more ROWIDs in ascending order by scanning the B*-tree index INDEX_STUDENTS_CAMPUS_CODE.
    3 This plan step retrieves rows from table STUDENTS through ROWID(s) returned by an index.
    4 This plan step joins two sets of rows by iterating over the driving, or outer, row set (the first child of the join) and, for each row, carrying out the steps of the inner row set (the second child). Corresponding pairs of rows are tested against the join condition specified in the query's WHERE clause.
    5 This plan step retrieves one or more ROWIDs in ascending order by scanning the B*-tree index SYS_C007065.
    6 This plan step joins two sets of rows by iterating over the driving, or outer, row set (the first child of the join) and, for each row, carrying out the steps of the inner row set (the second child). Corresponding pairs of rows are tested against the join condition specified in the query's WHERE clause.
    7 This plan step accepts a row set (its only child) and sorts it in preparation for a merge-join operation.
    8 This plan step retrieves all rows from table STUDENT_DEGREE.
    9 This plan step accepts a row set (its only child) and sorts it in preparation for a merge-join operation.
    10 This plan step accepts two sets of rows sorted on the join key. By walking both sets of rows in the order of the join key, every distinct pair of rows satisfying the join condition in the WHERE clause is found through a single pass of the row sets.
    11 This plan step designates this statement as a SELECT statement.

  • How to use index, when query has decode/case

    Hi,
    I have the following query
    i have a index on party_id,party_type_Code in the zx_party_tax_profile table But this index is not used as Iam using a decode on the columns of the zx_party_tax_profile table,
    Is there any way i can rewrite the query so that it uses index
    sELECT /*+ INDEX(ZX_PARTY_TAX_PROFILE_U2) */ party_tax_profile_id FROM (SELECT
    ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
    ThirdPartyTaxProfileEO.CUSTOMER_FLAG,
    ThirdPartyTaxProfileEO.SITE_FLAG,
    ThirdPartyTaxProfileEO.PARTY_TAX_PROFILE_ID,
    ThirdPartyTaxProfileEO.PARTY_ID,
    ThirdPartyTaxProfileEO.REP_REGISTRATION_NUMBER,
    ThirdPartyTaxProfileEO.OBJECT_VERSION_NUMBER,
    ThirdPartyTaxProfileEO.REGISTRATION_TYPE_CODE,
    ThirdPartyTaxProfileEO.COUNTRY_CODE,
    ThirdPartyTaxProfileEO.MERGED_TO_PTP_ID,
    ThirdPartyTaxProfileEO.MERGED_STATUS_CODE,
    ThirdPartyTaxProfileEO.PROGRAM_APP_NAME,
    ThirdPartyTaxProfileEO.PROGRAM_NAME,
    PartyPEO.PARTY_NAME,
    PartyPEO.PARTY_ID AS PARTY_ID1,
    PartyPEO.PARTY_NUMBER,
    decode(ThirdPartyTaxProfileEO.CUSTOMER_FLAG,
    'Y',decode(ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
    'Y', 'SC',
    'C'),
    decode(ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
    'Y', 'S',
    NULL)
    ) AS PARTY_USAGE,
    ThirdPartyTaxProfileEO.REP_REGISTRATION_NUMBER AS TAX_REG_NUMBER,
    LkupPartyUsage.MEANING AS PARTY_USAGE_DESC,
    PartyPEO.PARTY_NAME AS PARTY_FULL_NAME,
    PartyPEO.ADDRESS1||','||
    PartyPEO.ADDRESS2||','||
    PartyPEO.ADDRESS3||','||
    PartyPEO.CITY||','||
    PartyPEO.postal_code||','||
    PartyPEO.COUNTRY AS ADDRESS,
    PartyPEO.COUNTRY AS COUNTRY_CODE_TCA,
    TerritoryPEO.TERRITORY_SHORT_NAME AS COUNTRY_NAME,
    PartyPEO.JGZZ_FISCAL_CODE AS TAX_PAYER_ID,
    PartyPEO.DUNS_NUMBER_C AS DUNS_NUMBER,
    PartyPEO.Party_Number as Party_Num_Calc,
    null as REGISTRATION_TYPE_NAME,
    null as ROUNDING_LEVEL_NAME,
    null as ROUNDING_RULE_NAME,
    null as COUNTRY_NAME_PTP,
    'ZX_PARTY_TAX_PROFILE' as TAX_REPORTING_ENTITY_CODE
    FROM ZX_PARTY_TAX_PROFILE ThirdPartyTaxProfileEO,
    HZ_PARTIES PartyPEO,
    FND_LOOKUP_VALUES_VL LkupPartyUsage,
    FND_TERRITORIES_VL TerritoryPEO
    WHERE ThirdPartyTaxProfileEO.PARTY_ID = PartyPEO.PARTY_ID AND
    LkupPartyUsage.LOOKUP_CODE = decode(ThirdPartyTaxProfileEO.CUSTOMER_FLAG,
    'Y',decode(ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
    'Y', 'SC',
    'C'),
    decode(ThirdPartyTaxProfileEO.SUPPLIER_FLAG,
    'Y', 'S',
    NULL)
    ) AND
    PartyPEO.COUNTRY = TerritoryPEO.Territory_Code (+) AND
    LkupPartyUsage.LOOKUP_TYPE = 'ZX_TP_PARTY_USAGE'
    ORDER BY UPPER(PARTY_FULL_NAME)) QRSLT WHERE UPPER(PARTY_NAME) IS NOT
    NULL
    Any help will be appreciated

    You can rewrite your where clause to not use decode or case statements e.g. this:
      AND LkupPartyUsage.LOOKUP_CODE = DECODE( ThirdPartyTaxProfileEO.CUSTOMER_FLAG
                                , 'Y', DECODE( ThirdPartyTaxProfileEO.SUPPLIER_FLAG, 'Y', 'SC', 'C' )
                                     , DECODE( ThirdPartyTaxProfileEO.SUPPLIER_FLAG, 'Y', 'S', NULL ) )
                                     )can be rewritten to this:
    and (
          ( ThirdPartyTaxProfileEO.CUSTOMER_FLAG = 'Y'  AND
              ((ThirdPartyTaxProfileEO.SUPPLIER_FLAG = 'Y' AND LkupPartyUsage.LOOKUP_CODE = 'SC') or
                ThirdPartyTaxProfileEO.SUPPLIER_FLAG != 'Y' AND LkupPartyUsage.LOOKUP_CODE = 'C')) or
          ( ThirdPartyTaxProfileEO.CUSTOMER_FLAG != 'Y'  AND
              ((ThirdPartyTaxProfileEO.SUPPLIER_FLAG = 'Y' AND LkupPartyUsage.LOOKUP_CODE = 'S') or
                ThirdPartyTaxProfileEO.SUPPLIER_FLAG != 'Y' AND LkupPartyUsage.LOOKUP_CODE is null))
        )It's not as sussinct, but it avoids the use of functions that could be preventing the optimiser from using an index.

Maybe you are looking for

  • Purchase Requisition Item Version Completion

    Dear Friends,                       In a purchase requisition line item, In the version tab when the version check box is newly ticked, I want to capture that change. I tried to use get_datax method of IF_purchase_requisition _item in the line item c

  • What is an Oracle Client and why is it needed

    Hi, I have an application that is using an Oracle database as data source. Application and database is located on two different servers. Now I will move application to a new server and to do this I need to install an Oracle client and install this on

  • Thin line at top of HD channels

    When watching a number of HD channels, there is a very thin line above the picture box.  The line looks like it is the bottom of the picture that was accidentally projected ontot he top of the picture. I reset the reciever a few times and it goes awa

  • HT5610 How do you authorize a PC?  This is the most ridiculous thing I've ever seen.

    How do you authorize a PC?

  • User Exit or Badi for CN60 transaction

    Hi Can anyone tell me the user exits or Badis for Tcode CN60. i need to Include the Comments field for the activities from CN22.soi would like to know  is there any userexit for CN60. Thanks Sowjanya