我在我的 sql 中编写了一个非常慢的存储过程。数据库中有百万条记录。
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FetchEnergyLine`(IN From_Time INT, IN
To_Time INT, IN Meter_Id INT, IN Device_Id VARCHAR(10), IN ct INT)
BEGIN
DECLARE c INT(5) default 0;
DECLARE Count INT default 1;
SET autocommit=0;
SET @c=0;
SET Count = (SELECT COUNT(TimeStamp) FROM Meter_Data
WHERE
TimeStamp > From_Time
AND TimeStamp < To_Time
AND MeterID = Meter_Id
AND DeviceID = Device_Id );
IF Count > (2*ct) THEN SET Count=Count/ct;
ELSE SET COUNT = 20;
END IF;
SELECT * FROM ( SELECT TimeStamp, FwdHr, W , @c:=@c+1 as counter
FROM
Meter_Data
WHERE
TimeStamp > From_Time
AND TimeStamp < To_Time
AND MeterID = Meter_Id
AND DeviceID = Device_Id ORDER BY TimeStamp
) as tmp
WHERE
counter % Count =1;
END
我认为当我的数据较少时速度非常快,但我使用相同数据库的其他查询运行良好,但存储过程很慢。1)它可以是计数语句,它在开始时计算读数的数量,但我不太确定。
有人可以帮忙吗??提前致谢..