Conditional operator in where clause?

I have 4 assignable parameters for filtering records. If a parameter is not assigned the filter is ignored. This is working fine with the SQL-statement below except for one scenario: if none of the parameters are assigned I get no records at all but then I need all of the records.
with t as
    select 1 as val, 'a' as var from dual union all
    select 2 as val, 'a' as var from dual union all
    select 3 as val, 'b' as var from dual union all
    select 4 as val, 'b' as var from dual union all
    select 5 as val, 'b' as var from dual union all
    select 6 as val, 'c' as var from dual union all
    select 7 as val, 'd' as var from dual union all
    select 8 as val, 'e' as var from dual union all
    select 9 as val, 'e' as var from dual union all
    select 0 as val, 'f' as var from dual
select *
from t
where t.var = decode(:pvar1,'',null,:pvar1)
or t.var = decode(:pvar2,'',null,:pvar2)
or t.var = decode(:pvar3,'',null,:pvar3)
or t.var = decode(:pvar4,'',null,:pvar4)

You need no "OR"s
<br>
SQL> var pvar1 char
SQL> var pvar2 char
SQL> var pvar3 char
SQL> var pvar4 char
SQL> with t as(    select 1 as val, 'a' as var from dual union all
  2      select 2 as val, 'a' as var from dual union all
  3      select 3 as val, 'b' as var from dual union all
  4      select 4 as val, 'b' as var from dual union all
  5      select 5 as val, 'b' as var from dual union all
  6      select 6 as val, 'c' as var from dual union all
  7      select 7 as val, 'd' as var from dual union all
  8      select 8 as val, 'e' as var from dual union all
  9      select 9 as val, 'e' as var from dual union all
10      select 0 as val, 'f' as var from dual)
11  select *from t
12  where t.var = coalesce(:pvar1,:pvar2,:pvar3,:pvar4,t.var);
       VAL V
         1 a
         2 a
         3 b
         4 b
         5 b
         6 c
         7 d
         8 e
         9 e
         0 f
10 rows selected.
SQL> exec :pvar4 := 'b';
PL/SQL procedure successfully completed.
SQL>  with t as(    select 1 as val, 'a' as var from dual union all
  2       select 2 as val, 'a' as var from dual union all
  3       select 3 as val, 'b' as var from dual union all
  4       select 4 as val, 'b' as var from dual union all
  5       select 5 as val, 'b' as var from dual union all
  6       select 6 as val, 'c' as var from dual union all
  7       select 7 as val, 'd' as var from dual union all
  8       select 8 as val, 'e' as var from dual union all
  9       select 9 as val, 'e' as var from dual union all
10       select 0 as val, 'f' as var from dual)
11   select *from t
12   where t.var = coalesce(:pvar1,:pvar2,:pvar3,:pvar4,t.var);
       VAL V
         3 b
         4 b
         5 b
Message was edited by:
        jeneesh
