Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Stored Procedure Recompile

Author  Topic 

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-06-22 : 23:01:40
I have a stored procedure that recompiles many times.
I am using #temp table.
If i can not use derived tables, what i can do to stop recompiling.

Thanks

mk_garg

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-06-22 : 23:08:40
SQL Server 2000 supports table variables, they function very much like temp tables and can reduce the recompiles that a temp table would require. Books Online has an example under "DECLARE".

Be advised that if you are manipulating large amounts of data (100,000+ rows, and/or very wide tables of 100+ columns) then table variables may result in reduced performance. They cannot be indexed or have statistics created on them, which forces the query optimizer to perform scans instead of seeks. The only way to know which is best (table variable or temp table) is to test both and evaluate them.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-06-22 : 23:57:58
There are a lot of reasons why an SP will re-compile during execution. Avoiding them may require some rework on your part. But here are some reasons:

Large number of row changes to any table referenced by the proc after the changes occur.

Creation of objects within the proc.
If you're creating multiple object like temp tables, you can minimize recompiles by placing all create statements together at the beginnning of the procedure.

Referencing temp tables via called or calling procs or dynamic sql will force a recompile.

Cursors declared on temp tables will force recompile
(of course cursors should be avoided generally for performance reasons)

changing session options within the proc can force recompiles depending on the set options being changed.

Feel free to post the code and I'm sure someone can offer some suggestions for avoiding/reducing the recompiles


Be One with the Optimizer
TG
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-06-23 : 00:10:42
Unfortunately i am using sql server 7.0


CREATE PROCEDURE proc1 (
@stream_id int,
@type varchar(20) = 'Stream'
)

AS

if @type = 'Batch' begin
-- if a batch id is passed through the stream id
SELECT @stream_id = StreamId
FROM Batches
WHERE BatchId = @stream_id
end

DELETE StreamTracking
WHERE StreamId = @stream_id

DECLARE @job_id int
DECLARE @is_printing int
DECLARE @is_mailing int
DECLARE @is_inkjetting int
DECLARE @national_job_id int

SELECT @job_id = j.JobId, @is_printing = IsPrinting, @is_mailing = IsMailing, @is_inkjetting = IsInkjet, @national_job_id = NationalJobId
FROM Streams s INNER JOIN
Jobs j ON s.JobId = j.JobId
WHERE StreamId = @stream_id

DECLARE @laser_complete int
SELECT @laser_complete = StatusLevel
FROM JobControl.dbo.Status
WHERE Status = 'Laser Reconciled'

DECLARE @mail_complete int
SELECT @mail_complete = StatusLevel
FROM JobControl.dbo.Status
WHERE Status = 'Mail Reconciled'

DECLARE @inkjet_complete int
SELECT @inkjet_complete = StatusLevel
FROM JobControl.dbo.Status
WHERE Status = 'Inkjet Reconciled'

/* Sites */

CREATE TABLE #temp_data (
MachineType varchar(50),
Site varchar(30),
Qty int,
BatchStatus varchar(50)
)

/* PRINT INFORMATION */
if @is_printing = 1 begin
INSERT INTO #temp_data
SELECT 'Printer', MachineSite, SUM(BatchImages), BatchStatus
FROM Streams st INNER JOIN
Batches b ON b.StreamID = st.StreamID INNER JOIN
BatchesPrintDetails bpd ON b.BatchId = bpd.BatchId INNER JOIN
JobControl.dbo.MachineShifts ms ON ms.MachineShiftId = bpd.MachineShiftId INNER JOIN
JobControl.dbo.Printers p ON p.PrinterID = ms.MachineID
WHERE Remakes = 0 AND st.StreamID = @stream_id AND IsPrinting = 1 AND MachineType like 'Printer%' AND BatchStatus NOT LIKE '%Cancelled'
GROUP BY MachineSite, BatchStatus
end

