![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE [Name] = CASE WHEN @Name IS NULL THEN [Name] ELSE @Name END Query 2: SELECT * FROM Employees WHERE [Name] = 'Bob' I would expect SQL Server to construct an identical QEP under the hood for these two queries, and that they would require essentially the same amount of time to execute. However, Query 1 takes much longer to run on my indexed table of ~300,000 rows. By "longer", I mean that Query 1 takes about two seconds, while Query 2 returns almost instantly. Is there a way to implement a conditional WHERE clause without suffering this performance hit? I want to avoid using the IF...THEN method because I frequently require several optional parameters in the WHERE clause. Thanks! Jared |
#3
| |||
| |||
|
|
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE [Name] = CASE WHEN @Name IS NULL THEN [Name] ELSE @Name END Query 2: SELECT * FROM Employees WHERE [Name] = 'Bob' I would expect SQL Server to construct an identical QEP under the hood for these two queries, and that they would require essentially the same amount of time to execute. However, Query 1 takes much longer to run on my indexed table of ~300,000 rows. By "longer", I mean that Query 1 takes about two seconds, while Query 2 returns almost instantly. Is there a way to implement a conditional WHERE clause without suffering this performance hit? I want to avoid using the IF...THEN method because I frequently require several optional parameters in the WHERE clause. |
#4
| |||
| |||
|
|
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE [Name] = CASE WHEN @Name IS NULL THEN [Name] ELSE @Name END Query 2: SELECT * FROM Employees WHERE [Name] = 'Bob' I would expect SQL Server to construct an identical QEP under the hood for these two queries, and that they would require essentially the same amount of time to execute. However, Query 1 takes much longer to run on my indexed table of ~300,000 rows. By "longer", I mean that Query 1 takes about two seconds, while Query 2 returns almost instantly. |
|
Is there a way to implement a conditional WHERE clause without suffering this performance hit? I want to avoid using the IF...THEN method because I frequently require several optional parameters in the WHERE clause. |
#5
| |||
| |||
|
|
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE [Name] = CASE WHEN @Name IS NULL THEN [Name] ELSE @Name END Query 2: SELECT * FROM Employees WHERE [Name] = 'Bob' I would expect SQL Server to construct an identical QEP under the hood for these two queries, and that they would require essentially the same amount of time to execute. However, Query 1 takes much longer to run on my indexed table of ~300,000 rows. By "longer", I mean that Query 1 takes about two seconds, while Query 2 returns almost instantly. Is there a way to implement a conditional WHERE clause without suffering this performance hit? I want to avoid using the IF...THEN method because I frequently require several optional parameters in the WHERE clause. Thanks! Jared |
#6
| |||
| |||
|
|
Jared wrote: Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE [Name] = CASE WHEN @Name IS NULL THEN [Name] ELSE @Name END Query 2: SELECT * FROM Employees WHERE [Name] = 'Bob' I would expect SQL Server to construct an identical QEP under the hood for these two queries, and that they would require essentially the same amount of time to execute. *However, Query 1 takes much longer to run on my indexed table of ~300,000 rows. *By "longer", I mean that Query 1 takes about two seconds, while Query 2 returns almost instantly. Is there a way to implement a conditional WHERE clause without suffering this performance hit? *I want to avoid using the IF...THEN method because I frequently require several optional parameters in the WHERE clause. I would at least try the following: WHERE (@Name IS NULL OR [Name] = @Name) as well as WHERE NOT([Name] <> @Name)- Hide quoted text - - Show quoted text - |
|
"I don't know why you would expect the QEP to be the same. I wouldn't." |
|
I would at least try the following: WHERE (@Name IS NULL OR [Name] = @Name) |
#7
| |||
| |||
|
|
Consider the following two functionally identical example queries: Query 1: DECLARE @Name VARCHAR(32) SET @Name = 'Bob' SELECT * FROM Employees WHERE [Name] = CASE WHEN @Name IS NULL THEN [Name] ELSE @Name END Query 2: SELECT * FROM Employees WHERE [Name] = 'Bob' I would expect SQL Server to construct an identical QEP under the hood for these two queries, and that they would require essentially the same amount of time to execute. However, Query 1 takes much longer to run on my indexed table of ~300,000 rows. By "longer", I mean that Query 1 takes about two seconds, while Query 2 returns almost instantly. Is there a way to implement a conditional WHERE clause without suffering this performance hit? I want to avoid using the IF...THEN method because I frequently require several optional parameters in the WHERE clause. Thanks! Jared |
![]() |
| Thread Tools | |
| Display Modes | |
| |