[英]Stored procedure using cursor in mySql
我在mysql中使用游標編寫了一個存儲過程,但該過程需要10秒才能獲取結果,而該結果集只有450條記錄,所以我想知道為什么這個程序需要花費那么多時間來獲取記錄。
程序如下:
DELIMITER //
DROP PROCEDURE IF EXISTS curdemo123//
CREATE PROCEDURE curdemo123(IN Branchcode int,IN vYear int,IN vMonth int)
BEGIN
DECLARE EndOfData,tempamount INT DEFAULT 0;
DECLARE tempagent_code,tempplantype,tempsaledate CHAR(12);
DECLARE tempspot_rate DOUBLE;
DECLARE var1,totalrow INT DEFAULT 1;
DECLARE cur1 CURSOR FOR
select SQL_CALC_FOUND_ROWS ad.agentCode
, ad.planType
, ad.amount
, ad.date
from adplan_detailstbl ad
where ad.branchCode=Branchcode
and ( ad.date between '2009-12-1'
and '2009-12-31')
order by ad.NUM_ID asc;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET EndOfData = 1;
DROP TEMPORARY TABLE IF EXISTS temptable;
CREATE TEMPORARY TABLE temptable (
agent_code varchar(15)
, plan_type char(12)
, sale double
, spot_rate double default '0.0', dATE DATE
);
OPEN cur1;
SET totalrow=FOUND_ROWS();
while var1 <= totalrow DO
fetch cur1 into tempagent_code
,tempplantype
,tempamount
,tempsaledate;
IF( ( tempplantype='Unit Plan'
OR tempplantype='MIP'
) OR tempplantype='STUP') THEN
select spotRate into tempspot_rate
from spot_amount
where (( monthCode=vMonth
and year=vYear)
and ( ( agentCode=tempagent_code
and branchCode=Branchcode)
and (planType=tempplantype)
));
INSERT INTO temptable VALUES(
tempagent_code
,tempplantype
,tempamount
,tempspot_rate
,tempsaledate)
;
else
INSERT INTO temptable(
agent_code
,plan_type
,sale,dATE)
VALUES( tempagent_code,tempplantype,tempamount,tempsaledate);
END IF;
SET var1=var1+1;
END WHILE;
CLOSE cur1;
select * from temptable;
DROP TABLE temptable;
END //
DELIMITER ;
請運行以下內容並將所有結果粘貼到http://pastie.org/您應該替換?? 在執行前具有適當測試值的select語句中
show table status like 'adplan_detailstbl'\G
show create table adplan_detailstbl \G
show indexes from adplan_detailstbl;
explain select
ad.*
from
adplan_detailstbl ad
where
ad.branchCode = ?? and ad.date between '2009-12-01' and '2009-12-31'
order by
ad.NUM_ID;
show table status like 'spot_amount'\G
show create table spot_amount \G
show indexes from spot_amount;
explain select * from
spot_amount
where
monthCode=?? and year=?? and agentCode=?? and branchCode=?? and planType=??;
游標的本質是緩慢的,你的代碼中沒有任何東西顯然是糟糕的設計。 請記住,對於您的450行中的每一行,您的curosr迭代您發出一個或兩個語句。 每個查詢都將自行運行。 像這樣發表450-900這樣的聲明可能會花費相同的時間。
這就是為什么你應該盡可能地避免使用游標。
看起來你想要,根據一定的標准,選擇一組ov值或另一組。 也許你將查詢分成兩個選擇查詢(對於每個條件的情況)和UNION結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.