I've been working on a stored procedure performance issue for over a week now and is related to my other post on Stackoverflow here . Let me give you some background information.
We have a nightly process that starts and starts with a stored procedure that calls many many other stored procedures. Many called stored procedures call others, etc. I looked at some of the processes invoked and there are all sorts of frightening complex things, such as processing XML strings, unnecessary overuse of cursors, NOLOCK hints overused, rare use of complex processing, etc. - the list goes on, it's pretty awful.
This overnight process in our production environment takes an average of 1:15 to run. It sometimes takes 2 hours, which is unacceptable. I created a test environment on identical hardware for production and runs proc. It took 45 minutes when I started it. If I restored the database to the same point and started it again, it will take longer: indeed, if I repeat this action several times (recovery and repeated execution), the process takes a longer time until it becomes a plateau of about 2 hours. It really puzzles me, because every time I restore the database to the same point. There are no other user databases on the server.
I was thinking about two areas of investigation:
- Query Plans and Parameter Changes
- Tempdb
As a test, I restarted SQL Server to clear both cache and tempdb and restart proc with the same database recovery. The process took 45 minutes. I repeated this several times to make sure it was repeatable - again it took 45 minutes each time. Then I proceeded with a few tests to try to isolate the surprisingly longer run time when SQL Server does not restart:
- Run the initial stored procedure WITH RECOMPILE
- Before starting a procedure, run the DBCC FREEPROCCACHE command to clear the procedure cache
- Before starting the procedure, execute CHECKPOINT and then DBCC DROPCLEANBUFFERS to make sure that the cache is empty and clean.
The following script is executed so that all stored procedures are marked for recompilation:
DECLARE @proc_schema SYSNAME
DECLARE @proc_name SYSNAME
DECLARE prcCsr CURSOR local
FOR SELECT specific_schema,
specific_name
FROM INFORMATION_SCHEMA.routines
WHERE routine_type = 'PROCEDURE'
OPEN prcCsr
FETCH NEXT FROM prcCsr INTO @proc_schema, @proc_name
DECLARE @stmt NVARCHAR(MAX)
WHILE @@FETCH_STATUS = 0
BEGIN
SET @stmt = N'exec sp_recompile ''[' + @proc_schema + '].['
+ @proc_name + ']'''
EXEC ( @stmt
)
FETCH NEXT FROM prcCsr INTO @proc_schema, @proc_name
END
. , . - , 3-6 , , . , , proc , ?
tempdb , stackoverflow, , , ( , 24 ).
. 64- SQL Server 2005 3 (SP3) Windows 2003 R2 Ent..
,
.