473,748 Members | 6,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Slow procedure using view


Hi

I have a procedure that calls a view. The view is built with some outer
joins but it performs fine.

If I run in Query Analyzer

select count(*) from long_name_view
where name_id = 'AAA'

it returns instantly

The procedure has the same code. I juststripped down the code to narrow
the problem:

create or replace procedure my_name_proc
@nid VARCHAR(32)
AS

DECLARE
@nidkey_count INTEGER

select @nidkey_count = count(*)
from long_name_view
where name_id = @nid

print 'The count: ' + CAST(@nidkey_co unt as varchar)

GO

When I call in Query Analyzer:

exec my_name_proc 'AAA'

it takes a while to run, over 20 sec and the execution plan is
different. What is the reason that the same view is used in different
ways?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
1 1713
[posted and mailed, please reply in news]

Witold Iwaniec (wi****@poczta. onet.pl) writes:
I have a procedure that calls a view. The view is built with some outer
joins but it performs fine.

If I run in Query Analyzer

select count(*) from long_name_view
where name_id = 'AAA'

it returns instantly

The procedure has the same code. I juststripped down the code to narrow
the problem:

create or replace procedure my_name_proc
@nid VARCHAR(32)
AS

DECLARE
@nidkey_count INTEGER

select @nidkey_count = count(*)
from long_name_view
where name_id = @nid

print 'The count: ' + CAST(@nidkey_co unt as varchar)

GO

When I call in Query Analyzer:

exec my_name_proc 'AAA'

it takes a while to run, over 20 sec and the execution plan is
different. What is the reason that the same view is used in different
ways?


There are two possible answers here. One is that there is an indexed
view or an index on a computed column somewhere, and the procedure
was created with one of the settings ANSI_NULLS or QUOTED_IDENTIFI ER
off. For these settings the current run-time settings does not apply,
but those saved with the procedure. And these settings must be on for
indexed views or indexes on computed columns to be considered. You can
check this with:

select objectproperty( object_id('cur_ get_sp'), 'ExecIsAnsiNull sOn')
select objectproperty( object_id('cur_ get_sp'), 'ExecIsQuotedId entOn')

If any of these are 0, recreate the procedure with these settings on
and try again. (The most likely culprit is Enterprise Manager which
has some funny ideas about QUOTED_IDENTIFI ER.)

Note that it does not have to be the long_name_view itself which is
indexed, but it may include references to indexed views.

The other possible answer is that parameter sniffing is at work. When
you run the plain query, SQL Server knows exactly which value to use
and can determine which query plan from this. When you run stored
procedure the first time, SQL Servers uses the input value at that
point to determine the plan. This plan is then cached and reused, but
maybe that initial value was atypical, and you are stuck with a bad
plan as long as it stays in cache. This you can work around with

EXEC my_name_proc 'AAA' WITH RECOMPILE

to force a new plan.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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

13
15176
by: kristoff plasun | last post by:
I have a problem with a C++ DCOM application that prints Crystal Reports with data from Oracle. The SQL query is relatively complex but when the report is printed from the Crystal Reports designer it shows up very fast. When the report is printed from my application it takes about ten times as long to get the report to appear. When printing straight from the Crystal Reports
11
5406
by: Eugenio | last post by:
Excuse me in advance fo my little English. I've got this stored procedure **************************************************************************** ********** declare @Azienda as varchar(3), @Utente as varchar(20), @DataDa as datetime, @DataA as datetime, @AreaDa as varchar(3), @AreaA as varchar(3),
4
8101
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure creates a string of custom SQL statement and returns this string back to the main stored procedure. This SQL statements work fine on there own. The SQL returned from the sub stored procedure are returned fine. The datatype of the variable that...
3
2786
by: gizmo | last post by:
I have a stored procedure that queries a database using a Select statement with some inner joins and conditions. With over 9 million records it takes 1 min 36 sec to complete. This is too slow for my requirements. Is there any way I can optimize this query. I have thought about using an indexed view. I haven't done one before, does anyone know if this would have potential to improve performance or indeed any other performance...
12
2643
by: Neil | last post by:
I previously posted re. this, but thought I'd try again with a summary of facts. I have an Access 2000 MDB with a SQL Server 7 back end. There is a view that is linked to the database via ODBC and has been in place for several years without any performance problems. Recently I added a couple of fields to the output of the view, and it became very slow when scrolling. When just opened in the database window, the linked view takes about...
1
1210
by: Andrew | last post by:
All, Apologies if off topic. I'm new to .net, although an experienced programmer, and am working on the client end of a sql application. It holds every single request that goes through our internet server at work, and the application allows you to search and view this information. One of the main searches is to view the number of requests and filesize downloaded per day over a range of days. As you can imagine, the tables get quite...
3
1232
by: Guoqi Zheng | last post by:
Dear sir, My forum site is getting much slower. At this moment, it takes 20 seconds for the first hit. The second hit is much quicker, I think it is because of cache. Any idea how can I increase the speed of my ASP.NET site. This is a forum site using VB.NET + MSDE. The database is about 500 MB. I think the reason it is slow is because the page navigation. At first, I get the value of how many pages in total, then write the page...
13
3456
by: eighthman11 | last post by:
using Access 2003 and sql server version 8.0 Hey everyone. Created a text box where the user types in an Inventory number and it takes them to that inventory number on the contimuous form. The form is based on a link table to sql server. Here is the code: Dim rst As DAO.Recordset Dim InventoryItem As String InventoryItem = "'" & "TextBoxValue" & "'"
2
9840
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes to open in datasheet view. As an experiment, I deleted all rows in all tables; after that, the query took only seconds to open in both design view and datasheet view. From these facts, I conclude that Access is evaluating the query when I go to...
0
8826
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,...
1
9316
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
9241
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...
1
6793
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...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
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...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
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
3
2211
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.