|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, November 11, 2010 10:37 PM
Points: 13,
Visits: 43
|
|
Witch query is more efficient?? Table Structure: CREATE TABLE [dbo].[tbl_test]( [ID] [int] NOT NULL, [Title] [varchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_tbl_test] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) Query1: SELECT ID, Title FROM tbl_test WHERE (ID = 1) OR (Title LIKE '%abc%') Query2: SELECT ID, Title FROM tbl_test WHERE (Title LIKE '%abc%') OR (ID = 1)
I just want to know Whether or not changing the order of columns in where clause can effect performance(using OR in where)?
comparing ID field is faster than the like statement on Title column. so if the first statement in OR is true next statement should not be checked.. I think Query1 is faster as Like statement on title will not always be checked. and in query2 Like statement on title will always be checked... What u say ??
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, February 18, 2013 3:46 AM
Points: 1,422,
Visits: 1,883
|
|
I don't recommend relying on short-circuiting by the query optimizer - i.e. assuming that an "OR" or an "AND" condition is always going to be evaluated left to right. The order of evaluation is up to the query optimizer which in turn depends on the query, indexes, data distribution, choice of query plan etc.
You can try to use a CASE statement to see if that allows for a short-circuit. The rules for the CASE statement evaluation are documented as follows:
Simple CASE expression: The simple CASE expression operates by comparing the first expression to the expression in each WHEN clause for equivalency. If these expressions are equivalent, the expression in the THEN clause will be returned. Allows only an equality check. Evaluates input_expression, and then in the order specified, evaluates input_expression = when_expression for each WHEN clause. Returns the result_expression of the first input_expression = when_expression that evaluates to TRUE. If no input_expression = when_expression evaluates to TRUE, the SQL Server Database Engine returns the else_result_expression if an ELSE clause is specified, or a NULL value if no ELSE clause is specified.
Searched CASE expression: Evaluates, in the order specified, Boolean_expression for each WHEN clause. Returns result_expression of the first Boolean_expression that evaluates to TRUE. If no Boolean_expression evaluates to TRUE, the Database Engine returns the else_result_expression if an ELSE clause is specified, or a NULL value if no ELSE clause is specified.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 37,635,
Visits: 29,886
|
|
ahmed.net (10/9/2010)
Whether or not changing the order of columns in where clause can effect performance(using OR in where)?
Nope.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 6:33 PM
Points: 32,889,
Visits: 26,757
|
|
ahmed.net (10/9/2010) Witch query is more efficient?? Table Structure: CREATE TABLE [dbo].[tbl_test]( [ID] [int] NOT NULL, [Title] [varchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_tbl_test] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) Query1: SELECT ID, Title FROM tbl_test WHERE (ID = 1) OR (Title LIKE '%abc%') Query2: SELECT ID, Title FROM tbl_test WHERE (Title LIKE '%abc%') OR (ID = 1)
I just want to know Whether or not changing the order of columns in where clause can effect performance(using OR in where)?
comparing ID field is faster than the like statement on Title column. so if the first statement in OR is true next statement should not be checked.. I think Query1 is faster as Like statement on title will not always be checked. and in query2 Like statement on title will always be checked... What u say ??
What I say is... trust no one on such a thing. Not even the "pros" even though they're mostly right . Instead, do a test with a million rows and find out for yourself. Many good things will come from that...
1. You'll know the truth for sure because you will have seen it with your own eyes. 2. You'll remember the correct answer longer because you will have seen it with your own eyes. 3. You'll know how to build a million row test table.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 3:36 AM
Points: 19,
Visits: 78
|
|
Hi
Though I'm no expert my initial thought is, that they might not be any difference in the two selects when using an OR where, because SQL would have to check the ID field to see if it's a 1, and no matter if it is or not it would still need to check the title field for the like condition.
Now if it was an AND clause then maybe it might make a difference, especially if you have indexed the ID column but not the title column.
If you have ID=1 AND title LIKE '%text%' then if the first condition returns false then why would SQL need to check the next condition?
I've not tested this so if you do decide to create 1,000,000 rows of data to check the OR condition as suggested, then you might as well check it as an AND to see.
hope this helps.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:48 AM
Points: 37,635,
Visits: 29,886
|
|
harag (10/11/2010) Now if it was an AND clause then maybe it might make a difference, especially if you have indexed the ID column but not the title column.
If you have ID=1 AND title LIKE '%text%' then if the first condition returns false then why would SQL need to check the next condition?
If ID is indexed and the title is not then (ID = 1 and title LIKE %text%') and (title LIKE '%text%' and ID=1) will be treated exactly the same way. An index seek on the index with ID, matching rows get the title column looked up and then compared.
Order of evaluation (when columns are involved) is based on what indexes exist and which conditions the optimiser thinks will be quicker to evaluate/reduce row count faster, not on the position of the condition in the where clause.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|