0
我寫了一個存儲過程,在我的sql中很慢。數據庫中有百萬條記錄。Mysql存儲過程太慢
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:[email protected]+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)它可以是開始計數讀數的計數語句,但我不太確定。
任何人都可以幫忙嗎?在此先感謝..
執行計劃說什麼? – phadaphunk
聽起來像你可能需要一個或兩個索引。 – twoleggedhorse
我是新的與我的SQL。什麼是執行計劃?我在哪裏可以看到? –