Issue:

Hi I have a stored procedure and when i run it by using declare and set parameter it gives me result with in 30 seconds but if I use (exec sp_procedurename parametervalue1,parametervalue2) statement then it takes 5 minutes to execute.

Resolution:

If you’re running the same exact SQL as the stored procedure and the times are that different, your stored procedure is probably relying on metadata that is out of date. Try updating statistics or recompiling the stored procedure.

EXEC sp_procedurename ‘1-Nov-2011’, ‘15-Nov-2011’ WITH RECOMPILE
-- Update all statistics on a table
USE AdventureWorks2012;  
GO  
UPDATE STATISTICS Sales.SalesOrderDetail;  
GO  

-- Update the statistics for an index
USE AdventureWorks2012;  
GO  
UPDATE STATISTICS Sales.SalesOrderDetail AK_SalesOrderDetail_rowguid;  
GO  

Sources:

https://learn.microsoft.com/en-us/sql/t-sql/statements/update-statistics-transact-sql?view=sql-server-ver16

https://www.codeproject.com/Questions/620971/Taking-too-much-time-to-execute-stored-procedure

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/ba4b81cd-211c-4716-ba84-19ef2c8290e4/stored-procedure-slower-than-running-query-in-sql-server-management-studio?forum=transactsql

Last modified: February 7, 2023

Author

Comments

Write a Reply or Comment