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 5036
On Thu, 24 Jan 2008 20:11:21 -0800 (PST), Jared
<bl************ *******@gmail.c omwrote:
I don't know why you would expect the QEP to be the same. I wouldn't.
It would be interesting to see if COALESCE would cause the same
performance bottleneck. Can you try that on your system?
-Tom.
>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
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)
Try:
DECLARE @Name VARCHAR(32)
SET @Name = 'Bob'
SELECT * FROM Employees as e
INNER JOIN (SELECT id FROm Employees WHERE Name = @Name or Name IS NULL) t2
ON e.ID = t2.ID
Replacing ID with whatever your main Key is called
--
Jack Vamvas
_______________ _______________ _____
Search IT jobs from multiple sources- http://www.ITjobfeed.com
"Jared" <bl************ *******@gmail.c omwrote in message
news:d5******** *************** ***********@s8g 2000prg.googleg roups.com...
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
Jared (bl************ *******@gmail.c om) writes:
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.
SQL Server builds the query plan for the entire batch, and thus at
compile time the value of @Name is not known. Therefore the plan must
be such that it yields a correct result in either case.
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 have an article on my web site that discusses a number of possible
approaches to this problem, see http://www.sommarskog.se/dyn-search.html.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
On Jan 24, 10:48*pm, Ed Murphy <emurph...@soca l.rr.comwrote:
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 -
Tom van Stiphout wrote:
"I don't know why you would expect the QEP to be the same. I wouldn't."
I misspoke. I didn't mean that the QEP would be identical step for
step, but rather that the performance would be pretty much the same.
Ed Murphy wrote:
I would at least try the following:
WHERE (@Name IS NULL OR [Name] = @Name)
This gives me the same performance as hard-coded values, which is to
say it returns almost instantly. I'm not sure why using CASE results
in a longer execution time, but at this point I don't really care.
=)
Thanks for your help!
SELECT * FROM Employees
WHERE [Name] = IsNull(@Name,[Name])
"Jared" <bl************ *******@gmail.c oma écrit dans le message de news: d5************* *************** **...legroups .com...
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Paul Mateer |
last post by:
Hi,
I have been running some queries against a table in a my database and
have noted an odd (at least it seems odd to me) performance issue.
The table has approximately 5 million rows and includes the following
columns:
DocID (INTEGER, PRIMARY KEY, CLUSTERED)
IsRecord (INTEGER, NONCLUSTERED)
|
by: 57R4N63R |
last post by:
I'm currently building a website for one of the client. There has been
few errors here and there, but just recently the problem is getting worse.
Basically the symptoms is that when the user try to access the page, it
takes really long time to load. However, after up to 1 hour, the website
will run fine again as normal.
This issue has been there with the site. I usually just ask the system
admin to restart the IIS Service. However, the...
|
by: D. Stimits |
last post by:
A non-profit organization is interested in a new data application that
would use a SQL storage system. I'm interested to know how non-profit
companies that are not selling products are considered for licensing.
Can they use PostgreSQL just like anyone else or do non-profits qualify
as commercial use and need to consider other licensing?
D. Stimits, stimits AT comcast DOT net
---------------------------(end of...
|
by: Varangian |
last post by:
Hi,
I have a performance issue question?
which is best (in terms of efficiency and performance, I don't care
neatness in code)... building an ArrayList of Object Instances using
SqlDataReader OR using SqlDataAdapter to Fill a DataSet or DataTable ?
Thanks!
|
by: shsandeep |
last post by:
The ETL application loaded around 3000 rows in 14 seconds in a Development
database while it took 2 hours to load in a UAT database.
UAT db is partitioned.
Dev db is not partitioned.
the application looks for existing rows in the table...if they already
exist then it updates otherwise inserts them.
The table is pretty large, around 6.5 million rows.
| |
by: Peter Liu |
last post by:
someting like
return (a==b)?a:((b==c)?:..)................
thanks
|
by: toton |
last post by:
Hi,
I want a few of my class to overload from a base class, where the
base class contains common functionality. This is to avoid repetition
of code, and may be reducing amount of code in binary, not to get
polymorphic behavior.
None of them has virtual methods, and are self contained (no
destructor at all) thus do not have a chance to have memory error.
Thus the derived classes has additional functionality, not additional
data.
|
by: ve01201 |
last post by:
I am facing a peculiar problem when I use a case statement and a filter condition on the same column. The sql structure I use is as follows
select
case col1
when '1'
then 'A'
when '2'
then 'B'
end col1
from
|
by: Shades799 |
last post by:
Hi All,
I was wondering if any of you could help me with a very difficult problem that I am having.
I have an ASP site that works with an Oracle database using an ADODB connection. This connection is stored in a .dll file. This site had been working fine.
However recently we upgraded our Oracle database from 9i to 10g and ever since then we have been having serious performance problems with this site only.
The website works fine...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |