dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

URGENT: Why is SQL Server evaluating all "AND" expressions? - sqlserver-server

This is a discussion on URGENT: Why is SQL Server evaluating all "AND" expressions? - sqlserver-server ; Why is SQL Server evaluating all AND expressions? When I have "EXPRESSION1 AND EXPRESSION2 AND ...." if EXPRESSION1 evaluates to false, shouldn't SQL stop processing ??? (as all programming languages do). The following statements raises the exception "Null or empty ...


Home > Database Forum > Microsoft SQL Server > sqlserver-server > URGENT: Why is SQL Server evaluating all "AND" expressions?

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 07-25-2007, 08:39 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
usenet is on a distinguished road
Default URGENT: Why is SQL Server evaluating all "AND" expressions?

Why is SQL Server evaluating all AND expressions?
When I have "EXPRESSION1 AND EXPRESSION2 AND ...." if EXPRESSION1
evaluates to false, shouldn't SQL stop processing ??? (as all
programming languages do).

The following statements raises the exception "Null or empty full-text
predicate.", meaning that SQL is still evaluating the AND statement:

declare @freeTextOR varchar(200)
set @freeTextOR = ''


SELECT * FROM MyTable
WHERE (@freeTextOR <> '') AND CONTAINS((Somefield1, somefield2),
@freeTextOR)


Any ideas? Other options?
Thanks!
Reply With Quote
  #2  
Old 07-25-2007, 08:56 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
usenet is on a distinguished road
Default Re: URGENT: Why is SQL Server evaluating all "AND" expressions?

Here's a general answer:

Imagine below:

WHERE col1 BETWEEN 10 AND 20
AND col2 BETWEEN 10 AND 20

Now, say you have an index on col2 and selectivity is 10 rows. Say you have 20 million rows in the
table. Also, say you have no index on col1.

Obviously, you don't want SQL Server to first check col1 and only evaluate col2 if col1 is true.
That would mean that SQL Server need to check (and for that access) every row in the table, i.e. 20
million rows. So, the optimizer will probably "single out" the rows based on the col2 predicate
using the index (only 10 rows) and for these 10 rows check the col1 predicate.

The SQL language is not procedural (first do this, then do that), as you understand from above
reasoning. So, you cannot rely on any short circuit in the SQL language. Or rather, a vendor *could*
build an implementation that optimized the queries so that short circuit applies, but that wouldn't
give acceptable performance.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


"Gaspar" wrote in message news:eZRWHjrzHHA.1184@TK2MSFTNGP04.phx.gbl...
> Why is SQL Server evaluating all AND expressions?
> When I have "EXPRESSION1 AND EXPRESSION2 AND ...." if EXPRESSION1 evaluates to false, shouldn't
> SQL stop processing ??? (as all programming languages do).
>
> The following statements raises the exception "Null or empty full-text predicate.", meaning that
> SQL is still evaluating the AND statement:
>
> declare @freeTextOR varchar(200)
> set @freeTextOR = ''
>
>
> SELECT * FROM MyTable
> WHERE (@freeTextOR <> '') AND CONTAINS((Somefield1, somefield2), @freeTextOR)
>
>
> Any ideas? Other options?
> Thanks!


Reply With Quote
  #3  
Old 07-25-2007, 09:30 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
usenet is on a distinguished road
Default Re: URGENT: Why is SQL Server evaluating all "AND" expressions?

Thanks Tibor for your time.
I'll rewrite my query to avoid this problem.
Cheers,
Gaspar



Tibor Karaszi wrote:
> Here's a general answer:
>
> Imagine below:
>
> WHERE col1 BETWEEN 10 AND 20
> AND col2 BETWEEN 10 AND 20
>
> Now, say you have an index on col2 and selectivity is 10 rows. Say you
> have 20 million rows in the table. Also, say you have no index on col1.
>
> Obviously, you don't want SQL Server to first check col1 and only
> evaluate col2 if col1 is true. That would mean that SQL Server need to
> check (and for that access) every row in the table, i.e. 20 million
> rows. So, the optimizer will probably "single out" the rows based on the
> col2 predicate using the index (only 10 rows) and for these 10 rows
> check the col1 predicate.
>
> The SQL language is not procedural (first do this, then do that), as you
> understand from above reasoning. So, you cannot rely on any short
> circuit in the SQL language. Or rather, a vendor *could* build an
> implementation that optimized the queries so that short circuit applies,
> but that wouldn't give acceptable performance.
>

Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
SQL Server Courses usenet sqlserver-tools 0 03-20-2007 11:09 PM
SQL Server Courses usenet ms-sqlserver 0 03-20-2007 11:08 PM
SQL Server Courses usenet sqlserver-datawarehouse 0 03-20-2007 11:00 PM
MSSQLSERVER Error 17052 usenet ms-sqlserver 16 02-25-2007 09:46 PM
The Data Miner: SQL Server Data Mining Newsletter (February 2005) usenet sqlserver-datawarehouse 0 02-21-2005 10:39 PM


All times are GMT -4. The time now is 08:31 AM.