473,414 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Stored procedure performance optimization

Hello,

I have a question regarding stored procedure desing that provides the
optimal performance. Let's say we have a table Products that consists of
three columns: Name, Status, RegistrationTime. All columns are indexed and
users should be able to lookup data by any of the columns. We have two main
options to design stored procedures for data retrieval:

1. Design separate stored procedures for each search criteria:
LookupProductsByName, LookupProductsByStatus, LookupProductsByTime.

2. Write a generic stored procedure that will fit any search criteria:

CREATE PROCEDURE GetProducts (
@Name varchar(20),
@Status int = NULL,
@FromTime datetime = NULL,
@ToTime datetime = NULL)
AS BEGIN
SELECT
[Name],
[Status],
[RegistrationTime]
FROM [Products]
WHERE [Name]=CASE
WHEN @Name<>NULL THEN @Name
ELSE [Name]
END
AND [Status]=CASE
WHEN @Status<>NULL THEN @Status
ELSE [Status]
END
AND [RegistrationTime]>=CASE
WHEN @FromTimestamp<>NULL THEN @FromTimestamp
ELSE [RegistrationTime]
END
AND [RegistrationTime]<=CASE
WHEN @ToTimestamp<>NULL THEN @ToTimestamp
ELSE [RegistrationTime]
END
ORDER BY [RegistrationTime]
END;

The second option is very attractive, because it is obviously easier to
maintain such code. However, I am a little concerned about performance of
such stored procedure. It is not possible to foresee what index should be
used, index can only be selected each during procedure execution, because
search criteria can include either Name, Status or RegistrationTime. Will it
make this SP inefficient? Or perormance difference in such case is not big
(if any) and we should choose the second option because of its significant
code reduction?

Thanks in advance

Vagif Abilov
va***@online.no
Jul 20 '05 #1
3 4244

"Vagif Abilov" <va***@online.no> wrote in message
news:H3*******************@news4.e.nsc.no...
Hello,

I have a question regarding stored procedure desing that provides the
optimal performance. Let's say we have a table Products that consists of
three columns: Name, Status, RegistrationTime. All columns are indexed and
users should be able to lookup data by any of the columns. We have two
main options to design stored procedures for data retrieval:

1. Design separate stored procedures for each search criteria:
LookupProductsByName, LookupProductsByStatus, LookupProductsByTime.

2. Write a generic stored procedure that will fit any search criteria:

CREATE PROCEDURE GetProducts (
@Name varchar(20),
@Status int = NULL,
@FromTime datetime = NULL,
@ToTime datetime = NULL)
AS BEGIN
SELECT
[Name],
[Status],
[RegistrationTime]
FROM [Products]
WHERE [Name]=CASE
WHEN @Name<>NULL THEN @Name
ELSE [Name]
END
AND [Status]=CASE
WHEN @Status<>NULL THEN @Status
ELSE [Status]
END
AND [RegistrationTime]>=CASE
WHEN @FromTimestamp<>NULL THEN @FromTimestamp
ELSE [RegistrationTime]
END
AND [RegistrationTime]<=CASE
WHEN @ToTimestamp<>NULL THEN @ToTimestamp
ELSE [RegistrationTime]
END
ORDER BY [RegistrationTime]
END;

The second option is very attractive, because it is obviously easier to
maintain such code. However, I am a little concerned about performance of
such stored procedure. It is not possible to foresee what index should be
used, index can only be selected each during procedure execution, because
search criteria can include either Name, Status or RegistrationTime. Will
it make this SP inefficient? Or perormance difference in such case is not
big (if any) and we should choose the second option because of its
significant code reduction?

Thanks in advance

Vagif Abilov
va***@online.no


This article might help:

http://www.sommarskog.se/dyn-search.html

Simon
Jul 20 '05 #2
if you choose second option, dont' forget that expressions such as
@Status<>NULL never evaluate to true even if they are syntactically
valid, but you problably want to evaluate @status IS NOT NULL

Jul 20 '05 #3
Hi,
Step by step.

1.) Create Table as
CREATE TABLE [Products] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [varchar] (20) NOT NULL ,
[Status] [int] NOT NULL ,
[RegistrationTime] [datetime] NOT NULL ,
CONSTRAINT [pk_Products] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
) ON [PRIMARY]
GO

