473,725 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Diff. performance in Query Analyzer than when using stored procedure

Hi group,

I have a select statement that if run against a 1 million record
database directly in query analyzer takes less than 1 second.
However, if I execute the select statement in a stored procedure
instead, calling the stored proc from query analyzer, then it takes
12-17 seconds.

Here is what I execute in Query Analyzer when bypassing the stored
procedure:

USE Verizon
GO
DECLARE @phonenumber varchar(15)
SELECT @phonenumber = '6317898493'
SELECT Source_Identifi er,
BADD_Sequence_N umber,
Record_Type,
BAID ,
Social_Security _Number ,
Billing_Name,
Billing_Address _1,
Billing_Address _2,
Billing_Address _3,
Billing_Address _4,
Service_Connect ion_Date,
Disconnect_Date ,
Date_Final_Bill ,
Behavior_Score,
Account_Group,
Diconnect_Reaso n,
Treatment_Histo ry,
Perm_Temp,
Balance_Due,
Regulated_Balan ce_Due,
Toll_Balance_Du e,
Deregulated_Bal ance_Due,
Directory_Balan ce_Due,
Other_Category_ Balance

FROM BadDebt
WHERE (Telephone_Numb er = @phonenumber) OR (Telephone_Numb er_Redef =
@phonenumber)
order by Service_Connect ion_Date desc

RETURN
GO

Here is what I execute in Query Analyzer when calling the stored
procedure:

DECLARE @phonenumber varchar(15)
SELECT @phonenumber = '6317898493'
EXEC Verizon.dbo.bad debt_phonelooku p @phonenumber

Here is the script that created the stored procedure itself:

CREATE PROCEDURE dbo.baddebt_pho nelookup @phonenumber varchar(15)
AS

SELECT Source_Identifi er,
BADD_Sequence_N umber,
Record_Type,
BAID ,
Social_Security _Number ,
Billing_Name,
Billing_Address _1,
Billing_Address _2,
Billing_Address _3,
Billing_Address _4,
Service_Connect ion_Date,
Disconnect_Date ,
Date_Final_Bill ,
Behavior_Score,
Account_Group,
Diconnect_Reaso n,
Treatment_Histo ry,
Perm_Temp,
Balance_Due,
Regulated_Balan ce_Due,
Toll_Balance_Du e,
Deregulated_Bal ance_Due,
Directory_Balan ce_Due,
Other_Category_ Balance

FROM BadDebt
WHERE (Telephone_Numb er = @phonenumber) OR (Telephone_Numb er_Redef =
@phonenumber)
order by Service_Connect ion_Date desc

RETURN
GO

Using SQL Profiler, I also have the execution trees for each of these
two different ways of running the same query.

Here is the Execution tree when running the whole query in the
analyzer, bypassing the stored procedure:

--------------------------------------
Sort(ORDER BY:([BadDebt].[Service_Connect ion_Date] DESC))
|--Bookmark Lookup(BOOKMARK :([Bmk1000]),
OBJECT:([Verizon].[dbo].[BadDebt]))
|--Sort(DISTINCT ORDER BY:([Bmk1000] ASC))
|--Concatenation
|--Index
Seek(OBJECT:([Verizon].[dbo].[BadDebt].[Telephone_Index]),
SEEK:([BadDebt].[Telephone_Numbe r]=[@phonenumber]) ORDERED FORWARD)
|--Index
Seek(OBJECT:([Verizon].[dbo].[BadDebt].[Telephone_Redef _Index]),
SEEK:([BadDebt].[Telephone_Numbe r_Redef]=[@phonenumber]) ORDERED
FORWARD)
--------------------------------------

Finally, here is the execution tree when calling the stored procedure:

--------------------------------------
Sort(ORDER BY:([BadDebt].[Service_Connect ion_Date] DESC))
|--Filter(WHERE:([BadDebt].[Telephone_Numbe r]=[@phonenumber] OR
[BadDebt].[Telephone_Numbe r_Redef]=[@phonenumber]))
|--Compute Scalar(DEFINE:([BadDebt].[Telephone_Numbe r_Redef]=substring(Conv ert([BadDebt].[Telephone_Numbe r]),
1, 10)))
|--Table Scan(OBJECT:([Verizon].[dbo].[BadDebt]))
--------------------------------------

