I have following stored procedure:
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 $$
There is index set on nextCall column. Unfortunately it’s being logged into mysql-slow.log file as a query that is not using indexes properly. This procedure is used very, very often and I would be really happy to avoid bad indexing. Is it possible to rewrite to achieve that?
You are just selecting the oldest nextCall. There is no need to perform any calculations, you can just use
ORDER BY: