![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
I would like to explicitly prevent SQL Server 2005 from rewriting a query in my WHERE clause because the rewrite slows things down by many orders of magnitude. I have a query like SELECT * FROM A WHERE b in (31, 78) which takes forever (times out actually). If I instead do SELECT * FROM A WHERE b = 31 or if I do SELECT * FROM A WHERE b = 78, each query runs very quickly but the combination is incredibly slow despite the fact that I have indexes on appropriate things. After some investigation with the query optimizer, I determined that SQL Server is rewriting the predicate "b in (31, 78)" as (b>=31) AND (b<=78) as an intermediate step. I suspect this makes the query take a long time because there are *LOTS* of records with b>=31 and b<=78. |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
The usual re-write is: SELECT * FROM A WHERE b = 31 OR b = 78; |
#6
| |||
| |||
|
|
What do you get when you use the separate queries with UNION: SELECT <columns> FROM A WHERE b = 31 UNION ALL SELECT <columns> FROM A WHERE b = 78; -- Plamen Ratchevhttp://www.SQLStudio.com |
#7
| |||
| |||
|
|
Emin (emin.shop... (AT) gmail (DOT) com) writes: I would like to explicitly prevent SQL Server 2005 from rewriting a query in my WHERE clause because the rewrite slows things down by many orders of magnitude. I have a query like SELECT * FROM A WHERE b in (31, 78) which takes forever (times out actually). If I instead do SELECT * FROM A WHERE b = 31 or if I do SELECT * FROM A WHERE b = 78, each query runs very quickly but the combination is incredibly slow despite the fact that I have indexes on appropriate things. After some investigation with the query optimizer, I determined that SQL Server is rewriting the predicate "b in (31, 78)" as (b>=31) AND (b<=78) as an intermediate step. I suspect this makes the query take a long time because there are *LOTS* of records with b>=31 and b<=78. If that is happening, I would suspect that statistics are out of date. What happens if you run UPDATE STATISTICS WITH FULLSCAN on the table? -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Links for SQL Server Books Online: SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx SQL 2000:http://www.microsoft.com/sql/prodinf...ons/books.mspx |
#8
| |||
| |||
|
|
--CELKO-- (jcelko... (AT) earthlink (DOT) net) writes: The usual re-write is: SELECT * * FROM A *WHERE b = 31 * * OR b = 78; In SQL Server, this rewrite is performed in the parsing layer, and what the optimizer sees is the above which thus above is identifical with the original query. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Links for SQL Server Books Online: SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx SQL 2000:http://www.microsoft.com/sql/prodinf...ons/books.mspx |
#9
| |||
| |||
|
|
When I try entering that into the sql server management studio express I get errors about incorrect syntax. I'm using sql server 2005, can you give me an example of what syntax I should use? |
![]() |
| Thread Tools | |
| Display Modes | |
| |