![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Dear Experts, I have a query of the form SELECT * FROM A WHERE X=1 which runs in under a second and returns about 800 records. But when I add another clause and do SELECT * FROM A WHERE X=1 AND Y=2, the query is more than a 100 times slower and the server eventually times out. What I find confusing is that simply taking the first query and checking if Y=2 on every row should at most double the query time. I guess SQL server is trying to do something clever and getting confused in its optimizations. I tried simple things like using a common table expression such as WITH MYCTE AS (SELECT * FROM A WHERE X=1) SELECT * FROM MYCTE WHERE Y=2 or nested queries like SELECT * FROM (SELECT * FROM A WHERE X=1) STUFF WHERE Y=2 but neither work. Is there a way to simply tell SQL Server to just scan all the results for the second where clause instead of trying to do something too clever? Alternatively, do you have any suggestions on what to do in this case? I guess I could use a temporary table but that seems really ugly and wasteful. Thanks, -Emin |
#3
| |||
| |||
|
|
One more strange effect: If I do SELECT * FROM A WHERE X=1 AND Y=2 OPTION (FORCE ORDER) then things only take 5 times as long as SELECT * FROM A WHERE X=1 which is annoying but at least it runs. But if I do SELECT * FROM A WHERE X=1 AND Y=2 OPTION (FAST 5) then it runs super fast. Could someone explain what the FAST option does? [Note that A is a table-valued function in this case] Thanks again, -Emin On Apr 13, 9:08 am, Emin <emin.shop... (AT) gmail (DOT) com> wrote: Dear Experts, I have a query of the form SELECT * FROM A WHERE X=1 which runs in under a second and returns about 800 records. But when I add another clause and do SELECT * FROM A WHERE X=1 AND Y=2, the query is more than a 100 times slower and the server eventually times out. What I find confusing is that simply taking the first query and checking if Y=2 on every row should at most double the query time. I guess SQL server is trying to do something clever and getting confused in its optimizations. I tried simple things like using a common table expression such as WITH MYCTE AS (SELECT * FROM A WHERE X=1) SELECT * FROM MYCTE WHERE Y=2 or nested queries like SELECT * FROM (SELECT * FROM A WHERE X=1) STUFF WHERE Y=2 but neither work. Is there a way to simply tell SQL Server to just scan all the results for the second where clause instead of trying to do something too clever? Alternatively, do you have any suggestions on what to do in this case? I guess I could use a temporary table but that seems really ugly and wasteful. Thanks, -Emin |
#4
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |