我在 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 ;