We will go over how to optimize Stored Procedure with making simple changes in the code. Please note there are many more other tips, which we will cover in future articles.
- Include SET NOCOUNT ON statement: With every SELECT and DML statement, the SQL server returns a message that indicates the number of affected rows by that statement. This information is mostly helpful in debugging the code, but it is useless after that. By setting SET NOCOUNT ON, we can disable the feature of returning this extra information. For stored procedures that contain several statements or contain Transact-SQL loops, setting SET NOCOUNT to ON can provide a significant performance boost because network traffic is greatly reduced.
CREATE PROC dbo.ProcName
AS
SET NOCOUNT ON;
--Procedure code here
SELECT column1 FROM dbo.TblTable1
-- Reset SET NOCOUNT to OFF
SET NOCOUNT OFF;
GO
- Use schema name with object name: The object name is qualified if used with schema name. Schema name should be used with the stored procedure name and with all objects referenced inside the stored procedure. This help in directly finding the complied plan instead of searching the objects in other possible schema before finally deciding to use a cached plan, if available. This process of searching and deciding a schema for an object leads to COMPILE lock on stored procedure and decreases the stored procedure’s performance. Therefore, always refer the objects with qualified name in the stored procedure like
SELECT * FROM dbo.MyTable -- Preferred method
-- Instead of
SELECT * FROM MyTable -- Avoid this method
--And finally call the stored procedure with qualified name like:
EXEC dbo.MyProc -- Preferred method
--Instead of
EXEC MyProc -- Avoid this method
- Do not use the prefix “sp_” in the stored procedure name: If a stored procedure name begins with “SP_,” then SQL server first searches in the master database and then in the current session database. Searching in the master database causes extra overhead and even a wrong result if another stored procedure with the same name is found in master database.
- Use IF EXISTS (SELECT 1) instead of (SELECT *): To check the existence of a record in another table, we uses the IF EXISTS clause. The IF EXISTS clause returns True if any value is returned from an internal statement, either a single value “1” or all columns of a record or complete recordset. The output of the internal statement is not used. Hence, to minimize the data for processing and network transferring, we should use “1” in the SELECT clause of an internal statement, as shown below:
IF EXISTS (SELECT 1 FROM sysobjects
WHERE name = 'MyTable' AND type = 'U')
- Use the sp_executesql stored procedure instead of the EXECUTE statement.
The sp_executesql stored procedure supports parameters. So, using the sp_executesql stored procedure instead of the EXECUTE statement improve the re-usability of your code. The execution plan of a dynamic statement can be reused only if each and every character, including case, space, comments and parameter, is same for two statements. For example, if we execute the below batch:
DECLARE @Query VARCHAR(100)
DECLARE @Age INT
SET @Age = 25
SET @Query = 'SELECT * FROM dbo.tblPerson WHERE Age = ' + CONVERT(VARCHAR(3),@Age)
EXEC (@Query)
If we again execute the above batch using different @Age value, then the execution plan for SELECT statement created for @Age =25 would not be reused. However, if we write the above batch as given below,
DECLARE @Query NVARCHAR(100)
SET @Query = N'SELECT * FROM dbo.tblPerson WHERE Age = @Age'
EXECUTE sp_executesql @Query, N'@Age int', @Age = 25
the compiled plan of this SELECT statement will be reused for different value of @Age parameter. The reuse of the existing complied plan will result in improved performance.
- Try to avoid using SQL Server cursors whenever possible: Cursor uses a lot of resources for overhead processing to maintain current record position in a recordset and this decreases the performance. If we need to process records one-by-one in a loop, then we should use the WHILE clause. Wherever possible, we should replace the cursor-based approach with SET-based approach. Because the SQL Server engine is designed and optimized to perform SET-based operation very fast. Again, please note cursor is also a kind of WHILE Loop.
- Keep the Transaction as short as possible: The length of transaction affects blocking and deadlocking. Exclusive lock is not released until the end of transaction. In higher isolation level, the shared locks are also aged with transaction. Therefore, lengthy transaction means locks for longer time and locks for longer time turns into blocking. In some cases, blocking also converts into deadlocks. So, for faster execution and less blocking, the transaction should be kept as short as possible.
- Use TRY-Catch for error handling: Prior to SQL server 2005 version code for error handling, there was a big portion of actual code because an error check statement was written after every t-sql statement. More code always consumes more resources and time. In SQL Server 2005, a new simple way is introduced for the same purpose. The syntax is as follows:
BEGIN TRY
--Your t-sql code goes here
END TRY
BEGIN CATCH
--Your error handling code goes here
END CATCH
Reference: Pinal Dave (http://blog.SQLAuthority.com)










A very useful collection of tips.
Though I would expect a few people to come up with the ‘it depends’ and ‘not always’ arguments, this is certainly a helpful set of tips/best-practices that every TSQL developer should add to his/her check list.
ugh..I have a complex stor proc that I could so use all of your help on. It runs fine but takes over 8 hours to run! Any suggestions would be so greatly appreciated.
BEGIN
– SET NOCOUNT ON added to prevent extra result sets from
– interfering with SELECT statements.
SET NOCOUNT ON;
/*
IF OBJECT_ID(‘tempdb..#tempAgingRecords’) IS NOT NULL
drop table #tempAgingRecords
*/
declare @itemid varchar(40),
@dataareaid varchar(10)
–Master records
select distinct t4.product_number itemid,
t3.company_id dataareaid,
1 processed
into #tempAgingRecords
from edw.dbo.f_inventory_transaction t1
left outer join edw.dbo.dim_inventory_trans_type t2 on
t1.transaction_type_key = t2.transaction_type_key
left outer join edw.dbo.dim_company t3 on
t1.company_key = t3.company_key
left outer join edw.dbo.dim_product t4 on
t1.product_key = t4.product_key
left outer join edw.dbo.dim_date t5 on
t1.date_key = t5.date_key
where –t2.transaction_type not in (‘Profit/Loss’, ‘Transfer’, ‘Quarantine Order’, ‘Transfer Order Shipment’, ‘Transfer Order Receive’)
t2.issues_status in (‘None’, ‘Sold’, ‘Deducted’, ‘Picked’)
and t2.receipt_status in (‘None’, ‘Purchased’, ‘Received’, ‘Registered’)
and t1.transaction_quantity < 0
–and t4.product_number = 'Meiban CM'
and t4.product_key is not null
and t5.[date] ’1/1/1900′
order by t4.product_number
–Variables for aging process
declare @negTable table (
negDatestatus datetime,
negQty decimal(28,12),
negProcessed int,
negRecid bigint,
negInventoryAmount decimal(28,12)
)
declare @posTable table (
posDatestatus datetime,
posQty decimal(28,12),
posProcessed int,
posRecid bigint,
posInventoryAmount decimal(28,12)
)
declare @negDatestatus datetime,
@negQty decimal(28,12),
@posDatestatus datetime,
@posQty decimal(28,12),
@negRecid bigint,
@posRecid bigint,
@negInventoryAmount decimal(28,12),
@posInventoryAmount decimal(28,12)
truncate table work_invent_trans_aging
while (select sum(processed) from #tempAgingRecords) 0
begin
select @itemid = itemid, @dataareaid = dataareaid from #tempAgingRecords where processed 0
insert into @negTable
select t5.[date] datestatus,
sum(t1.transaction_quantity) qty,
1 processed,
rec_id,
sum(case when t3.company_id = ‘rel’
then isnull(case when issues_status in (‘None’, ‘Sold’, ‘Deducted’,'Picked’)
and receipt_status in (‘None’, ‘Purchased’, ‘Received’, ‘Registered’)
then t1.transaction_quantity * t4.unit_cost end, 0)
else isnull(case when issues_status in (‘None’, ‘Sold’, ‘Deducted’,'Picked’)
and receipt_status in (‘None’, ‘Purchased’, ‘Received’, ‘Registered’)
then t1.posted_cost_amount end, 0) +
isnull(case when issues_status in (‘None’, ‘Sold’, ‘Deducted’,'Picked’)
and receipt_status in (‘None’, ‘Purchased’, ‘Received’, ‘Registered’)
then t1.adjustment_cost_amount end, 0) +
isnull(case when issues_status in (‘Deducted’,'Picked’)
or receipt_status in (‘Received’, ‘Registered’)
then t1.physical_cost_amount end, 0) end) as InventoryAmount
from edw.dbo.f_inventory_transaction t1
left outer join edw.dbo.dim_inventory_trans_type t2 on
t1.transaction_type_key = t2.transaction_type_key
left outer join edw.dbo.dim_company t3 on
t1.company_key = t3.company_key
left outer join edw.dbo.dim_product t4 on
t1.product_key = t4.product_key
left outer join edw.dbo.dim_date t5 on
t1.date_key = t5.date_key
where t4.product_number = @itemid
and t3.company_id = @dataareaid
–and t2.transaction_type not in (‘Profit/Loss’, ‘Transfer’, ‘Quarantine Order’, ‘Transfer Order Shipment’, ‘Transfer Order Receive’)
and t2.issues_status in (‘None’, ‘Sold’, ‘Deducted’, ‘Picked’)
and t2.receipt_status in (‘None’, ‘Purchased’, ‘Received’, ‘Registered’)
and t1.transaction_quantity < 0
–and t4.product_number is not null
and t5.[date] ’1/1/1900′
group by t5.[date],
rec_id
having sum(t1.transaction_quantity) 0
–and t4.product_number is not null
and t5.[date] ’1/1/1900′
group by t5.[date],
rec_id
having sum(t1.transaction_quantity) > 0
–Get the first positive quantity
select @posRecid = posRecid, @posDatestatus = posDatestatus, @posQty = posQty, @posInventoryAmount = posInventoryAmount from @posTable p1 where posProcessed 0 order by posDatestatus desc, posRecid desc
while (select isnull(sum(negProcessed), 0) from @negTable n1) 0
begin
–Get negative quantity
select @negRecid = negRecid, @negDatestatus = negDatestatus, @negQty = negQty, @negInventoryAmount = negInventoryAmount from @negTable n2 where negProcessed 0 order by negDatestatus desc, negRecid desc
if (select isnull(sum(posProcessed), 0) from @posTable) = 0
begin
–Insert negative value if there are no positive quantities
insert into work_invent_trans_aging
values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, ’1/1/1900′, @negQty, ‘New1′, @negInventoryAmount)
set @negQty = 0
set @negInventoryAmount = 0
end
else
begin
if @posQty > (@negQty * -1)
begin
–Write into result table the consumed negative amount
insert into work_invent_trans_aging
values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, @negQty, ‘Original’, @negInventoryAmount)
set @posQty = @posQty + @negQty
set @posInventoryAmount = @posInventoryAmount + @negInventoryAmount
end
else
–If the negative quantity has left to consume, get another positive record
while @negQty < 0
begin
–If there are no more positive record (still negatives left), then insert and end
if (select isnull(sum(posProcessed), 0) from @posTable) = 0
begin
insert into work_invent_trans_aging
values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, '1/1/1900', @negQty, 'New2', @negInventoryAmount)
set @negQty = 0
set @negInventoryAmount = 0
end
else
begin
if (@negQty * -1) < @posQty
begin
insert into work_invent_trans_aging
values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, @negQty, 'New3', @negInventoryAmount)
set @posQty = @posQty + @negQty
set @posInventoryAmount = @posInventoryAmount + @negInventoryAmount
set @negQty = 0
set @negInventoryAmount = 0
end
else
begin
–Write into result table the consumed positive amount
insert into work_invent_trans_aging
values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, (@posQty * -1), 'New4', (@posInventoryAmount * -1))
set @negQty = @negQty + @posQty
set @negInventoryAmount = @negInventoryAmount + @posInventoryAmount
update @posTable
set posProcessed = 0
where posDatestatus = @posDatestatus
and posRecid = @posRecid
select @posRecid = posRecid, @posDatestatus = posDatestatus, @posQty = posQty, @posInventoryAmount = posInventoryAmount from @posTable p1 where posProcessed 0 order by posDatestatus desc, posRecid desc
end
end
end
end
update @negTable
set negProcessed = 0
where negDatestatus = @negDatestatus
and negRecid = @negRecid
set @negDatestatus = NULL
set @negQty = NULL
set @negRecid = NULL
end
–select * from @posTable t1 order by t1.posDatestatus, posRecid
–select * from @negTable t1 order by t1.negDatestatus, negRecid
–Clear aging variables
set @negDatestatus = NULL
set @negQty = NULL
set @negRecid = NULL
set @negInventoryAmount = NULL
set @posDatestatus = NULL
set @posQty = NULL
set @posRecid = NULL
set @posInventoryAmount = NULL
delete @negTable
delete @posTable
update #tempAgingRecords
set processed = 0
where itemid = @itemid
and dataareaid = @dataareaid
end
END
GO
Please, when you post something that you are trying to explain it to the community, provide samples like in your other posts, for me is very abstract sample about Try and Catch
BEGIN TRY
–Your t-sql code goes here (give a example here)
END TRY
BEGIN CATCH
–Your error handling code goes here (give a example here)
END CATCH
Anyway thank you for the tips…
Knowing that it is there to use, you could always take a look in Books Online or search elsewhere for in depth examples. There are lots of examples and trying them out will help you understand how it works more clearly. This set of tips doesnt go in to depth on any of the items as they each could have an article on their own.
If i am using temporary tables, local and global in some of my store procedures, should i use indexes (clustered & non-clustered) on these tables to get maximum performance, specially when i know that these temporary tables will contain lot of rows ???
Hello Aasim,
Creating an index to be used for once would not provide any improvement because processing consumed to create an index itself would overcome the benifit of its uses. We should create an index on any table (permanent or temporary table) if that is usable in more than one statements.
Regards,
Pinal Dave
Hello Aasim,
Like SQL Genius (Pinal) said, if we use temp tables more than once in stored procedure, then definitely it is suggested to create indexes on temporary tables especially when you are dealing with huge amounts of data.
I have seen significant differences in my procedure performance after I created Indexes on huge temp tables.
~ IM.
I would create an index if a temp table contained a lot of rows and was going to be used to join another table.
Hello Pinal,
Sorry, but I disagree with your statement about there is no sense for creating an index on # table if this index will be used just once. Many times in my practice “creation index on the fly” saved tens of minutes, sometimes hours on data processing. It is all about data amount. Sometimes you can’t avoid of using a # table and that table becomes huge in some cases. When we talking about million records temporary table and we have to join this table with another multimillion table we have to create indexes otherwise we can wait forever till procedure is completed.
When we have deal with VLDB, the creation of index(es) on # table on the fly is definitely solution.
BTW, very nice site, I like it, thank you!
Thanks,
IgorA
Very useful tips…. thanks
Aasim,
I would profile the procedure with/without indexes, and then decide what is the actual best practice for your case.
A very good the article.
Hi Pinal,
Nice set of tips, especially the one on execute and sp_executesql.
Thank you
Ramdas
Hello!
Interesting that you wrote this article today. Along with my other responsibilities, I am also part of the DBA team and was working on a code review today and recommended the usage of schema qualifiers (dbo.MyStoredProcedure) for the stored procedures. I had logically thought it through and was going to do a little experiment tomorrow to see the performance difference that qualifers provide. I will run the test tomorrow and update if time permits.
Great compilation, by the way. I agree with Jacob – this should be part of every database developer’s/administrator’s checklist.
Have a Great Day!
Use schema name with object name:
I executed the both queries with schema name and without schema name and checked the execution plan of both the queries.I didn’t find any differrence.
Does anyone have solution how to prove this tip?
whenever we execute any SELECT query first database engine checks in master database. if it’s not there in master database it checks in the current session database depends on schema.
Thanks for some great tips that I am sure to use!
Thanks a lot. Very useful tips
Very Good Tips for Writing Stored Procedure.
It help a lot when you are not familiar in writing SP in sql server.
Regarding using point on
Use IF EXISTS (SELECT 1) instead of (SELECT *):
There is no guarantee SELECT 1 will outperform SELECT *
Also the result is a boolean value and it doesn’t matter how many columns are used inside EXISTS
Regarding using point on
Use IF EXISTS (SELECT 1) instead of (SELECT *):
There is no guarantee SELECT 1 will outperform SELECT *
Also the result is a boolean value and it doesn’t matter how many columns are used inside EXISTS
Well, I would think that in the case of EXISTS the subquery is terminated when first matching row is found and only boolean value is returned. This would seem logical.
But since MSDN doesn’t say anything about this I think the safest way would be to use:
EXISTS (SELECT TOP(1) 1 FROM foo WHERE bar = ..)
This is because if subquery isn’t terminated it would do the matching against all the rows in ‘foo’ and return 0..n rows. This means that there would be 0..n unneccessary matching operations and 0..n * column_count data readings.
With TOP(1) you tell that you are only insterested in the first matching row and no subsequent rows are matched. And because you are returning constant value 1 there will be no unnecessary read operation to the table.
Okay. I went ahead and tested this. It seems that there’s no diffence between any of these:
SELECT 1 FROM MyTable WHERE EXISTS (SELECT * FROM MyTable)
SELECT 1 FROM MyTable WHERE EXISTS (SELECT 1 FROM MyTable)
SELECT 1 FROM MyTable WHERE EXISTS (SELECT TOP(1) 1 FROM MyTable)
I’ve heard from others to use EXISTS (SELECT *) because it allows SQL Server to select the best index. Also, the exists clause causes it to select only one row
@rudesyle
INDEX usage would not be based on the *, but on the WHERE clause. Think about it, EXISTS causes a lookup which we are hoping to be an INDEX scan of sorts. The match is based on the WHERE clause where the correlation is defined.
@Madhivann
Technically, SELECT 1 is faster: http://www.sqlskills.com/BLOGS/CONOR/2008/02/default.aspx?page=2
Brad Schulz has a nice write-up here: http://bradsruminations.blogspot.com/2009/09/age-old-select-vs-select-1-debate.html
Personally, i use * because it states explicitly “i don’t want the results”. Anything else might give the developer pause.
Brian,
From link1
However, at runtime the two forms of the query will be identical and will have identical runtimes.
So SELECT 1 is not faster
Aslo from link2
Sure, it will take a couple of extra femtoseconds to compile (compared to the boring SELECT 1),
See the definition of femtoseconds
It is actually very negligible in real time
http://en.wikipedia.org/wiki/Femtosecond
@Madhivanan
Yes, but the expansion itself takes time! :)
When Brad said femtoseconds, he was being facetious.
The idea here is, it does take longer, just not much. As is a very, very, very, very, very, very, tiny bit longer.
Pinal, this is great stuff!
Guys – regarding the “select 1″ versus “select *”, I feel you are missing something here.
I have seen notable peformance improvements by using SELECT 1 instead of SELECT *. Looking at the Execution Plan to address to performance, I could see that SELECT * was having to bring back all fields using the clustered index. When SELECT 1 was used instead, a covering index based on the WHERE clause was used by the engine and performance was notably better. As in a query taking 4 secs now takes 0.15 secs.
Also, from a developers point of view, SELECT 1 is preferred because it is symantically stating that the fields are not important to the query.
Thanks Pinal . It’s a great tips.
These tips are very useful an SQL Developer!!!
Thanks Pinal
Beginner like me it’s a great tips.
As Madhivanan points out, there is no benefit in the ‘select 1′ syntax over ‘select *’ if you’re using ‘if exists’ at the same time.
There is no data processed from the query inside the ‘exists’ clause and so it makes no difference what you ‘select’. Books Online’s examples for the ‘exists’ keyword uses the ‘select *’ syntax.
A much more important point to make is not to perform a count and compare it to zero (a lot of people seem to do this) – so:
if (select count(*) from …) > 0
is especially bad, and should be replaced with
if exists (select * from …)
instead.
Great Article
Tips will helpful so much for beginners like me
Thank You
This is great. Thank you all for great participation.
I am really expecting more experts to come and share their suggestions.
Nice set of tips.
A couple of things that every developer should realize is that before directly jumping to tune your stored procedures, make sure you have checked your table size, schema, index information etc etc. Most of the times, the ’cause’ of slow performing queries is bad design.
Check the Tuning advisor http://msdn.microsoft.com/en-us/library/ms166575.aspx
Thanks Pinal,
lovely tips… are they only or there exists any more tips?
Thanks,
Nitin Sharma
Best stored procedures optimization tip => do NOT use them!
Do you have any valid reasons?
Hi,
On the part where you’ve suggested not to use the ‘SP_’ prefix. Wouldn’t that be a problem only if you are creating stored procedures using the dbo schema.
Please elaborate the issue.
Regards.
Hello Hamza,
A stored procedure with prefix “sp_” whether in dbo schema or user defined schema, whether fully qualified name or non-qualified name, always first checked in master database. And if your stored procedure is in another database then an extra search would be an overhead every time you call that stored procedure.
Regards,
Pinal Dave
That’s why I say
SQLAuthority rocks!!!
I have seen in one of the blogs to use EXISTS like
IF EXISTS(Select null from table)
Will it optimize the perfomance better than
IF EXISTS(Select 1 from table)
?
@Divya
It makes no difference what is put there.
Brad Schulz has an interest article on it: http://bradsruminations.blogspot.com/2009/09/age-old-select-vs-select-1-debate.html
Even 1/0 is allowed! Obviously, it is not evaluated.
Technically, however, * does get expanded, adding some minuscule amount of time: http://www.sqlskills.com/BLOGS/CONOR/2008/02/default.aspx?page=2
So, anything other than * takes the same amount of time. * takes an iota longer. Personally, i use *, to show that i do not care what the results are.
Hello Divya,
I not think so because there is no significant difference in transferring of 1 byte and a null value.
Other than that there is no difference between these two queries.
Regards,
Pinal Dave
“I not think so because there is no significant difference in transferring of 1 byte and a null value.”
While that is true in this exact case I like to be pedantic about this and elaborate this a bit and probably confuse everybody up :)
There is no concept NULL value if you think about C code or even the CPU. Pointers in C can have NULL values but that just means that the pointer is pointing to memory in the address 0×00000000 (in 32-bit machine). The actual pointer is still taking sizeof(VOID*) amount of memory even when it points to NULL.
Now if you think about functions in C or any other language they always reserve space in the function stack for the return value. You can say, again in C, that you don’t care about the return value and declare the function to return “void” but still it takes 4 bytes of memory in 32-bit machine for return value.
And actually, of you think about the CPU, there is 32 bit register reserved exactly for this.
And now considering that it makes no difference to return one byte or four bytes since there is space for four bytes anyway. And in fact, many times (all the times?) when you deal with C data types CHAR (1 byte) or SHORT (2 bytes) you end up taking 4 bytes because of the padding to keep memory aligned.
Uh! I almost felt like going back in time some ten years when I was writing kernel drivers for Windows :)
Thank you Marko for providing the details about memory allocation of NULL value.
Regards,
Pinal Dave
Good stuff! Would love a follow up on table/scalar functions performance.
Very good tips, useful for sql developers.
@Brian,
Nice blog posts
But none of them proved that SELECT 1 will outperform SELECT *. They only guess it
@Madhivanan
The Conner article does explain about the extra action being done. Whether this is noticeable or not is another question. But it is an extra action over a non-* query.
Thanks for another great post! You continue to provide great posts to the SQL-community.
Sir, This is very useful tips for my site. But also have one problem.
I am searching for a stored procedure which will eliminate all the STOP words like “in”, “the” in my query and accordingly search the result.
Is it correct :-
//This is my stored procedure
CREATE PROCEDURE sp_GetInventory
@location varchar(50) AS
select column1, column2 from table1
where column1 like ‘%SearchString%’ or column2 like ‘%SearchString%’
EXECUTE sp_GetInventory ‘SearchString’
Dear pinal,
very useful tips for Optimization for stored procedures.
Yes it is really good performance not using SP_ProcName.
We are using instead as dbo.USP_ProcName means UserStoredProcedure
[...] 21, 2010 by pinaldave Few days ago I wrote article about SQL SERVER – Stored Procedure Optimization Tips – Best Practices. I received lots of comments on particular blog article. In fact, almost all the comments are very [...]
Hello Pinal Sir,
These tips are very useful for me…..
Thanks….
It is very helpful article, but I have one comment about the point
Do not use the prefix “sp_” in the stored procedure name: If a stored procedure name begins with “SP_,” then SQL server first searches in the master database and then in the current session database. Searching in the master database causes extra overhead and even a wrong result if another stored procedure with the same name is found in master database.
I created two stored procedures with the same name in “Master” and “Another new DataBase”, and I executed it from the new one.
The result returned from the new one.
This mean that, it didn’t execute the one in the Master, although there’s a SP in the Mater with the same name.
Can you please elaborate this.
This is a killer post since optimizing database code or SQL statements is a killer job.
However the hints provided here are very useful. Simple optimization hints, that anyone can use, and more advanced but still easy to understand.
About the SELECT 1 versus the SELECT * i think that the first should be preferred over the last.
A few years ago i came across a few optimization tips for Oracle (my previous professional background) and this tip was already mentioned. But later i read that Oracle internally optimizes EXIST (SELECT * FROM …). Perhaps the same approach is used inside SQL Server and that’s why the difference isn’t noticed.
The reason stated for using SELECT ‘ANY_CONSTANT’ over SELECT * is that * would read every column, so it would be more efficient to return a constant. The gains would be obtained in the reduced reading of data.
Regards.
Great article!
Use schema name with object name:
I executed the both queries with schema name and without schema name and checked the execution plan of both the queries.I didn’t find any differrence.
Does anyone have solution how to prove this tip?
Hello Swati,
The difference will be considerable when the queries involves too many tables,views and processes large amount of data.
Regards
Sheju
Pinal,
If we execute the Stored procedure starting with “sp_”
by this way
Exec DBname.dbo.SPname
SQL engine directly go on specified database instead of master DB to search that sp.
Is it right? Let me know your comments.
Thanks
Darshan Shah
Yes. That is correct
i have confused about store procedure becuase query is good to procedure plz expain full how to store procedure and what is use of bussiness inudstires
Hi Pinal,
Could you please tell some what in detail about performance tuning, may be with an example like – If a query takes more time what are all the causes for that or if a stored procedure takes more time what are all causes for that so that many DBA’s as like me who are all in intermediate stage would gain a lot . Thanks!
good afternoon all the query is used to table but what is used of trigges and stored procedure because query is profitable in table but what is used of procedure in table
i dont know what is used of procedure,triggers
plz define with example
what is used of table and database
thankyou
Pinal:
How would one discover / prove via a trace “Do not use the prefix “sp_” in the stored procedure name….first searches in the master database and then in the current session database”
awsome article sir
Truly amazing article…..Really thankful to you Mr.Pinal..
Hi Pinal and all the readers
Nice article..
Btw one more tips of using DDL and DML query..
Always use the DDL queries like create table or drop table first and then use DML query like Select, Update or Delete
As DDL queries always need a recompilation of procedures in order to make the changes in our DML Queries
Nice Artical.
Thanks a lot. Very nice explanation
gr8 article ,,thx Pinal
hi all,
regd. sql sp, i face the prob. regd. passing date value…
it gives error ‘Invalid syntax at date-part..’
here i give u the code 4 sp …
CREATE Procedure BimstInsert
(@Date Datetime,@Pc Numeric(6,0),@Ic Numeric(6,0),@Qt Numeric(6,0))
As
Begin
declare @ra Numeric(6,2)
Declare @ch Numeric(6,0)
Select @ra=I.Rate from ItemMst As I
Where @Ic=I.Code
Select @ch=IsNull(Max(B.ChNo),0)+1 from BiMst As B
Insert into BiMst Values
(@ch,@Date,@Pc,@Ic,@Qt,@ra,(@Qt*@ra))
End
———————————————————–
exec BimstInsert (’10/jan/2010′,1,1,10)
i also tried for diff. date style…
pls. help me
thanks in advance…
bye…
You need to use unambiguous format like YYYYMMDD. Refer this post for more informations
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-ii.aspx
where are the values which you passing in
Insert into BiMst Values
(@ch,@Date,@Pc,@Ic,@Qt,@ra,(@Qt*@ra))
the value must me in datetype format which you will be passing in @date
Hi sir,
I am fresher and i am working in welcomenetwork company
i have string like ’0012340056789′
but i want only the string like ’12340056789′
that is starting 0 should be removed so how can i
do in sql server function
Reply
Thanks & Regards,
Dipak B. Kansara
select cast(col as bigint) from your_table
Some of my most impressive performance improvements have come from replacing cursor-based code with carefully constructed set-based processing. That tip alone can pay the bills…
Is it better to have multiple small stored procedure or one big stored procedure (Combining multiple sp into one and having some conditional logic in it to choose which sql should be used) ?
I love to have multiple stored procedure specific to the user request in place of having one which can serve all kinds of request but i need some better reasons for it.
Thanks for the all your help over the years…
I think it is better to have seperate procedures so that it is easy to maintain. If the result set contains different number of columns, it is not possible to design a report based on it
Solution for optimizing SQL stored Procedures are WITH (ROWLOCK) for insert, update and select, and if you dont do any calculation on your syntax you can use also WITH (NOLOCK).
Nice site..
Good Site
Hi, Pinal.
Could the number of parameters being sent to a stored procedure from a VB .Net application affect the performance of an If Exists (…) Update… statement in the procedure?
Specifically: The procedure takes 144 parameters some of which are varchar(200), and others are varchar(50). The procedure is called from a live data broadcast handler sub-routine written in VB .Net.
The updates seem to take excessively long time, specially when the broadcasts are quicker. So much so that the time lag grows into several hours over a day.
I have not maintained an index on the table, because it causes another procedure (with insert commands) to slow down.
Any suggestions would be appreciated.
Why are you using 144 parameters? How many columns does your table have?
their in many more way divedi procedure into 2 and call one by one
our system also have limit if over than load program in system then it also slow
why the data base not slow it also effect
144 columns :-)
Sir,
Thanks for the information or tips you provide for optimazation in SQL. I have been working on an application which uses XML file and database as well. We read XML files and extract required information and update that information in database. We have approx 18 stored procedure which updates 7-8 tables for a single XML file.
And we have about 22500 Xml files. It takes about 3 hours on local computer and if we update remote server using this application it takes about 10 hours to run completely. Please tell me more about optimization in SQL stored procedures.
Thanks
> The reuse of the existing complied plan will result in >improved performance.
It is true for sp_executesql if we have uniform distribution of data. In other case we will have degradation of performance. You can save a bit time on reuse existing plan but you can lose a lot time when you reuse bad plan. So in my opinion if you have big table with non-uniform distribution of data reusing of plan is bad idea
i want to auto insert records in a muster entry table with imp fields in time & out time as a data type date time
should i prefere trigger to auto insert my in time daily
at 9:00 am except weekends????
What is the purpose of this? This cannot be automated using a trigger. You should use a job to do this
declare @CountryID [int]
select @CountryID=isnull(max(CountryID),0)+1 from Country
no need to use trigger simple query u can auto increment
Use schema name with object name: The object name is qualified if used with schema name. Schema name should be used with the stored procedure name and with all objects referenced inside the stored procedure. This help in directly finding the complied plan instead of searching the objects in other possible schema before finally deciding to use a cached plan, if available. This process of searching and deciding a schema for an object leads to COMPILE lock on stored procedure and decreases the stored procedure’s performance. Therefore, always refer the objects with qualified name in the stored procedure like
SELECT * FROM dbo.MyTable — Preferred method
– Instead of
SELECT * FROM MyTable — Avoid this method
–And finally call the stored procedure with qualified name like:
EXEC dbo.MyProc — Preferred method
–Instead of
EXEC MyProc — Avoid this method
Use schema name with object name
I’m not able to find the difference between
SELECT * FROM dbo.MyTable — Preferred method
– Instead of
SELECT * FROM MyTable — Avoid this method
Tell me the difference . I dont undestand this concept
Hello Ramya,
When you use dbo.mytable it indicates that schema name,server serch easly.
Hi Dave,
As you said
“Do not use the prefix “sp_” in the stored procedure name: If a stored procedure name begins with “SP_,” then SQL server first searches in the master database and then in the current session database. Searching in the master database causes extra overhead and even a wrong result if another stored procedure with the same name is found in master database.”
- so i have created the stored procedure sp_test in my own database and same in master database.
i have excuted the stored procedure from my local database and i found that local stored procedure is getting executed all time.
please clarify me..
Fortunately most applications have stored procedures that follow the same basic operations and design.
So I always prefer some stored procedure generator so that sp becomes consistent.
Tools4Sql.net has gr8 sp generator and it really generates optimized and efficient stored procedures for MS SQL Server
Hi Dave,
I have a typical situation. I have two tables, 1. user 2.address. each user contains more than 2 or 3 address. I have to update user table with all address ids and count concatenated string in a field.
ex: user.address = 4;addid1;sddid2;addid3.
I have used cursor, while loop and temp table to update. in all cases it is taking more than 24 hours. for production we don’t have that much time. I have to complete in 4 to 6 hours.
can you please guide me?
your help much appreciate.
Regards,
Seshadri
Can you post some sample data from each table and the result you want?
I did a comparison executing and showing actual plan and cost and between the 2, they were both at 50% so could you provide an example of where there is a definite impact?
The point I tried to verify was
Use IF EXISTS (SELECT 1) instead of (SELECT *)
I have used Tools4Sql.net and I found it wonderful but I think they are missing features of Encryption which is really necessary.
Simple but useful tips. Thanks.
HI sir,
Very nice.. It helped me a lottttt……
Thanks a bunch…Great tips
Hi Dave,
Nice Article
hi sir,
i am using cursors for looping
any alternative.
problem statement is i want pass cursor value in where condition in select statement
example:
select count(*) from table
— inner joins
where id= cursor value
Post your full requirement. There may be an easy way of doing it without using a cursor
[...] (Read more here) [...]
I think this point is bit misleading: Use IF EXISTS (SELECT 1) instead of (SELECT *):
The column list with SELECT in IF EXISTS () is ignored, so you provide column list or * it just ignores them, here is an example:
if exists (select 1/0)
select ‘yes’
else
select ‘no’
GO
select 1/0
GO
plz sombody resolve my problem
i have a query in sql server and i have to change the results into xml form through stored procedure
plz sombody tell
thanks
Have a look at For xml path in SQL Server help file
Hi sir
please give a very importance point of store procedures and function Difference
Hi sir
please give a very importance point of store procedures and function Difference
Hii Pinal,
Nice artical.
Thanks for the information or tips you provide for optimazation in sql.
nice tips pinal thks
Hi Pinal,
Nice artical.
But as you mentioned there in your article-
“always refer the objects with qualified name in the stored procedure” like:
—————————————————————————————————
SELECT * FROM dbo.MyTable — Preferred method
– Instead of
SELECT * FROM MyTable — Avoid this method
–And finally call the stored procedure with qualified name like:
EXEC dbo.MyProc — Preferred method
–Instead of
EXEC MyProc — Avoid this method
—————————————————————————————————
we could face problem when we have more than one database position like a test database server and a live database and database admin are different or database admin can be changed time to time, in that case to maintain the synchronization we need to change the query regarding database admin, which is quite impossible for a developer.
Hello, Amit!
User-schema separation introduced in SQL Server 2005 was meant to avoid this situation that you are talking about. Users are no longer directly linked to an object.
In the qualified object name that Pinal demonstrates, the “dbo” is not the user name, but the default schema name.
Hello Pinal Sir,
Good Artical.
Kamlesh Gupta
Use schema name with object name:
Hi sir i have some doubt regarding the above explanation
I try select * from dbo.Categories in NorthWInd db
execution plan showing Clustred index scan Estimated cpu cost 0.003125
and select cached plan size 9B
after that i try select * from Categories
execution plan showing Clustred index scan Estimated cpu cost 0.003125
and select cached plan size 9B
both the execution plans are same.
so why we need to put schema name with object name
regards,
swapnil K
SQL Server takes some extra time to determine the schema of the object if it omitted. This time is really very mimimum that you cannot find a difference at all
Re: Swapnil,
For this situation to work, as Pinal is suggesting, you will have to take note of the time taken before the cached plan is found. Having the qualified schema name reduces the time taken to search for the correct plan.
Obviously we’re splitting hairs here, but in the world of DBA’s that’s often what we do if there’s a requirement to attain the fastest possible response.
Dallas
Hi Pinal,
very good article and informative too.
Hi Yashwant,
I think T4S Stored Procedure Generator is good but will it improve performance of my SPs or not?
Why do u make things complex by adding odd things like “SET NOCOUNT ON;”
try keep things simple.
“CEATE PROCEDURE proc_name
as
your query”
thats it
Contrary to your comment, SET NOCOUNT ON is quite important. What it ensures is that the number of rows affected is not returned to the caller.
You may be surprised to know, but it is true that not having SET NOCOUNT ON may result in the application not behaving as it should.
In my opinion, removing “SET NOCOUNT ON” is not making things simple – it’s making things suicidal!
Hi Pinal,
Nice set of tips
Does adding Comments in stored procedure or function causes ANY KIND of burden on SQL Server performance ?
Hi Pinal,
These tips are very nice and helping me allot
Thanks
Wonderful article Pinal.
Thank you!
Would you suggest using the schema name for all object references in a sproc including joins and conditional clauses?
For example:
SELECT b.BatchID, b.BatchName, bt.BatchTransactionID, bt.BatchTransactionName
FROM dbo.Batch
INNER JOIN dbo.BatchTrx WITH(NOLOCK) ON dbo.BatchTrx.BatchID = dbo.Batch.BatchID
WHERE dbo.Batch.StatusID = 1
AND dbo.BatchTrx.StatusID = 1
Hello Pinal Dave, thanks you for your blog, is a very good source for me.
My cuestion is when I execute the select directly in the SP does it reuse the
execution plan ?, like this
CREATE PROC dbo.ProcName @p1 int
AS
SET NOCOUNT ON;
–Procedure code here
SELECT column1 FROM dbo.TblTable1 where ProcID = @p1
– Reset SET NOCOUNT to OFF
SET NOCOUNT OFF;
GO
Thanks for the excellent Tips..