/* MAIL INFORMATION */
if @is_mailing = 1 begin
INSERT INTO #temp_data
SELECT 'Mail Machine', MachineSite, SUM(BatchMailpacks), BatchStatus
FROM Streams st INNER JOIN
Batches b ON b.StreamID = st.StreamID INNER JOIN
BatchesMailDetails bmd ON b.BatchId = bmd.BatchId INNER JOIN
JobControl.dbo.MachineShifts ms ON ms.MachineShiftId = bmd.MachineShiftId INNER JOIN
JobControl.dbo.MailMachines m ON m.MailMachineId = ms.MachineID INNER JOIN
JobControl.dbo.Status s ON b.BatchStatus = s.Status
WHERE Remakes = 0 AND st.StreamID = @stream_id AND IsMailing = 1 AND MachineType like 'Mail Machine%' AND BatchStatus NOT LIKE '%Cancelled'
GROUP BY MachineSite, BatchStatus
end

/* INKJET INFORMATION */

if @is_inkjetting = 1 begin
INSERT INTO #temp_data
SELECT 'Inkjet', MachineSite, SUM(BatchMailpacks), BatchStatus
FROM Streams st INNER JOIN
Batches b ON b.StreamID = st.StreamID INNER JOIN
BatchesInkjetDetails bmd ON b.BatchId = bmd.BatchId INNER JOIN
JobControl.dbo.MachineShifts ms ON ms.MachineShiftId = bmd.MachineShiftId INNER JOIN
JobControl.dbo.Inkjets i ON i.InkjetID = ms.MachineID INNER JOIN
JobControl.dbo.Status s ON b.BatchStatus = s.Status
WHERE Remakes = 0 AND st.StreamID = @stream_id AND IsInkjet = 1 AND MachineType like 'Inkjet%' AND BatchStatus NOT LIKE '%Cancelled'
GROUP BY MachineSite, BatchStatus
end

DECLARE site CURSOR FOR
SELECT DISTINCT Site
FROM #temp_data

OPEN site

DECLARE @site varchar(30)
DECLARE @print_qty int
DECLARE @print_complete int
DECLARE @mail_qty int
DECLARE @mail_qty_complete int
DECLARE @inkjet_qty int
DECLARE @inkjet_qty_complete int

FETCH NEXT FROM site INTO @site
WHILE @@fetch_status = 0
BEGIN

SELECT @print_qty = isnull(SUM(Qty), 0)
FROM #temp_data
WHERE MachineType = 'Printer'

SELECT @print_complete = isnull(SUM(Qty), 0)
FROM #temp_data td INNER JOIN
Status s ON s.Status = td.BatchStatus
WHERE MachineType = 'Printer' AND StatusLevel >= @laser_complete

SELECT @mail_qty = isnull(SUM(Qty), 0)
FROM #temp_data
WHERE MachineType LIKE 'Mail Machine%'

SELECT @mail_qty_complete = isnull(SUM(Qty), 0)
FROM #temp_data td INNER JOIN
Status s ON s.Status = td.BatchStatus
WHERE MachineType = 'Mail Machine' AND StatusLevel >= @mail_complete

SELECT @inkjet_qty = isnull(SUM(Qty), 0)
FROM #temp_data
WHERE MachineType = 'Inkjet'

SELECT @inkjet_qty_complete = isnull(SUM(Qty), 0)
FROM #temp_data td INNER JOIN
Status s ON s.Status = td.BatchStatus
WHERE MachineType = 'Inkjet' AND StatusLevel >= @inkjet_complete

INSERT INTO StreamTracking (
JobId,
StreamId,
StreamTotalImages,
StreamImagesComplete,
StreamTotalMailpacks,
StreamMailpacksComplete,
StreamTotalInkjetMailpacks,
StreamInkjetMailpacksComplete,
Site,
LastUpdated,
NationalJobId
)
VALUES (
@job_id,
@stream_id,
@print_qty,
@print_complete,
@mail_qty,
@mail_qty_complete,
@inkjet_qty,
@inkjet_qty_complete,
@site,
getdate(),
@national_job_id
)


FETCH NEXT FROM site INTO @site
END

CLOSE site
DEALLOCATE site

DROP TABLE #temp_data


GO


mk_garg
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2005-06-23 : 00:30:49
get rid of the cursor completely. go with temp tables instead. If you are having performance problems, the recompiles might be bad but the cursor is definitely going to be bad. change that to see if your performance improves.



-ec
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-06-23 : 00:38:50
i agree with droping cursor.
Can you suggest what will be code chage to drop cursor?

Thanks

mk_garg
Go to Top of Page
   

- Advertisement -