从存储过程触发时,MySQL SELECT Query需要很长时间

时间:2013-09-19 17:16:50

标签: mysql sql select stored-procedures

我在mysql中有一个查询,它从单个表中获取数据。表包含450万行和所有必需的索引。

当我从SQLYog执行查询时,需要0.016秒。但是,当我将查询嵌入存储过程并执行存储过程时,需要34秒。

有人可以帮忙解释为什么这个程序需要很长时间吗?

有无程序的查询执行计划相同。

程序代码如下:

DELIMITER $$

USE `centraldb`$$

DROP PROCEDURE IF EXISTS `sa4_getparticipatingvehiclestset111`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sa4_getparticipatingvehiclestset111`(IN paramlicencetag VARCHAR(15),IN paramparticipantid INT)
BEGIN

SET @s = CONCAT('SELECT `LicenceTag`,`ParticipantId`,(SELECT `From_Datetime` FROM participant_licencetag WHERE `LicenceTag`=p.LicenceTag AND `Participant_Indicator`=\'DN\' AND ParticipantId = p.ParticipantId limit 1) AS dn_from_datetime,(SELECT `To_Datetime` FROM participant_licencetag WHERE `LicenceTag`=p.LicenceTag AND `Participant_Indicator`=\'DN\' AND ParticipantId = p.ParticipantId limit 1) AS dn_to_datetime,(SELECT `From_Datetime` FROM participant_licencetag WHERE `LicenceTag`=p.LicenceTag AND `Participant_Indicator`=\'ED\' AND ParticipantId = p.ParticipantId limit 1) AS ed_from_datetime,(SELECT `To_Datetime` FROM participant_licencetag WHERE `LicenceTag`=p.LicenceTag AND `Participant_Indicator`=\'ED\' AND ParticipantId = p.ParticipantId limit 1) AS ed_to_datetime
 FROM `participant_licencetag` p');

    PREPARE stmt2 FROM @s;
    EXECUTE stmt2;
    DEALLOCATE PREPARE stmt2;


END$$

DELIMITER ;

0 个答案:

没有答案