473,666 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

odd stored procedure behavior

We have a stored procedure that we've tried with two slightly
different designs. It needs to take a 30 day date range and return a
result set.

Design 1 takes one date as a parameter. The other date is calculated
in a local variable to be 30 days before the one that was passed in.
Both data types are datetime and are in the where clause.

Design 2 takes two dates as parameters with the 30 days being
calculated outside the stored procedure, both in the where clause.

There's some joins, but the main table has maybe 20 million rows.

This is sql server 2000.

Design 1 takes maybe 30 mintues to run. Design 2 runs 15 times
faster.

The plan says that Design 1 is doing a table scan on the 20 million
row table. For Design 2, the plan says it's doing a bookmark lookup
on the date in question.

Why?

brian

Feb 8 '07 #1
1 1598
brianlanning (br**********@g mail.com) writes:
We have a stored procedure that we've tried with two slightly
different designs. It needs to take a 30 day date range and return a
result set.

Design 1 takes one date as a parameter. The other date is calculated
in a local variable to be 30 days before the one that was passed in.
Both data types are datetime and are in the where clause.

Design 2 takes two dates as parameters with the 30 days being
calculated outside the stored procedure, both in the where clause.
...
Design 1 takes maybe 30 mintues to run. Design 2 runs 15 times
faster.

The plan says that Design 1 is doing a table scan on the 20 million
row table. For Design 2, the plan says it's doing a bookmark lookup
on the date in question.

Why?
When the optimizer compiles the query plan, it works the stored procedure
as a whole. Thus it has no knowledge of the values of local variables.
Instead it applies standarad assumption which for BETWEEN is 20%, if
memory serves.

However, the optimizer does look at the parameter values, and use
these as guidance. When it knwos both end of the period, it can
estimate more exactly how many rows you will select. Note however
that this plan is cached, and if you the next time call the procedure
with an interval of, say, two years, the same plan will be used,
although that plan may not be good for this longer interval.

What you also could try is:

datecol BETWEEN @startdate AND dateadd(DAY, 30, @startday)

Hopefully, the optimizer understands the condition and can act
accordingly.

--
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
Feb 8 '07 #2

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

Similar topics

4
7969
by: TJ Olaes | last post by:
Hello all, this is my second post to this newsgroup. It's a question about stored procedures and permissions and how these behave between databases. Here's the scenario. I have a database that stores information for a system "A", and I have a different database on the same SQL server that stores the login and other info "LOGIN". I write a stored procedure in the "A" database that checks some tables in the "LOGIN" database, let's call...
4
2577
by: Dan | last post by:
I've encountered some strange behavior in a recursive procedure I'm writing for a bill of materials. First let me ask directly if what I think is happening is even possible: It seems like the procedure is not following the recursion in serial order, but in parallel. In other words, after one instance of the procedure calls itself, it continues executing lines below the recursion before the recursion is done. Is that possible? I...
7
5671
by: Anthony Robinson | last post by:
Have been encountering an odd issue. Every now and again, certain packages of stored procedures just become invalid. I'm aware that dropping or altering an underlying table would render a package invalid, but we are doing no such thing... After banging my head on the wall for a bit I noticed that the two stored procedures that are experiencing this behavior are procedures that are called from within another procedure (they're not both...
0
1220
by: Anthony Robinison | last post by:
I'm seeing some really messed up behavior from a stored procedure. Here is the DDL: CREATE PROCEDURE AIM.GetUserIDRetrievals (IN strUserID VARCHAR(64), IN decAIMConnectionID DECIMAL(13,0)) ------------------------------------------------------------------------ -- SQL Stored Procedure ------------------------------------------------------------------------ SPECIFIC AIM.GetUserIDRetrieval LANGUAGE SQL
2
5450
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
2
3496
by: TJS | last post by:
in a script, a stored procedure is called. I want to verify the procedure is complete before proceeding ahead. How can I have the script wait until a stored procedure is verified as complete before proceeding ahead ??
0
2140
by: george | last post by:
Hi world! Anyone experienced building gridview in Oracle environment using stored procedures, please help! Right now, I have issues using Oracle SP, I spent a whole day on this and still cannot get the results back, so shamelessly turn to you for help! OK, the web page part of SqlDataSource is defined as: <asp:SqlDataSource ID="SqlDataSource4" runat="server"
2
5086
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have coded multiple select statements in a single stored procedure, and when I execute this procedure on SQL Server Management Express, I correctly get multiple result sets. But, if I try to add a new Data Source to to my VB 2005 project, and point to this stored procedure, the data source wizard only sees the 'first' select statement. Is there a way to load multiple tables in a DataSet from a single stored procedure with multiple...
0
2751
by: zaza24 | last post by:
Hello there I am loosing my time since a few days trying to install a CRM (ACT! 2008 version 10) software on a brand new HP notebook with Vista prof OS. Ths installation do not show any troubles and works fine; the software installs also Microsoft SQL Server Express 2005 (the different services installed are SQL Server for ACT7, SQL server Active Directory Helper, SQL Server Browser and SQL Server VSS Writer). SQL Service for ACT7 is in...
0
8356
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
8871
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
8640
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
6198
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
5666
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
4198
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...
1
2773
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
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.