Thanks for any help on my path to optimizing this query for our
production environment.

Regards,

Warren Wright
Scorex Development Team
Jul 20 '05 #1
5 7291
wa***********@u s.scorex.com (Warren Wright) wrote in message news:<84******* *************** ****@posting.go ogle.com>...
Hi group,

I have a select statement that if run against a 1 million record
database directly in query analyzer takes less than 1 second.
However, if I execute the select statement in a stored procedure
instead, calling the stored proc from query analyzer, then it takes
12-17 seconds.


<snip>

One possible reason is parameter sniffing - see here:

http://groups.google.com/groups?selm...&output=gplain

Simon
Jul 20 '05 #2
sq*@hayes.ch (Simon Hayes) wrote in message news:<60******* *************** ****@posting.go ogle.com>...
One possible reason is parameter sniffing - see here:

http://groups.google.com/groups?selm...&output=gplain

Simon


Wow. Thats a bit of an eye opener. It makes me wonder how best to
make sure a decent plan is chosen by SQL Server, and the answer seems
to be to make it recompile the stored procedure every time ?

or is there a way to make SQL simply use the index at all times? I'd
hate to spend a lot of time on my dev machine getting the stored
procedure to run correctly on a million record table, only to port it
to my production machine and have it take forever on the 33 million
record database because of some magically crafted execution plan :-)

Thanks,

Warren
Jul 20 '05 #3
Here is something else that I don't understand. The stored procedure
I listed above compares a phone number that is passed in against a
Telephone_Numbe r column that is 15 digits long, and against a computed
column (Telephone_Numb er_Redef), that is the left 10 digits of the
Telephone_Numbe r column.