2.) Fill random rows. Exactly 262144 rows
3.) Create Indexes as
CREATE INDEX [Products2] ON [dbo].[Products]([Name]) ON [PRIMARY]
CREATE INDEX [Products3] ON [dbo].[Products]([Status]) ON [PRIMARY]
CREATE INDEX [Products4] ON [dbo].[Products]([RegistrationTime]) ON
[PRIMARY]
4.) Create Proc as
create PROCEDURE GetProducts (
@Name varchar(20),
@Status int = NULL,
@FromTime datetime = NULL,
@ToTime datetime = NULL)
AS BEGIN
SELECT
[Name],
[Status],
[RegistrationTime]
FROM [Products]
WHERE [Name]=CASE
WHEN @Name IS NOT NULL THEN @Name
ELSE [Name]
END
AND [Status]=CASE
WHEN @Status IS NOT NULL THEN @Status
ELSE [Status]
END
AND [RegistrationTime]>=CASE
WHEN @FromTime IS NOT NULL THEN @FromTime
ELSE [RegistrationTime]
END
AND [RegistrationTime]<=CASE
WHEN @ToTime IS NOT NULL THEN @ToTime
ELSE [RegistrationTime]
END
ORDER BY [RegistrationTime]
END
5) And analyse execution plan (Query: GetProducts @Name='ab1')

|--Sort(ORDER BY:([Products].[RegistrationTime] ASC))
|--Clustered Index
Scan(OBJECT:([Prod430D].[dbo].[Products].[pk_Products]),
WHERE:((([Products].[Name]=If ([@Name]<>NULL) then [@Name] else
[Products].[Name] AND [Products].[Status]=If ([@Status]<>NULL) then
[@Status] else [Products].[Status]) AND [Products].[RegistrationTime]>=If
([@FromTime]<>NULL) then [@FromTime] else [Products].[RegistrationTime]) AND
[Products].[RegistrationTime]<=If ([@ToTime]<>NULL) then [@ToTime] else
[Products].[RegistrationTime]))

Wrong - Clustered Index Scan

6)Create index as
CREATE NONCLUSTERED INDEX [Products22] ON [dbo].[Products]
([RegistrationTime] ASC, [Name] ASC, [Status] ASC )
And goto 5)

|--Index Scan(OBJECT:([Prod430D].[dbo].[Products].[Products22]),
WHERE:((([Products].[Name]=If ([@Name]<>NULL) then [@Name] else
[Products].[Name] AND [Products].[Status]=If ([@Status]<>NULL) then
[@Status] else [Products].[Status]) AND [Products].[RegistrationTime]>=If
([@FromTime]<>NULL) then [@FromTime] else [Products].[RegistrationTime]) AND
[Products].[RegistrationTime]<=If ([@ToTime]<>NULL) then [@ToTime] else
[Products].[RegistrationTime]) ORDERED FORWARD)
Yeah OK Index Scan

Warning:

Creating Procedure as
create PROCEDURE GetProducts1 (
@Name varchar(20),
@Status int = NULL,
@FromTime datetime = NULL,
@ToTime datetime = NULL)
AS BEGIN
SELECT
[Name],
[Status],
[RegistrationTime]
FROM [Products]
WHERE ([Name]=@Name or @Name is null)
AND ([Status]=@Status or @Status is NULL)
AND ([RegistrationTime] = @FromTime or @FromTime is null)
AND ([RegistrationTime]= @ToTime or @ToTime is null)
ORDER BY [RegistrationTime]
END;

Is very bad idea because exec time grow dramatically.

Conclusion
Solution <Write a generic stored procedure > is acceptable with good indexes
in tables.
Jul 20 '05 #4

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

Similar topics

10
by: Dragonhunter | last post by:
Hello, The aspfaq.com seems to really push stored procedures, and I hear the same advice here all the time. So I want to take the advice. Is it possible to create and practically maintain,...
12
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...
3
by: nandan | last post by:
Hi, Has any one ever compared the performance of calling a DataTable's Select method with a stored procedure doing the same thing? My point is: dataRows = DataTable.Select(filter) is better or...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
5
by: Nesa | last post by:
I have a stored procedure that wraps a moderately complex query over 5, 6 related tables. The performance of the procedure is unacceptably slow as it takes on average 5-10 min to complete. To...
2
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
1
by: upstart | last post by:
Hi everyone…this is a tough one. You guys have been such a help before, hopefully you can point me in the right direction now. I have a Report I am working on that uses a stored procedure to...
2
by: db2admin | last post by:
hi, I have query which runs great when optimization level is changed to 3 but does not run fine with default optimization level of 5. since this is a query in java code, i do not know how can i...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.