SQL: order of WHERE clauses important for performance?

I wonder if the order of the WHERE clauses does affect performance or if the database optimize each query so that the order is irrelevant? Example: is
SELECT *
FROM Table1
WHERE (fast condition check)
AND (slow condition check)
faster than
SELECT *
FROM Table1
WHERE (slow condition check)
AND (fast condition check)
because the first condition check might be false and therefore the second is not executed? (Some kind of Java if (fast && slow) is faster than if (slow && fast) ?)

It depends on how sophisticated the database optimizer is. IBM's DB2/UDB will completely rewrite the syntax of SQL as part of optimization and does not care what the order of the where clause is. Oracle actually has two optimizers RULE and COST. The RULE based optimizer is affected by the order of the where clause, COST is not (RULE is no longer available in the newest version of Oracle, Oracle 10g).

Similar Messages

  • Sql order in where clause...

    i want to ask about
    how oracle deal with the order of where cluase
    as in select * from where tname = 'EMP'
    and tabtype = 'TABLE'
    oracle strat from down (tabtype) or from up (tname)
    and there is difference between this order
    if the where clause contains (and) or (or)
    i always need to know this thing but no place i found say about it in strait way..
    thanks..
    null

    I am sorry for the late reply.
    I guess that ORDER doesn't matter in the WHERE clause. We should take 2 cases:
    1) Say, in a WHERE clause, U have a limiting condition on one of the indexed field of the table, then definitely that will be executed first and the result set will be passed to the other conditions.
    2) Say, in the WHERE clause, U don't have any indexed field then ORACLE will go for cost based execution. In this case, it will first go to the column, for which the SELECTIVITY is the highest ( ORACLE does so internally ) and then the result set is passed to the other conditions.
    Hope this helps.
    Correct me, if I am wrong.

  • Can a recursive sql have a where clause

    can the following sql have a where clause and I dont want to hard code the id is is that possible...
    select DISPLAY_NAME, ID, PARENT_ID, display_in_parent_folder, sub_folder_sequence
    from wwsbr_all_folders start with id = 8888
    connect by prior id= parent_

    actually g is another alias I created sorry I missed it... if I do this as a regular sub query I get three records but when I implement it as a recursive query it does not retutn any thing. Below is my regular query followed by the recursive query which I am using to organize the result but get no results:
    REGULAR Query that retrieves records with parent_id = from the list of parent ids
    select distinct f.DISPLAY_NAME, f.id, f.type_id, f.type_siteid, f.base_type_id, f.parent_id, f.sub_folder_sequence
    from WWSBR_ALL_FOLDERS f, WWSBR_ALL_FOLDERS g
         where g.parent_id = ( select g.parent_id from WWSBR_ALL_FOLDERS e
    where
         e.caid = 103 AND
    e.parent_id <> 0 AND
    e.type_id = 1 AND
    e.display_in_parent_folder <> 0 AND
         e.id = f.parent_id)
    order by f.id, f.sub_folder_sequence;
    Recursive query that results nothing:
    select distinct f.DISPLAY_NAME, f.id, f.type_id, f.type_siteid, f.base_type_id, f.parent_id, f.sub_folder_sequence
    from WWSBR_ALL_FOLDERS g, WWSBR_ALL_FOLDERS f start with f.id = ( select g.parent_id from WWSBR_ALL_FOLDERS e
    where
         e.caid = 103 AND
    e.parent_id <> 0 AND
    e.type_id = 1 AND
    e.display_in_parent_folder <> 0 AND
         e.id = f.parent_id)
         connect by prior f.id= g.parent_id
    order by f.id, f.sub_folder_sequence;

  • Offset in where clause of for all entries

    Hi ,
    I need to fetch VBELN from VBKD table based on Purchase order no which is fetched from a Z-table.So I am using for all entries with where condition as VBKD-BSTKD_M = Ztable-PONumber  and fetching values from VBKD .But at times PO number in VBKD table can have additional things attached to it .
    Eg Ztable-PO number = 12345678 , VBKD-BSTKD_M = 12345678-001 so I need to ignore the '-001' part and still fetch data from VBKD.So how can I do that.
    Pls let me know if you have any ideas.
    Thanks in advance,
    Rajasekaran

    Hi ,
    We cannot use offset directly in where as it will result in syntax error as the field length for both fields have to match .In my case the length of both Ztable -PO number field and vbkd-bstkd_m are 35 characters.So cannot use offset directly in where clause of for all entries.
    Pls let me know if you have any other ideas.
    Thanks in advance,
    Rajsekaran

  • Order in where clause - SQL statement

    Hi,
    The order of the fields in Where clause in OpenSQL statements is important to get the right index?
    Select a b c from t1
    where 
    d = p_d and
    e = p_e
    Or
    Select a b c from t1
    where 
    e = p_e and
    d = p_d
    Index:
    columns e and d.
    Thanks !

    HI,
      Both will give you the same result.. but it is always good to pass the sequence as in the table.. the performance will be good when you follow the sequece of occurance of the fields.
    Thanks
    Mahesh

  • Order of Where clause

    Hello GURUs
    I have doubt about following. Please can all help me.
    1. Does the order of table in from clause make difference. what should be the order. . largest to snmallest.
    2. Does where clause order make difference. also left side to right side of equality comaprision. table row column on left and table column with less row on right.?
    3. in USE_NL order of table make difference or not.? which one should be first one with less row or one with more rows. ?
    4. Somtimes i have seen that USE_NL(table name) with only one table. in what condition that will be used. Larger or the smaller and then will the where clause make difference on that with USE_NL HINT.?
    Message was edited by:
    devmiral

    Ok, because you insist :-)
    I think you shouldn't care that much about these subjects, really. Oracle has an excellent cost based optimizer (cbo) that does all the work for you. It decides which predicate to apply first, the order of the tables and much more. But, and this is an important but, the cbo must have recent statistics of all the objects involved in your queries. So make sure you analyze regularly or use monitoring to stay up to date.
    Applying hints like USE_NL is like saying: "I know way better than the cost based optimizer how to get the results quicker, so you should always use this access path".
    Most of the time, however, something else is wrong, and by applying some hints you may (temporarily) fix the symptoms of your problem, but you are not addressing the root cause. So in case you feel the need to apply such a hint, please don't and start investigating what is really causing your problems.
    Hope this helps.
    Regards,
    Rob.

  • SQL Strings in Where Clause - single quotes issue

    All,
    I’m having issues related to SQL String literals when trying to deploy to flash. The complication works fine but I then get a message (see below) stating that a ; is missing. The SQL query and the iview runs fine when we use numeric values or do not apply a where clause on the table.
    I am using Visual Composer that has been packaged with NW2004sSP7_Preview
    VC & Flex Version: 645.7.0.3
    The Error message is get is;
    Error in executing a process for Flex compilation, Error 1033: ';' expected
          (C:usrsapJ2EJC01j2eeclusterserver0GUIMachine_Business_Packagestest_48731FLEX_COMPILATION_FOLEDRAADCN.mxml:269)
    Error 1205: The statement 'Test' is incomplete.
          (C:usrsapJ2EJC01j2eeclusterserver0GUIMachine_Business_Packagestest_48731FLEX_COMPILATION_FOLEDRAADCN.mxml:269)
    Failed to compile AADCN.mxml
    When I goto the deployment file – it has the below line;
    <i>'<Request type="EXECUTE_RELATIONAL" system="BI_JDBC" system_type="SAP_BI_JDBC" maxrows="500" templateid="BIR_SQL"><Objects type="INPUT" shape="OBJ" role="INPUT"><Object type="INPUT_FIELD" id="SQL_STATEMENT" appName="SQL" mapped="0" value=""/></Objects><Objects type="OUTPUT" shape="SET" role="OUTPUT"><Object type="OUTPUT_FIELD" id="name" appName="name"/></Objects><Objects id="1" type="TEMPLATE_PARAMETER"><Object id="2" type="SQL" value="select name from pub.srcompany where name ='Test Company'"/></Objects></Request>';</i>
    It seems that the parser for the flash deployment is prematurely finishing the parse when it hits the single quotation in the SQL string!
    We had a similar error where if we had a carriage return in the SQL (e.g. between the from & the where clause) then the deployment parser was stating that the line finished prematurely. In the deployment file the carriage return forced a new line thus making the message incomplete. Removing the carriage return resolve that issue
    The SQL Preview in the SQL Editor within VC works fine with the string literals in the where clause.
    The functionality compiles & deploys in web dynpro however it does not return the results to the table
    Questions
    1:> Has anyone successfully used flash with string literals in the SQL where clause or had seen this issue in the past?
    2:> Is there a setting to get the SQL working on Web dynpro for the information to be returned that I may have missed?
    Any assistance would be greatly appreciated.
    Best Regards,
    Ian.

    Hey,
    I have worked with SQL Editor a lot. Here's a how to guide I put together on it:
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/6339e7d4-0a01-0010-1c98-db00e52e989a
    Let me know if that helps...
    Prakash

  • Change JDBC select SQL query's where clause at runtime

    Hi,
    I have one JDBC sender channel with Select SQL Query which helps in fetching data from JDE table. We have defined where clause to fetch some particular records i.e. if ABC = NULL then fetch the record.. and then update query is used to turn that flag to "Y" i.e. ABC ="Y" so that next time the channel don't pick the same records again.
    Now the problem is that if we want to fetch some records again whose flag has been set to "Y" we cannot change the select query again and again in the production environment.
    Is there any way to change the select query of the JDBC channel at runtime e.g. Picking up some flat file where the conditions for the particular records to be picked are defined (ID=123) and changing the WHERE clause of select query in the JDBC channel for those records to be picked without using BPM.
    Thank you!
    With regards,
    Simran

    Hi Simran,
    Yes, it is possible with the help of store procedure. In SQL Server there is a IF-Else Conditional block, which you can use inside store procedure. Create a SP with 2 additional parameter something like FlagReadAgain and KeyField. Pass the flag always as True or False. For the Key field pass the record key which you want to read again if you are passing the Flag as True else pass it as 0 or whatever.
    In the procedure check if the value of flag is True -> If yes, Read the entry with key name specified in where part.
    In the else part put the query which you are using for the normal flow.
    IF (FlagReadAgain) = TRUE
    BEGIN
       SELECT EmpName, EmpAddress
       FROM Employee
       WHERE EmpID = 'E001'
    END
    ELSE
       BEGIN
         SELECT EmpName, EmpAddress
         FROM Employee
         WHERE Status = 'N'
       END
    You may also use the approach which Srini mentioned. Create an application to restore any value to intial stage so that it can be picked up by XI when it poll again.
    Choose any approach which you like.
    Regards,
    Jitender Chauhan

  • Simple finder results in huge slow sql query -- why WHERE clause?

    Hi all,
    wondering about the poor performance of my finder methods I took a look into the SQL that went to the database.
    "SELECT DISTINCT OBJECT(p) FROM Person p"
    results in
    SELECT PersonID, FirstName, LastName, [etc.] FROM person WHERE (PersonID='5354B71CC0A80113008BD3BB11A57FA1') OR (PersonID='5354B893C0A80113008BD3BB3C6918FF') OR (PersonID='5354B910C0A80113008BD3BBC83093BE') OR [etc.]
    where all 2000 primary keys are listed. This query costs ~ 4 seconds on my box.
    A query without WHERE costs ~ 0 seconds. So why this "WHERE" clause?
    Is this a JBoss specific problem? I am using version 3.2.3.
    thanx!
    Marcus

    It's not a bug - it's how JBoss implements it's own
    "findAll" methodology. Whether it's a good
    implementation or not is open to debate.Really strange.
    Anyway all my performance problems disappeared with setting the read-ahead strategy to "on-find". Although I don't fully understand why.
    Marcus

  • Howto use jsp with where-clause restriction for viewObject

    hello there,
    i am currently trying to figure out how to pass parameters via url to the jsp,
    so that the corresponding viewobjects where clause can be extended and i only get the select-results that i want. i am familiar with doing this in a stright java-environment but don't know how to implement it in a jsp-client.
    does anyone know something about this or can name me a ressource i can read about that.
    Thanx
    Selim Keser

    thank you, but i have only 19 days left for
    my diploma and nobody in this company can help me with this issue.
    what i need is an example or something like a reciept about what to do
    to achieve the following:
    I want to type in an url like:
    localhost:8080/myapp/browse.jsp?customerid=123
    so that i can extend the where-clause in my ViewObject
    and only see the rows of customer 123 in my jsp.
    it is a bit hard for a newbie to figure out how to pass down the parameters way down to the ViewObject. I am shure that this is easy to do once one understands the how it works. This is my first "Web-Experience" and I am running out of time.
    Thanx
    Selim Keser

  • Plz guide - ordering in where clause

    This is in general ..
    when we write query we will have 2 or more tables in from clause
    what should be first in order to get optimum performance.
    1. Joins of tables
    2. filtering clauses eg. table.abc = 20
    Plz help.
    Thanx in advance.

    in my own i have the practice of ordering it by according to the first declaration of tables/views in the FROM clause. e.g.
      Select order.order_num,
             customer.customer_name,
             items.item_description,
             orders.quantity
        From orders,
             customers,
             items
       Where order.customer_id         = customers.customer_id
         and customers.customer_active = 'Y'
         and order.item_id             = items.item_id
         and items.item_active         = 'Y'

  • Can we use where clause in for-each: tag_name ?- urgent

    Hi All,
    I am using templated builder 5.5
    1)2 G_LINE Repeating Groups will exist for every Invoice Line. Each will have an INVOICE_LINE_TYPE element, set as either ‘Line’ or ‘Tax’. Both of the G_LINE groups will be used to create the Invoice Lines output for a consolidated invoice.
    2)<?for-each:G_LINES WHERE ORDER_LINE_TYPE = ‘LINE’?>
    Data to be displayed...
    <?end FOR-EACH?>
    Repeating group to display all of the invoice lines associated with a particular consolidated invoice.
    The XML Element to base repeating group on is G_LINES
    Is it possible to use the above tags, if yes how an d if no, will this be suppoerted in 5.6.1 version?
    Thanks in advance

    Hi
    You'll have to add the code but you can use XPATH to limit the data to the LINE data:
    <?for-each:G_LINES[ORDER_LINE_TYPE = ‘LINE’]?>
    this limits the rows to just those that satisfy the condition.
    If you want to show the LINE and TAX data you would be better to loop over the G_LINES and then use an 'if' statement across the group. So you would have a two row table to hold the LINE and TAX data and then use a if@row statement to filter on LINE and TAX data.
    Regards, Tim

  • SQL Plus Skipping Where Clause

    Question: We are using .sql files, that are executed though SQLPlus to implement our production code, we have had instances where a developer put a extra line in the middle of his SQL statement. In the below examples, the 2nd script will execute and delete everything in the entire table, then give a error message message stating that it doesn't know what 'WHERE' is, and then commits(Only happens when executing from a .sql file, if you put the below SQL in the buffer and execute it... it doesn't happen)... does anyone know a way to tell SQLPlus to ignore carrage returns as the end of the SQL statement?
    Example 1: Delete 1 Row
    DELETE FROM TABLE1
    WHERE ID = 1;
    COMMIT;
    Example 2: Delete's ALL Rows
    DELETE FROM TABLE1
    WHERE ID = 1;
    COMMIT;

    set sqlblankline on
    (this default value is off)
    and addtional...
    set sqlterminator ;
    (this default value is ;)
    Message was edited by:
    ushitaki

  • Is  order in which the conditions given in the 'WHERE' clause are imortant

    Hi All,
    Is the order in which the conditions are given in the 'WHERE' clause are important for
    performance with regard to oracle query plan.
    Regards

    Short answer: no.
    Longer answer: Well. probably not. If you are using very old versions of the database or the rule-based optimizer, you can probably find some corner cases where it mattered. If you are using a vaguely recent version of the database with the cost based optimizer, it shouldn't matter-- the optimizer is free to evaluate the conditions in whatever order it would like and it chooses to do so in the order that it expects to be most efficient. That said, as the number of objects and conditions increases, the number of possible permutations the CBO tries to consider increase dramatically to the point that an exhaustive analysis of all possible conditions isn't feasible. In that case, the optimizer will have to make a reasonable effort to explore the set of possible plans rather than doing an exhaustive search-- the order of conditions is likely to matter slightly in the optimizer's choice of which branches to explore and which to prune if there are many conditions with similar (or identical) costs. Now, that being said, it is very unlikely that you could do anything useful with this knowledge that would be able to force the optimizer to do something that you'd want. It's possible to do, of course, it's just not a particularly useful way of influencing the optimizer.
    Justin

  • Pl/sql select distinct in a where clause

    Greedings,
    I am trying to execute some kind of distinct using rowid in where clause but for some reason im not getting the appropriate result. Any guidance is welcome
    SELECT acnt_code,cat,cat_desc,buc FROM so_budgets_cat where
    Trim(buc)='S03'
    and
    NOT exists (SELECT  1
                       from   so_budgets_cat tab2
                       where  (  tab2.cat     =  so_budgets_cat.cat )
                       and    tab2.ROWID  > so_budgets_cat.ROWID
    ORDER BY cat,so_budgets_cat.rowidThanks in advance

    Can you provide some example data and example output to further clarify?
    It might be that you want something like the code below, but it's hard to tell.
    SELECT acnt_code
    ,      cat
    ,      cat_desc
    ,      buc
    FROM
    (SELECT acnt_code
    ,      cat
    ,      cat_desc
    ,      buc
    ,      ROW_NUMBER() OVER (PARTITION BY cat ORDER BY <something>) rn
    FROM   so_budgets_cat
    WHERE  Trim(buc)='S03')
    WHERE rn = 1;

Maybe you are looking for

  • Can't get past "Formatting"

    Just bought a new 20gb iPod Colour and was all excited to get started...I'm not too familiar with the ins and outs of computers other than the basics and so didn't realize my laptop only had USB1.1...after reading all the posts I have decided to get

  • SQL Loader CLOBs

    I am moving data from an oracle 10g table to another oracle 10g table, transforming the data in Access, filtering records, merging fields, etc.., then via Access VBA creating CTL files and neccessary scripts, then loading the data via SQL Loader. Get

  • Poor audio/video synching when saving LR4 slideshow

    In spite of its relatively limited capabilities I put quite a bit of effort into creating slideshows, e.g. sequence of images, duration, and some other nice touches in order to do the best I can. In a recent slideshow I was able to re-order the seque

  • Firefox 3.6.12 crashes or does not open as soon as firebug is installed, why?

    I am using firefox for last 5-6 years. But now these days I am fed up with its performance. I installed ff 3.6.12 and add-on firebug latest(1.5). When I restart FF, it shows up in process tabs but does not open. I disable it from process tab and cont

  • Stop/start controls in flv sections

    Hi there, I have a small video program I need help with.  I am using a single FLV file. On the player interface in addition to play/pause and stop, I have a number of chapter buttons, which I have successfully pointed to the corresponding points in t