Naming conventions are an integral part of quality code but sometimes we need to pay additional care when deciding on the naming patterns. There are two types of programmers, those that prefix everything and those that don't. In the world of SQL Server, the former group is further divided in two parts, the ones that use sp_ prefix for naming their Stored Procedures and others who use something else (usp_ or short app name). The recommendation has been to avoid sp_. This article explains the reasons for this recommendation with supported data from my experiments.
sp_ does not stand for Stored Procedure
The myth is that sp_ stands for Stored Procedure. In fact it means System Stored Procedure. SQL Server has some system Stored Procedures defined in the master database. These procedures start with the sp_ prefix and are accessible from any other database. One such example is sp_helptext. If you want to get the text of a Stored Procedure then execute sp_helptext '<SP Name>'.
Performance impact
To understand the impact on performance, let's create a database, say DummyDb. Create a table DummyTable and insert 100 rows into it.
Create database
- CREATE DATABASE [DummyDb]
- CREATE TABLE [dbo].[DummyTable](
- [Id] [int] NULL,
- [Name]
- [varchar](100) NULL
- ) ON [PRIMARY]
- DECLARE @Count int;
- SET @Count = 100;
- WHILE(@Count>0)
- BEGIN
- INSERT INTO DummyTable VALUES (@Count, 'Name_'+ CONVERT(varchar(100),@Count));
- SET @Count = @Count-1;
- END
Create GetEmployee
- CREATE PROCEDURE GetEmployee
- AS
- BEGIN
- SELECT ID, Name FROM DummyTable
- END
- CREATE PROCEDURE sp_GetEmployee
- AS
- BEGIN
- SELECT ID, Name FROM DummyTable
- END
- GO
- Execute GetEmployee
- DECLARE @Count int;
- SET @Count = 500;
- WHILE(@Count>0)
- BEGIN
- exec GetEmployee
- SET @Count =
- @Count-1;
- END
- Execute sp_GetEmployee
- DECLARE @Count int;
- SET @Count = 500;
- WHILE(@Count>0)
- BEGIN
- exec sp_GetEmployee
- SET @Count =
- @Count-1;
- END
The preceding data clearly prooves that the sp_ prefix degrades the performance of Stored Procedures.
Why it is slow
Whenever SQL Server gets a command to execute a Stored Procedure with sp_ as the prefix, it first scans all of the system Stored Procedures. If it doesn't find a matching procedure there, then it scans the user defined Stored Procedures. This additional time causes the performance degradation.
Rakesh KalluriPosted Apr 1, 2015, 3:29 AM
good
Former memberPosted Mar 27, 2015, 11:26 AM
nice
Khargesh RajputPosted Mar 26, 2015, 11:47 PM
good one sir , I was in practice to use sp_ but now try to avoid thanks sir
Gowtham RajamanickamPosted Mar 26, 2015, 10:20 PM
good one..