I did mistake. Follow Dave's post                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • Function-based Index and an OR-condition in the WHERE-clause

    We have some problems with functin-based indexes and
    the or-condition in a where-clause.
    (We use oracle 8i (8.1.7))
    create table TPERSON(ID number(10),NAME varchar2(20),...);
    create index I_NORMAL_TPERSON_NAME on TPERSON(NAME);
    create index I_FUNCTION_TPERSON_NAME on TPERSON(UPPER(NAME));
    The following two statements run very fast on a large table
    and the execution-plan asure the usage of the indexes
    (-while the session is appropriate configured and the table is analyzed):
    1)     select count(ID) FROM TPERSON where upper(NAME) like 'MIL%';
    2)     select count(ID) from TPERSON where NAME like 'Mil%' or (3=5);
    In particular we see that a normal index is used while the where-clause contains
    an OR-CONDITION.
    But if we try the similarly select-statement
    3)     select count(ID) FROM TPERSON where upper(NAME) like 'MIL%' or (3=5);
    the CBO will not use the function-index.
    (This behavior we only expect with views but not with indexes.)
    We ask for an advice like an hint, which enable the CBO-usage
    of function-based indexes in connection with OR.
    This problem seems to be artificial because it contains this dummy logic:
         or (3=5).
    This steams from an prepared statement, where this kind of boolean
    flag reduce the amount of different select-statements needed for
    covering the hole business-logic, while using bind-variables for the
    concrete query-parameters.
    A more realistic (still boild down) version of our prepared select-statement run in
    SQL Plus:
    define x_name = 'MIL%';
    define x_firstname = '';
    select * FROM TPERSON
    where (upper(NAME) like '&x_name' or ( '&x_name' = ''))
    and (upper(FIRSTNAME) like '&x_firstname' or ('&x_firstname' = ''))
    and ...;
    In particular we dont refernce the tablecolumn , but the QUERY-Parameter
    yield the second boolean value in the or-condition.
    The problem is that this condition ('&x_name' = '') dont use any index.
    thanks a lot for spending your time with this problem

    Try
    SELECT /*+ RULE */
    as your hint. I don't have the book with me, but this last weekend I read a section about your very problem. The book was a Oracle Press gold cover about Oracle 8i Performance tuning. If you e-mail me I can quote you the chapter when I get home Friday.

  • Like condition in the where clause.

    Dear All,
    I have created a new form using a datablock wizard with one table.
    Now i placed a text box and a button in the same form. I will be entering the value in the text box and based up the condition in the where clause of the datablock property i am restricting the values where the column name is thetext box value.
    Now my problem is i need to give a string value in the text box and the datablokc has to populate based up the like condtion. I dont know the syntax how to give like condition in the where clause of the property pallet.
    Please help me in this.
    Thanks in advance.
    Cheers,
    Raju

    Maybe try this ...
    Create a button to query the block :
    In the WHEN-BUTTON-PRESSED-Trigger enter :
    DECLARE
        v_block_def_where         VARCHAR2(255)     := '1 = 1';
    BEGIN
        v_block_def_where := v_block_def_where || ' AND NAME_OF_YOUR_TABLE_COLUMN LIKE ''' || '%' || :NAME_OF_YOUR_BLOCK.SEARCH_ITEM || '%' ||  '''';
        SET_BLOCK_PROPERTY('NAME_OF_YOUR_BLOCK', DEFAULT_WHERE, v_block_def_where);
        GO_BLOCK('NAME_OF_YOUR_BLOCK');
        EXECUTE_QUERY;
    END;

  • Include dynamic conditions in the where clause

    Hi All,
    I have an requirement where i need to dynamically include the conditions in the where clause based on the bind variable.
    My condition is like this.
    SELECT f.scheduled_date, a.bld_id, a.room_id, a.cage_id, f.test_num
    FROM feeding_test_results f,
    v_onl_gf_anmls a
    WHERE f.sak_feeding_test_res > 5000000
    AND f.anml_id(+) = a.anml_id
    AND f.bld_id = 'A5'
    AND f.room_id > 0
    AND f.cage_id > '0'
    case when :check_value = 1 Then
    AND scheduled_date <= '16-Apr-2009'
    AND scheduled_date >= '06-Apr-2009'
    Else
    AND scheduled_date = '16-Apr-2009'
    End
    AND f.feeding_status <> 'X') f
    Kindly anyone help me how to acheive this.
    Thanks & Regards,
    P. Gayathri Devi

    michaels2 wrote:
    and scheduled_date <= case when :check_value = 1 then date '2009-04-16' end
    and scheduled_date >= case when :check_value = 1 then date '2009-04-06' end
    and scheduled_date = case when not :check_value = 1 then date '2009-04-16' end
    Hmmm, you may want some NVL's around those case statements such as..
           and scheduled_date <= NVL(case when :check_value = 1 then date '2009-04-16' end,scheduled_date)
           and scheduled_date >= NVL(case when :check_value = 1 then date '2009-04-06' end,scheduled_date)
           and scheduled_date = NVL(case when not :check_value = 1 then date '2009-04-16' end,,scheduled_date)or some such thing. Otherwise the comparisons with Null will just cause it all to fail.

  • Adding filter conditions dynamically in WHERE clause -dynamic SQL help

    Here I have a simple condition but very complicated query. Basically, I have to put a filter condition in my where clause. "Location" comes to the stored procedure as parameter. Plus there are other parameters as well.
    If location = "all", I can run the query simply and get the result. But when Location = "CA", which is just a subset of "ALL" then I am having hard time putting one -- AND statement in WHERE clause.
    This query is designed for location = 'ALL'
    open cv for
    'Select col1, col2, col3, Col4
    from t1, t2, t3
    WHERE condition1
    AND condition2
    AND condition3'
    AND location = location_id --- This should only run if location IS NOT ALL
    I have written a dynamic query but it doesn't work for that part. Any ideas will be appreciated. Thanks,

    From what I understood
    If location = 'ALL' then
    fetch all the records
    Else
    add extra filter location_id = <supplied location id>
    End If
    If this is the condition the following logic should solve your issue.
    open cv for
    'Select col1, col2, col3, Col4
    from t1, t2, t3
    WHERE condition1
    AND condition2
    AND condition3'
    AND ((location_id = location_id and location = 'ALL') or (location_id = location))Regards
    Raj

  • AND and OR operations in WHERE clause

    Hello, Dear Oracle professionals.
    In WHERE clause when I use AND or OR operations, is there any way of working ORACLE server to select rows?
    For example
    WHERE con1 and con2 and con3 and con4 and con5OR
    WHERE con1 or con2 or con3 or con4 or con5How oracle checks this conditions ? From the begining to the end ? in order ?
    May be I should put some more probable TRUE condition to the end(or to the begin).
    Whant to know how oracle thinks.
    Thanks in advance.

    Hi,
    Khayyam wrote:
    Hello, Dear Oracle professionals.
    In WHERE clause when I use AND or OR operations, is there any way of working ORACLE server to select rows?
    For example
    WHERE con1 and con2 and con3 and con4 and con5OR
    WHERE con1 or con2 or con3 or con4 or con5How oracle checks this conditions ? From the begining to the end ? in order ?It evalauates the one that is likely to make the most difference first. In the case of AND, that means the condition that is least likely to be true (as far as the optimizer can predict).
    May be I should put some more probable TRUE condition to the end(or to the begin).It doesn't matter to the optimizer. Do whatever you find easier to read and debug.
    In ancient times, using the rule-based optimizer, order did matter, but there's no reason to be using the rule-based optimizer now. Oracle 11 doesn't even have the option.
    By the way, be careful not to mix AND and OR without parentheses. That is, never say:
    WHERE  x AND y OR z    -- ***** No!  Wrong!  ******Instead say
    WHERE  (x AND y) OR z   or
    WHERE  x AND (y OR z)   depending on what you want. If you don't use partentheses, then there are rules about how the expresssion is evaluated, but it's a waste of your time to learn them.

  • IN operator in Where Clause

    Hi Experts,
    I have been facing a problem in passing multiple values to the where clause in select query. I have 15 values in which i need to check in where condition.
    Ex:  SELECT AMATNR ASPART
               INTO TABLE IT_DIVISION
               FROM MARA AS A
               INNER JOIN MARC AS B ON
               AMATNR = BMATNR
               FOR ALL ENTRIES IN IT_UPDATE
               WHERE A~MATNR EQ IT_UPDATE-MATNR
               AND A~MTART IN ('ZLHA', 'ZFER', 'ZHAL', 'ZNLA', 'ZOPS',
                               'ZPSE', 'ZROH', 'ZUNB', 'ZVER ', 'ZDIA').
    Now i need to include some more (5) values, but its showing some error.
    Please guide me in this.
    Thnx in Adv..Meher

    Hi,
    Make use of a range table.
    types : begin of T_range,
    sign type TVARV_SIGN,
    option type TVARV_OPTI,
    low type mtart,
    high type mtart,
    end of t_range.
    data: IT_RANGE type standard table of T_range.
    data : wa_range type T_RANGe.
    wa_range-sign = 'I'.
    wa_range-option = 'EQ'.
    wa_range-low = 'ZLHA'
    append wa_range to IT_RANGE.
    wa_range-sign = 'I'.
    wa_range-option = 'EQ'.
    wa_range-low = 'ZFER'
    append wa_range to IT_RANGE.
    wa_range-sign = 'I'.
    wa_range-option = 'EQ'.
    wa_range-low = 'ZHAL'
    append wa_range to IT_RANGE.
    and so on till all your values are filled in the table and then use this table in the IN clause.
    SELECT AMATNR ASPART
    INTO TABLE IT_DIVISION
    FROM MARA AS A
    INNER JOIN MARC AS B ON
    AMATNR = BMATNR
    FOR ALL ENTRIES IN IT_UPDATE
    WHERE A~MATNR EQ IT_UPDATE-MATNR
    AND A~MTART IN it_range.
    The error which you are getting is because of the spacing between the different MTART values which you have included in the bracket.
    After every comma there should be a space.
    But you should always use range for such requirements.
    regards,
    Ankur Parab
    Edited by: Ankur Parab on Jun 23, 2009 6:59 PM

  • Invalid relational operator in where clause dynamic

    I have a procedure where I pass the WHERE CLAUSE as a parameter like this:
    PROCEDURE PRC_CONSULTAR_AFIL_PEND(PV_WHERE IN VARCHAR2,
    RESULTSETM IN OUT SYS_REFCURSOR)
    IS
    BEGIN
    OPEN RESULTSETM FOR
    'SELECT PAISAFIL,
    TIPDOCAFIL,
    NUMDOCAFIL,
    APPAFIL,
    APMAFIL,
    NOMAFIL,
    SEXAFIL,
    FECNACAFIL,
    CODPNA,
    ESTADO,
    FECHA,
    TO_DATE(FECGEN),
    NOMCOLUMN
    FROM SUNAFILERR
    WHERE ' || PV_WHERE;
    I am passing the where clause as: 'APPAFIL'||' '||'APMAFIL'||' '||'NOMAFIL LIKE UPPER(rojas%)'

    Miguel Angel wrote:
    I have a procedure where I pass the WHERE CLAUSE as a parameter like this:
    PROCEDURE PRC_CONSULTAR_AFIL_PEND(PV_WHERE IN VARCHAR2,
    RESULTSETM IN OUT SYS_REFCURSOR)
    IS
    BEGIN
    OPEN RESULTSETM FOR
    'SELECT PAISAFIL,
    TIPDOCAFIL,
    NUMDOCAFIL,
    APPAFIL,
    APMAFIL,
    NOMAFIL,
    SEXAFIL,
    FECNACAFIL,
    CODPNA,
    ESTADO,
    FECHA,
    TO_DATE(FECGEN),
    NOMCOLUMN
    FROM SUNAFILERR
    WHERE ' || PV_WHERE;
    I am passing the where clause as: 'APPAFIL'||' '||'APMAFIL'||' '||'NOMAFIL LIKE UPPER(rojas%)'So your where clause is
    WHERE APPAFIL APMAFIL NOMAFIL LIKE UPPER (rojas%)Can you see why that's an error? It would make more sense if there was an operator between APPAFIL and APMAFIL, but then there's obviously something missing between APMAFIL and NOMAFIL as well, and some quotes missing later on.
    The following is valid SQL code
    WHERE   APPAFIL  != APMAFIL
    AND     NOMAFIL  LIKE UPPER ('rojas%')Of course, I have no idea if that's what you want or not.
    Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements) for any tables used, and the results you want from that data.
    if the problem involves parameters (such as the pv_where), then post a couple of different parameters, and the results you want from the same sample data for each parameter.
    Explain how you get those results from that parameter and that data.
    Always say which version of Oracle you're using.

  • How to use string operation in where clause of select query

    Hello All,
    I just want to know how can i write a restriction in select query saying retrive data only begins with name "DE*".
    Explaination: If my table has records and names starts with character then i want to write a query to fetch all the records in which names starts with DE*.
    Thanks in advance for your quick reply...
    Dev.

    Hi
    In the where clause you need to write like
    WHERE NAME LIKE 'DE%'
    Regards
    Sudheer

  • How to use multiple search conditions in the where clause

    Hi,
    Below is my query
    /****** Script for SelectTopNRows command from SSMS  ******/
    SELECT distinct 
    ctacct,sum(GLMN02)
      FROM [ODS].[Staging].[tODS_INF_GLPCT]
      inner join 
      [ODS].[Staging].[tODS_INF_GLPGL] ON tODS_INF_GLPCT.CTPAGE = tODS_INF_GLPGL.GLPAGE
      where 
      CTACCT like '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4110%'
    This one gives me the exact result i want, now if i add one more to my where clause like this
    /****** Script for SelectTopNRows command from SSMS  ******/
    SELECT distinct 
    ctacct,sum(GLMN02)
    --,ctdesc,CTPAGE
    --SUM(GLMN02)
      FROM [ODS].[Staging].[tODS_INF_GLPCT]
      inner join 
      [ODS].[Staging].[tODS_INF_GLPGL] ON tODS_INF_GLPCT.CTPAGE = tODS_INF_GLPGL.GLPAGE
      where 
      CTACCT like'[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4110%' or
      CTACCT like '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4115%'
    This query doesnt give me the exact values instead it gives me all the weird values.Can someone please help me with how to work on this where clause?
    Thanks

    Hi Patrick,
    This is what i ve tried earlier and it isnt working .If i use the first query
    /****** Script for SelectTopNRows command from SSMS  ******/
    SELECT distinct 
    ctdesc,SUM(GLMN02)
    --,ctdesc,CTPAGE
    --SUM(GLMN02)
      FROM [ODS].[Staging].[tODS_INF_GLPCT]
      inner join 
      [ODS].[Staging].[tODS_INF_GLPGL] ON tODS_INF_GLPCT.CTPAGE = tODS_INF_GLPGL.GLPAGE
      where 
     CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4110%'
       --OR
       --CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4115%'
      and GLYEAR = 2014
      and CTDESC = 'Sales'
      group by ctdesc
    The result set is 
    Sales                        
    -182273.96
    And if i use the second query
    SELECT distinct 
    ctdesc,SUM(GLMN02)
    --,ctdesc,CTPAGE
    --SUM(GLMN02)
      FROM [ODS].[Staging].[tODS_INF_GLPCT]
      inner join 
      [ODS].[Staging].[tODS_INF_GLPGL] ON tODS_INF_GLPCT.CTPAGE = tODS_INF_GLPGL.GLPAGE
      where 
     CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4110%'
     OR
       CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4115%'
      and GLYEAR = 2014
      and CTDESC = 'Sales'
      group by ctdesc
    The result set i get is 
    Sales                        
    -1455441.08
    And i verified that  CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4115%'
    this second line of where clause has not desc as Sales.
    I am not sure whats causing the difference in the values.
    Can you please help me with this?
    Thanks

  • How do I use the IF-ELSE condition in a WHERE clause?

    I want to merge the condition results of my 2 If statements. First if statement result shows only both 0 result, in second if statement result one of them has to be bigger than 0... 
         If (Inventory) <> 0 Then
            If Apple = "" And Banana = "" Then
         strSQL = strSQL & " AND (myApple = 0 AND myBanana = 0)"
      End If
      End If
         If int(Inventory) <> -1 Then
      If Apple = "" And Banana = "" Then
         strSQL = strSQL & " AND (myApple <> 0 OR myBanana <> 0)"
      End If
      End If
    **Want to get something like this**
         If int(Inventory) <> 0 Then
                If Apple = "" And Banana = "" Then
           strSQL = strSQL & " AND ((myApple = 0 AND myBanana = 0) AND (myApple <> 0 AND myBanana <> 0))"
        End If
        End If
        If int(Inventory) <> -1 Then
        If Apple = "" And Banana = "" Then
           strSQL = strSQL & " AND (myApple <> 0 AND myBanana <> 0)"
        End If
        End If
       myApple myBanana
             0       0
             0       0
             0       5
             1       0
             6       0
             0       0
          continue.....

    Maybe
    with
    fruits as
    (select null apple,null banana from dual union all
    select 'Golden Delicious','Chiquita' from dual union all
    select 'Granny Smith',null from dual union all
    select null,'Cavendish' from dual
    select apple,banana,nvl2(apple,1,0) * nvl2(banana,1,0)
      from fruits
    APPLE
    BANANA
    NVL2(APPLE,1,0)*NVL2(BANANA,1,0)
    0
    Golden Delicious
    Chiquita
    1
    Granny Smith
    0
    Cavendish
    0
    Regards
    Etbin

  • How to change operator of join conditions in where clause?

    Hello
    I have a situation... I want to change the operator between each join conditions in the where clause when these join conditions are not from the same join..
    For example, I have the following schema:
    Dim1 ------ DimA -------Fact1
    Dim1-------DimB -----Fact1
    So DimA and DimB are aliasas of one dim table, but the join is different.
    Now if I run this model, what I will get in the where clause of the query is:
    Where Dim1 = DimA and Dim1 = DimB and DimA= Fact1 and DimB = fact1.
    Is there a way I can change these "and" operator to "OR", so that the where clause would look like this: Where Dim1 = DimA and Dim1 = DimB and DimA= Fact1 OR DimB = fact1?
    This is different from simply changing the join operator within the same join, because these are different joins and I'd like to control how they relate to each other..
    Please help
    Thanks

    Sometimes, business rules are complex, so there isn't always a way to simplify things.  Is your issue that it's complex and error prone, or is it performance due to the OR clauses?
    One possibility that will at least make it easier to test and debug is something like this:  (pseudocode)
    From Table1 Inner join Table2 on x=y etc.etc.
    CROSS APPLY
    (Select case when a=b and (c=d or e=f) then 1 else 0 end) as Situation1
    , case when h=i or j = k then 1 else 0 end) as situation2
    , case when l = m then 1 else 0 end) as situation 3
    ) as CA_Logic_Simplifier
    Where situation1 = 1 and situation2 = 1 and situation3 = 1
    Although you could say, "Hey, this is basically doing the same thing as before", this approach would be far easier to test and debug, because you can at a glance look at the values for situation1, 2, 3, etc. to see where errors are being introduced. 
    The cross apply makes the columns situation1/2/3 "instantiated", so they are usable in the where clause. Divide and conquer.  

  • Condition 1 = 1 in a Where Clause

    What purpose the following condition in a where clause generated by Oracle Discoverer serve? Just Curious !
    And 1 = 1;

    This is a very handy abd cool and slick thing to do in SQL. It allows for the SQL to run. Normally you see this as follows ...
    SELECT/INSERT/UPDATE/DELETE ...
    FROM TABLES ...
    WHERE (all normal conditions)
    AND
    --[ABORT]
    --[TRUE]
    --1=0
    --[FALSE]
    1=1;
    If you switch the 1=1 to 1=0 then the SQL will not run. It's a handy way for a programmer to turn on/off sections of code. Another handy method I use goes a little further ...
    SELECT/INSERT/UPDATE/DELETE ...
    FROM TABLES ...
    WHERE (all normal conditions)
    AND
    --[ABORT]
    1=(select abort_flag from t_abort);
    So I have a master abort flag that I can switch from 1 to 0 as a process is running. That way my process can still continue but I turn off what ever sections I don't want to run if I set the abort_flag to 0.
    Should I be in a multi user environment I usually have a view on the abort table so it only returns 1 record for that particular SID/USER that is running the process (so I don't abort other people's code).
    Tyler D.

  • In which order does the query's where clause condition is checked

    Hi All,
    can anyone tell me in which order does oracle evaluate the where clause condition?
    for ex: select * from users where user_type='Admin' and stauts='Enabled'

    user9981103 wrote:
    The conditions in the WHERE clause are checked in the reverse order of the given order.
    i.e; first it will check stauts='Enabled'* and then user_type='Admin'* for your given queryWhy do you believe that?
    If there is a sufficiently selective b-tree index on USER_TYPE, the optimizer would undoubtedly use that index to process the USER_TYPE = 'Admin' condition and then check the STATUS column.
    If there is a sufficiently selective b-tree index on STATUS, the optimizer would undoubtedly use that index to process the STATUS='Enabled' condition and then check the USER_TYPE column.
    If there is a composite b-tree index on (STATUS, USER_TYPE), the optimizer would use that index to process the two conditions nearly simultaneously, though technically the leading column of the index is evaluated first. Of course, if you reverse the order of columns in the index, you get a different evaluation order
    If there are bitmap indexes on STATUS and USER_TYPE, Oracle can combine the two indexes and evaluate the two conditions simultaneously.
    And that is leaving out tons of query plan options and other optimizer wrinkles.
    Justin

  • How to change access path for 'where' clause by using HINTS?

    I searched a loooot of posts and haven't found a solution for my case. I don't even know whether it is possible or not. Is it possible to change the sequence of Oracle "Predicate Information"?
    Here is my SQL and Oracle's execution plan.
      SELECT Max(logId) AS logId FROM online_users_t
      WHERE online_users_date >= to_date('2011-09-19 10:00:00') - 3.2 AND online_users_date <= to_date('2011-09-19 10:00:00') AND online_users_result in (1, -1)
      GROUP BY online_users_user
    | Id  | Operation                    | Name               | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT             |                    | 24800 |   629K|  1336   (1)| 00:00:17 |
    |   1 |  HASH GROUP BY               |                    | 24800 |   629K|  1336   (1)| 00:00:17 |
    |*  2 |   TABLE ACCESS BY INDEX ROWID| ONLINE_USERS_T     | 38833 |   985K|  1334   (1)| 00:00:17 |
    |*  3 |    INDEX RANGE SCAN          | ONLINE_USERS_T_IDX |   116K|       |   313   (1)| 00:00:04 |
    Predicate Information (identified by operation id):
       2 - filter("ONLINE_USERS_RESULT"=(-1) OR "ONLINE_USERS_RESULT"=1)
       3 - access("ONLINE_USERS_DATE">=TO_DATE(' 2011-09-16 05:12:00', 'syyyy-mm-dd
                  hh24:mi:ss') AND "ONLINE_USERS_DATE"<=TO_DATE(' 2011-09-19 10:00:00', 'syyyy-mm-dd
                  hh24:mi:ss'))I have 2 conditions in my 'where' clause, one is date range and the other is 'online_users_result in (1, -1)'. It seems that Oracle filter the table by using 'online_users_result in (1, -1)' first, then access it through date range.
    What I want to do is firstly filtering the table by using date range followed by other things. How can I do it?
    Any clue or help would be highly appreciated.
    Thanks in advance.

    It seems that Oracle filter the table by using 'online_users_result in (1, -1)' first, then access it through date range. No it's not.
    What I want to do is firstly filtering the table by using date range followed by other things. How can I do it?That's precisely what it's doing now.
    It is using the T_IDX index to quickly find all rows that satisfy the range predicate on the date column.
    And then filter those rows to only retrieve the ones that satisfy the other predicate (... in (1,-1)).

Maybe you are looking for