Hi All,
I have the below SQL Function
ALTER FUNCTION dbo.analosi
(@mmdoc varchar(10),@mmdocyear varchar (4),@UnitA varchar(3),@mmcat varchar(4))
RETURNS float
AS
BEGIN
declare @res int
set @res=0
if @unitA='KG'
select @res=SUM(QKG) FROM RPT.dbo.RPT_MMD
GROUP BY MMDoc, MMDocYear, StorLoc, MMCat
HAVING (MMDoc = @mmdoc) AND (MMDocYear = @mmdocyear) AND (MMCat = @mmcat)
if @unitA='ST'
select @res=SUM(QST) FROM RPT.dbo.RPT_MMD
GROUP BY MMDoc, MMDocYear, StorLoc, MMCat
HAVING (MMDoc = @mmdoc) AND (MMDocYear = @mmdocyear) AND (MMCat = @mmcat)
RETURN @res
END
I call the the dbo.analosi function from an Update SQL Statement.
UPDATE PERF_Performance
SET ANALOSI_KG = dbo.analosi(MMDoc, MMDocYear, 'KG', 'ANAL')
The table that the function reads has about 600000 records. The problem is that the total execution time of the Update SQL Statement for about 300 records is about 30 secs. The updated records may vary depending on user options. That means that may need to be updated for much more records and the total execution time will increase much more. Is there any tip to reduct the total execution time?
Thank you.
I have the below SQL Function
ALTER FUNCTION dbo.analosi
(@mmdoc varchar(10),@mmdocyear varchar (4),@UnitA varchar(3),@mmcat varchar(4))
RETURNS float
AS
BEGIN
declare @res int
set @res=0
if @unitA='KG'
select @res=SUM(QKG) FROM RPT.dbo.RPT_MMD
GROUP BY MMDoc, MMDocYear, StorLoc, MMCat
HAVING (MMDoc = @mmdoc) AND (MMDocYear = @mmdocyear) AND (MMCat = @mmcat)
if @unitA='ST'
select @res=SUM(QST) FROM RPT.dbo.RPT_MMD
GROUP BY MMDoc, MMDocYear, StorLoc, MMCat
HAVING (MMDoc = @mmdoc) AND (MMDocYear = @mmdocyear) AND (MMCat = @mmcat)
RETURN @res
END
I call the the dbo.analosi function from an Update SQL Statement.
UPDATE PERF_Performance
SET ANALOSI_KG = dbo.analosi(MMDoc, MMDocYear, 'KG', 'ANAL')
The table that the function reads has about 600000 records. The problem is that the total execution time of the Update SQL Statement for about 300 records is about 30 secs. The updated records may vary depending on user options. That means that may need to be updated for much more records and the total execution time will increase much more. Is there any tip to reduct the total execution time?
Thank you.