Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Witch query is more efficient?? Expand / Collapse
Author
Message
Posted Saturday, October 9, 2010 6:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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 ??
Post #1001710
Posted Saturday, October 9, 2010 8:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen 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.





Post #1001719
Posted Saturday, October 9, 2010 9:55 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 6:55 AM
Points: 42,817, Visits: 35,939
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

Post #1001727
Posted Saturday, October 9, 2010 11:35 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:13 AM
Points: 36,978, Visits: 31,499
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1001776
Posted Monday, October 11, 2010 5:26 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, September 30, 2013 2:57 AM
Points: 19, Visits: 84
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.
Post #1002072
Posted Monday, October 11, 2010 5:38 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 6:55 AM
Points: 42,817, Visits: 35,939
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

Post #1002076
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse