我创建了一个非常复杂的存储过程,用于检查c_sent_messages
表中是否存在c_passwords
表中的密码。然后,它输入一些消息并更新数据库中的记录。最后,它正在从c_sent_messages
到archive
表中输入记录。在我将添加记录的两行添加到archive
并从c_sent_messages
删除消息之前,一切都很好(不增加存储空间)
INSERT INTO archive SELECT *, NOW() FROM c_sent_messages WHERE id=idi;
DELETE FROM c_sent_messages WHERE id=idi;
我的处理器也是一个很好的处理器(Intel 7700 HQ),我没有运行任何其他软件/进程。
在添加这两行之后,该过程花费了太多时间才能将记录添加到archive
表中。我想最小化此过程的处理时间。有什么方法可以使其更快? (向archive
添加1000条记录需要120秒)
DELIMITER $$
CREATE PROCEDURE flood ()
BEGIN
DECLARE i INT DEFAULT 0;
declare ifExist int DEFAULT 0;
DECLARE numara VARCHAR(10);
declare adamin_gonderdigi varchar(150);
declare idi int default 0;
myloop: LOOP
SET idi = (SELECT id from c_sent_messages where stats = 'N' limit 1);
SET numara = (SELECT sender from c_sent_messages where id = idi);
SET adamin_gonderdigi = (SELECT msg_text from c_sent_messages WHERE id = idi);
select count(1) into ifExist from c_passwords b WHERE adamin_gonderdigi = b.pass;
IF ifExist > 0 THEN
set ifExist = 0;
select count(1) into ifExist from c_sent_messages a WHERE a.stats ="N";
IF ifExist > 0 THEN
INSERT INTO c_received_messages ( receiver, sender, msg_text, stats, x_date )
VALUES
(numara, "5757", "Code is received successfully.", "S",NOW());
UPDATE c_passwords a SET a.stats = "S", a.x_date = NOW()
WHERE pass = adamin_gonderdigi;
update c_sent_messages set stats = 'S' where id = idi;
ELSE
INSERT INTO c_received_messages ( receiver, sender, msg_text, stats, x_date )
VALUES(numara,"5757","Code is used before!","S",NOW());
update c_sent_messages set stats = 'U' where id = idi;
END IF;
ELSE
INSERT INTO c_received_messages ( receiver, sender, msg_text, stats, x_date )
VALUES(numara,"5757","Please send a valid code!","S",NOW());
update c_sent_messages set stats = 'F' where id = idi;
END IF;
INSERT INTO archive SELECT *, NOW() FROM c_sent_messages WHERE id = idi;
DELETE FROM c_sent_messages WHERE id=idi;
SET i = i + 1;
IF
i = 900000 THEN
LEAVE myloop;
END IF;
END LOOP myloop;
END $$
DELIMITER;
答案 0 :(得分:0)
SELECT
完成-参见JOIN
。INDEX(stats, id)
吗?