Mysql存储过程优化——使用临时表代替游标(转)
Mysql游标在操作小数据量时比较方便,效率可观,但操作大数据量,速度比较慢,甚至直接产生系统错误。
一般说来,当操作的数据超过1万条时,就避免用游标吧。
为了测试游标性能,写了下面一个游标对IDC_Gather_Info表中数据进行遍历
View Code
CREATE DEFINER=`root`@`%` PROCEDURE `debug`(IN `beginTime` int, IN `checkTime` int) BEGIN DECLARE t_id VARCHAR(64) DEFAULT ''; DECLARE t_item TINYINT DEFAULT 0; DECLARE t_result VARCHAR(8192) DEFAULT ''; DECLARE cursorDone INT DEFAULT 0; DECLARE cur CURSOR FOR SELECT Asset_Id, Check_Item, Check_Result from IDC_Gather_Info WHERE Check_Time > beginTime AND Check_Time <= checkTime; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET cursorDone = 1; OPEN cur; cursorLoop:LOOP FETCH cur INTO t_id, t_item, t_result; IF cursorDone = 1 THEN LEAVE cursorLoop; END IF; END LOOP; CLOSE cur; END
1.数据量15万,存储过程执行失败,提示错误:Incorrect key file for table '/tmp/#sql_3044_0.MYI';try to repair it
2.数据量5万,执行成功,耗时31.051s
3.数据量1万,执行成功,耗时1.371s
下面使用临时表替换游标:1.数据量15万,执行成功,耗时8.928s
View Code
CREATE DEFINER=`root`@`%` PROCEDURE `debug`(IN `beginTime` int, IN `checkTime` int) BEGIN DECLARE t_id VARCHAR(64) DEFAULT ''; DECLARE t_item TINYINT DEFAULT 0; DECLARE t_result VARCHAR(8192) DEFAULT ''; DECLARE maxCnt INT DEFAULT 0; DECLARE i INT DEFAULT 0; DROP TABLE IF EXISTS Gather_Data_Tmp; CREATE TEMPORARY TABLE Gather_Data_Tmp( `Tmp_Id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `Asset_Id` VARCHAR(16) NOT NULL, `Check_Item` TINYINT(1) NOT NULL, `Check_Result` VARCHAR(8192) NOT NULL, PRIMARY KEY (`Tmp_Id`) )ENGINE=MyISAM DEFAULT CHARSET=utf8; SET @tSql = CONCAT('INSERT INTO Gather_Data_Tmp (`Asset_Id`, `Check_Item`, `Check_Result`) SELECT Asset_Id, Check_Item, Check_Result FROM IDC_Gather_Info WHERE Check_Time > ',beginTime,' AND Check_Time <= ',checkTime); PREPARE gatherData FROM @tSql; EXECUTE gatherData; SELECT MIN(`Tmp_Id`) INTO i FROM Gather_Data_Tmp; SELECT MAX(`Tmp_Id`) INTO maxCnt FROM Gather_Data_Tmp; WHILE i <= maxCnt DO SELECT Asset_Id, Check_Item, Check_Result INTO t_id, t_item, t_result FROM Gather_Data_Tmp WHERE Tmp_Id = i; SET i = i + 1; END WHILE; END
2.数据量5万,执行成功,耗时2.994s
3.数据量1万,执行成功,耗时0.634s
可以看到Mysql的游标在处理大一点的数据量时还是比较乏力的,仅适合用于操作几百上千的小数据量。