0

我有以下存储过程:

CREATE PROCEDURE testProc(IN p_idProject INTEGER)
BEGIN
DECLARE nowTime DATETIME;
SET @nowTime = NOW();

     SELECT 
         idCustomer 
     FROM NextCalls WHERE @nowTime-nextCall = 
         (SELECT MAX(@nowTime - nextCall) 
          FROM NextCalls 
          WHERE idProject = p_idProject AND nextCall < @nowTime) 
     LIMIT 1;
END $$

列上设置了索引nextCallmysql-slow.log不幸的是,它作为未正确使用索引的查询被登录到文件中。这个过程非常非常频繁地使用,我很乐意避免错误的索引。是否可以重写以实现这一目标?

4

1 回答 1

0

您只是选择最旧的 nextCall。无需执行任何计算,您只需使用ORDER BY

SELECT idCustomer 
FROM NextCalls 
WHERE idProject = p_idProject AND nextCall < CURRENT_TIMESTAMP
ORDER BY nextCall
LIMIT 1
于 2012-06-29T09:45:42.500 回答