Subject | Stored Procedure & Plan & Temporary Table |
---|---|
Author | Tanz Anthrox |
Post date | 2004-02-27T06:59:22Z |
Hi!,
I have a SP that executes a different SP so many times. Performance is not good enough.
And, I wish to learn what are the possible (best) practises about this?
I added an example.Let me first explain
when I run SP_RUNTHIS procedure it calls SP_RUNMANYTIMES(:begindate,:enddate) procedure so many times.
when I check the SP_RUNTHIS it reads (50K records) but SP_RUNMANYTIMES reads ove (2.3 million records). Note row count in tables are not more that 50K.
I do not want to create a temporary table and store SP_RUNMANYTIMES result in it?
I think I need to solve this by using PLAN.
So, If you were me, what would you do?
For Ex :
CREATE PROCEDURE SP_RUNMANYTIMES (
BASLATARIH DATE,
BITISTARIH DATE)
RETURNS (
MUH_HESAPAD VARCHAR (50),
GIDERTOPLAM DOUBLE PRECISION,
GELIRTOPLAM DOUBLE PRECISION,
TIP VARCHAR (10))
AS
BEGIN
/* GIDERLER ALINIYOR */
FOR
SELECT M1.MUH_HAREKETKODU,SUM(M1.MUH_MIKTAR) AS GIDERTOPLAM
FROM MUHASEBE_MIKTARLAR_ISARETLI M1
JOIN HAREKETKODLARI HK1
ON HK1.HAREKETKODU = M1.MUH_HAREKETKODU
WHERE
(HK1.HAREKETKODU_TIPI = 'GIDER')
AND
(M1.MUH_TARIH BETWEEN :BASLATARIH AND :BITISTARIH)
GROUP BY M1.MUH_HAREKETKODU
INTO
:MUH_HESAPAD, :GIDERTOPLAM
DO
BEGIN
TIP = 'GIDER';
GELIRTOPLAM = NULL;
SUSPEND;
END
END
CREATE PROCEDURE SP_RUNTHIS
RETURNS (
MUH_HESAPAD VARCHAR (50),
HAREKETKODU VARCHAR (50),
PARENT VARCHAR (50),
GIDERTOPLAM DOUBLE PRECISION,
GELIRTOPLAM DOUBLE PRECISION,
TIP VARCHAR (10))
AS
declare variable TIPI VARCHAR(10);
BEGIN
FOR
select
HS.HESAPLAR_HESAPAD,
HK.HAREKETKODU,
HS.HESAPLAR_PARENT,
HK.HAREKETKODU_TIPI
FROM HESAPLAR HS
LEFT OUTER JOIN hareketkodlari HK
ON HS.HESAPLAR_HESAPAD = HK.HAREKETKODU_HESAPAD
WHERE (HS.HESAPLAR_HESAPAD IN (SELECT HESAPAD FROM sp_hesaplar_talikodlar))
INTO
:MUH_HESAPAD, :HAREKETKODU, :PARENT, :TIPI
DO
BEGIN
if (TIPI ='GELIR') then
BEGIN
SELECT SP.GELIRTOPLAM
FROM SP_RUNMANYTIMES('1.1.2003','1.5.2003') SP /******************* HERE !!!!!!!!!!!!!!! **************************/
WHERE (SP.MUH_HESAPAD = :HAREKETKODU)
INTO :GELIRTOPLAM;
END ELSE GELIRTOPLAM=NULL;
if (TIPI ='GIDER') then
BEGIN
SELECT SP.GIDERTOPLAM
FROM SP_RUNMANYTIMES('1.1.2003','1.5.2003') SP /******************* HERE !!!!!!!!!!!!!!! **************************/
WHERE (SP.MUH_HESAPAD = :HAREKETKODU)
INTO :GIDERTOPLAM;
END ELSE GIDERTOPLAM=NULL;
SUSPEND;
END
END
Best Regards,
[Non-text portions of this message have been removed]
I have a SP that executes a different SP so many times. Performance is not good enough.
And, I wish to learn what are the possible (best) practises about this?
I added an example.Let me first explain
when I run SP_RUNTHIS procedure it calls SP_RUNMANYTIMES(:begindate,:enddate) procedure so many times.
when I check the SP_RUNTHIS it reads (50K records) but SP_RUNMANYTIMES reads ove (2.3 million records). Note row count in tables are not more that 50K.
I do not want to create a temporary table and store SP_RUNMANYTIMES result in it?
I think I need to solve this by using PLAN.
So, If you were me, what would you do?
For Ex :
CREATE PROCEDURE SP_RUNMANYTIMES (
BASLATARIH DATE,
BITISTARIH DATE)
RETURNS (
MUH_HESAPAD VARCHAR (50),
GIDERTOPLAM DOUBLE PRECISION,
GELIRTOPLAM DOUBLE PRECISION,
TIP VARCHAR (10))
AS
BEGIN
/* GIDERLER ALINIYOR */
FOR
SELECT M1.MUH_HAREKETKODU,SUM(M1.MUH_MIKTAR) AS GIDERTOPLAM
FROM MUHASEBE_MIKTARLAR_ISARETLI M1
JOIN HAREKETKODLARI HK1
ON HK1.HAREKETKODU = M1.MUH_HAREKETKODU
WHERE
(HK1.HAREKETKODU_TIPI = 'GIDER')
AND
(M1.MUH_TARIH BETWEEN :BASLATARIH AND :BITISTARIH)
GROUP BY M1.MUH_HAREKETKODU
INTO
:MUH_HESAPAD, :GIDERTOPLAM
DO
BEGIN
TIP = 'GIDER';
GELIRTOPLAM = NULL;
SUSPEND;
END
END
CREATE PROCEDURE SP_RUNTHIS
RETURNS (
MUH_HESAPAD VARCHAR (50),
HAREKETKODU VARCHAR (50),
PARENT VARCHAR (50),
GIDERTOPLAM DOUBLE PRECISION,
GELIRTOPLAM DOUBLE PRECISION,
TIP VARCHAR (10))
AS
declare variable TIPI VARCHAR(10);
BEGIN
FOR
select
HS.HESAPLAR_HESAPAD,
HK.HAREKETKODU,
HS.HESAPLAR_PARENT,
HK.HAREKETKODU_TIPI
FROM HESAPLAR HS
LEFT OUTER JOIN hareketkodlari HK
ON HS.HESAPLAR_HESAPAD = HK.HAREKETKODU_HESAPAD
WHERE (HS.HESAPLAR_HESAPAD IN (SELECT HESAPAD FROM sp_hesaplar_talikodlar))
INTO
:MUH_HESAPAD, :HAREKETKODU, :PARENT, :TIPI
DO
BEGIN
if (TIPI ='GELIR') then
BEGIN
SELECT SP.GELIRTOPLAM
FROM SP_RUNMANYTIMES('1.1.2003','1.5.2003') SP /******************* HERE !!!!!!!!!!!!!!! **************************/
WHERE (SP.MUH_HESAPAD = :HAREKETKODU)
INTO :GELIRTOPLAM;
END ELSE GELIRTOPLAM=NULL;
if (TIPI ='GIDER') then
BEGIN
SELECT SP.GIDERTOPLAM
FROM SP_RUNMANYTIMES('1.1.2003','1.5.2003') SP /******************* HERE !!!!!!!!!!!!!!! **************************/
WHERE (SP.MUH_HESAPAD = :HAREKETKODU)
INTO :GIDERTOPLAM;
END ELSE GIDERTOPLAM=NULL;
SUSPEND;
END
END
Best Regards,
[Non-text portions of this message have been removed]