This is because sometimes our client passes in a 10 digit number, and
sometimes a 15 digit version that includes some check digits on the
end (Don't ask).

Anyway, in the execution plan for when the stored proc is executing, I
see the following:

-----------------------------
Sort(ORDER BY:([BadDebt].[Service_Connect ion_Date] DESC))
|--Filter(WHERE:([BadDebt].[Telephone_Numbe r]=[@phonenumber] OR
[BadDebt].[Telephone_Numbe r_Redef]=[@phonenumber]))
|--Compute Scalar(DEFINE:([BadDebt].[Telephone_Numbe r_Redef]=substring(Conv ert([BadDebt].[Telephone_Numbe r]),
1, 10)))
|--Table Scan(OBJECT:([Verizon].[dbo].[BadDebt]))
-----------------------------

It appears to be recomputing the Telephone_Numbe r_Redef column values
on the fly, instead of using the values already present. The
Telephone_Numbe r_Redef column is indexed specifically to allow that
second comparison in the WHERE statement to be a SARG, but it seems
this is being ignored.

Is it being ignored because SQL had already decided to do a table
scan, and so though it might as well speed things up by not scanning
both columns? or is SQL doing a table scan because it thinks it needs
to re-compute the values for Telephone_Numbe r_Redef on the fly?

Argh.

Thanks,

Warren Wright
Scorex Development Team
Dallas
Jul 20 '05 #4
More follow-up on this issue, to help you experts analyze what's going
on here.

I've spent the day trying various things, with no success. I tried
using hints to suggest that the index on Telephone_Numbe r_Redef be
used, which results in an error stating the stored procedure couldn't
be executed due to an unworkable hint.

I've tried declaring a new variable in the stored proc with a value
set equal to the @phonenumber input, so SQL couldn't optimize based on
the actual value being passed in.

I've tried changing the index for the computed column to be a
clustered index.

I only wish I could simply tell SQL to use the same execution plan it
uses when I run the query from the analyzer!! All problems would be
solved!

No matter what, if I run the query from query analyzer, the response
time is a few milliseconds. If I run the stored procedure, the
response time is at least 17 seconds due to a completely suboptimal
execution plan (where the Telephone_Numbe r_Redef's index isn't used at
all).

Introducing the new version of the stored proc with the OR statement
that checks against the computed column as well bogs down the
production server, and results in timeouts and app errors for our
client.

Frustrated,

Warren
Jul 20 '05 #5
[posted and mailed, please reply in news]

Warren Wright (wa***********@ us.scorex.com) writes:
Here is something else that I don't understand. The stored procedure
I listed above compares a phone number that is passed in against a
Telephone_Numbe r column that is 15 digits long, and against a computed
column (Telephone_Numb er_Redef), that is the left 10 digits of the
Telephone_Numbe r column.


Computed column? Which you have an index on? Aha!

While Bart's article on parameter sniffing is good reading it is not
the answer here. Index on computed columns (as well on views) can
only be used if these SET options are ON: ANSI_NULLS, QUOTED_IDENTIFI ER,
ANSI_WARNINGS, ARITHABORT, ANSI_PADDING and CONCAT_NULLS_YI ELDS_NULL.
And NUMERIC_ROUNDAB ORT be OFF.

The killer here is usually QUOTED_IDENTIFI ER. That option, together
with ANSI_NULLS is saved with the procedure, so that the run-time
setting does not apply, but the setting saved with the procedure.
QUOTED_IDENTIFI ER is ON by default with ODBC and OLE DB, as well
with Query Analyzer. But OSQL and Enterprise Manager turns it off.
So you need to make sure that the procedure is created with
QUOTED_IDENTIFI ER on.

You can review the current setting with

select objectproperty( object_id('your _sp'), 'IsQuotedIdentO n')

--
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 #6

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

Similar topics

3
4980
by: Dick Zeeman | last post by:
Hi, I have a problem regarding the perfomance of a stp in combination with character translation. The following happens. We have an automated installation script (nt command file) for creating all stored procedures in a database. At first we used osql in this script, but with osql the international
12
8350
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
2424
by: Philip Mette | last post by:
I run a stored procedure manually each day without fail using Query Analyzer.I.E. I right click in the browser window on the stored procedure hit open and execute. I am trying to get my "DBA" to schedule this job in Enterprise manager and they keep getting syntax errors that make no sense and keep asking me to fix the code. Is there any difference in how the job scheduler executes a procedure vrs doing it in Query analyzer via Open/Execute...
1
3163
by: Bruce Hendry | last post by:
In a nutshell, I have a stored proc that takes appx 15 minutes when it uses a read-only database, and about 2 minutes when the database is not read-only. Details: SQL Server 7 The stored proc exists on a writable database on the same server. It stuffs a subset of about 20000 rows into a temp table and repeatedly updates the
12
2953
by: John Scott | last post by:
Ok...here we go... I am trying to manually run an SQL server back up job from c#. Here is a snippet of my code to begin the process: string strSQL = "USE msdb " + "\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " + "\n EXEC sp_delete_job @job_name = 'TempBackupJob' " + "\n GO"; newDB.Command(strSQL);
9
2467
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug through the application in Visual Studio .NET 2003 the application an exception when it executes the query.
1
2292
by: central_scrutinizer | last post by:
I have an ASP that has been working fine for several months, but it suddenly broke. I wonder if windows update has installed some security patch that is causing it. The problem is that I am calling a stored procedure via an ASP (classic, not .NET) , but nothing happens. The procedure doesn't work, and I don't get any error messages. I've tried dropping and re-creating the user and permissions, to no avail. If it was a permissions...
2
1171
by: mariuszb | last post by:
I use job executing stored procedures to get data for OLAP. All works fine except one thing: one of the procedures takes a few hours to complete. Executing procedure via remote desktop and query analyzer directly on the server gives the same behaviour. Same procedure executed from query analyzer/management studio from PC works fine - executes in about five minutes. SQL server 2k. SP has set nocount, estimated execution plans have same...
0
1239
by: billchalmers | last post by:
I am using sql 2005 express and I have a strange proble, when I execute a stored procedure from the query window I get the results I expect, but when I execute it from an asp page then I get a slightly different result. My stored procedure accepts a start date and a Day of week, it then gets the date of the next date that falls on that day, it is for clinic generation, it does an insert into a clinic table, if I pass in the following params ...
0
8752
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
9401
Oralloy
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...
0
9257
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
9176
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
9113
tracyyun
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...
0
8097
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...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.