473,657 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Low performance when using parameterised query

Hey...

Why do I sometimes experience low performance when I use a parameter instead
of an exact value?

For example the following performs very bad:

declare @TypeID integer

select @TypeID = 10

select ID from t_Table1 t1, t_Table2 t2 where

t_t1.ID = t_t2.FID and

t2.Type = @TypeID

but this query performs ok:

select ID from t_Table1 t1, t_Table2 t2 where

t_t1.ID = t_t2.FID and

t2.Type = 10

Jakob
Jul 20 '05 #1
1 1665
[posted and mailed, please reply in news]

Jakob (ja************ *@hotmail.com) writes:
Why do I sometimes experience low performance when I use a parameter
instead of an exact value?
For example the following performs very bad:

declare @TypeID integer
select @TypeID = 10

select ID from t_Table1 t1, t_Table2 t2 where
t_t1.ID = t_t2.FID and
t2.Type = @TypeID

but this query performs ok:

select ID from t_Table1 t1, t_Table2 t2 where
t_t1.ID = t_t2.FID and
t2.Type = 10


First, you are not using a parameter, but a variable. This may seem
like a quibble, but it has some importance. More about that later.

To understand this, we need to know that SQL Server maintains statistics
about the values of table columns. When SQL Server needs to build a
plan for a query, it consults the statistics to see how the values
are distributed.

In the second query, SQL Server can thus look up the distribution of
t2.Type and find that maybe only a handful of the rows are likely to
match the condition. Assuming that there is an non-clustered index on
this column, SQL Server assumes that using this index is good.

But in the first query, SQL Server does not know the value of @TypeID,
because SQL Server builds the plan for the entire batch. It therefore
makes a general assumption, and may find that there are not that many
distinct values of t2.Type. If @TypeID has a common value, using the
index will be more expensive than scanning the table.

There is a third case, when you have a stored procedure:

CREATE PROCEDURE jakob @TypeID int AS
select ID from t_Table1 t1, t_Table2 t2 where
t_t1.ID = t_t2.FID and
t2.Type = @TypeID
go
EXEC jakob 10

In this case, SQL Server builds the query plan on first invocation,
and then it does indeed in regard the value passed to it. Sometimes
this may be bad, because SQL Server caches the plan. So if this value
is atypical, succeding invocations could perform poorly.

--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6764
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used the SQL Profile to watch the T-SQL-Command which Access ( who creates the commands?) creates and noticed:
12
8339
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I will work on preparing SQL statements that will create the tables, insert sample record and run the SP. I would hope people will look at my SP and give me any hints on how I can better write the SP.
2
2006
by: Yonatan Goraly | last post by:
I am in the process of adding PostgreSQL support for an application, in addition to Oracle and MS SQL. I am using PostgreSQL version 7.3.2, Red Hat 9.0 on Intel Pentium III board. I have a query that generally looks like this: SELECT t1.col1, t2.col1 FROM t1, t2 WHERE t1.x=t2.y AND t2.p='string' AND t2.q=1 This query is strikingly slow (about 100 sec when both t1 and t2 has
5
3998
by: Scott | last post by:
I have a customer that had developed an Access97 application to track their business information. The application grew significantly and they used the Upsizing Wizard to move the tables to SQL 2000. Of course there were no modifications made to the queries and they noticed significant performance issues. They recently upgraded the application to Access XP expecting the newer version to provide performance benefits and now queries take...
5
4569
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
2
1213
by: Kevin Lawrence | last post by:
Hi all SQL Server 2000 & C# 2005 Currently if I want to execute a batch of INSERT's I will join all the inserts up with ; and then execute them at once in one execution, however I am considering the use of parameterised queries - I'm wondering if there is any major performance issues here, say I have 100 INSERT's, now I've got 100 individual Command objects.
4
2681
by: Jim Devenish | last post by:
I have converted an Access back-end to SQL Server back-end but am having some problems. The Access to Access application has been running well for some years. I have successfully copied all the data to SQL Server and linked the tables to the front end .mdb (I am not using .adp). Some queries were performing poorly so I have converted these to Views and linked to them. Everything works well with good response but when about 8-10...
5
1510
by: John | last post by:
Hi I have a parameterised query for a report like this; PARAMETERS Event_ID Short; SELECT DISTINCTROW Events., ... FROM Events WHERE (((Events.)=)); Now I am trying to run the report while passing the parameter value like
9
5752
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just call it Express for simplicity). I have, to try to simplify things, put the exact same DB on two systems, one running MSDE and one running Express. Both have 2 Ghz processors (one Intel, one AMD), both have a decent amount of RAM (Intel system...
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
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,...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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,...
0
7354
agi2029
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...
1
6177
isladogs
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...
1
2743
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
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.