I am running event to fetch data from main table and inserting it into Summary
table
I am running MySQL event every 2 minute and calling stored procedure inside my event for some grouping, parsing and calculations from main table to summary table.
But while selecting 2 minutes records, some time last record got missed while creating summary occasionally
Frequency of missing record is in 1 day 90-100 records (1 day record count in main table is 30K).
MySQL select query inside stored procedure:
SELECT ID, COUNT(*) AS TOT_COUNT
FROM CUSTOMER
WHERE (TIMESTAMP > (DATE_FORMAT((NOW() - INTERVAL 3 MINUTE),"%Y-%m-%d
%H:%i:00"))
AND TIMESTAMP <= (DATE_FORMAT((NOW() - INTERVAL 1 MINUTE),"%Y-%m-%d %H:%i:00")))
GROUP BY ID, NAME;
Note: selecting previous 2 minutes record, skipping current minute records
I have tried updating select statement where condition as below (missing record frequency reduced to 50%)
SELECT ID, NAME, COUNT(*) AS TOT_COUNT
FROM CUSTOMER
WHERE (SUBSTRING_INDEX(TIMESTAMP, ':', 2) != SUBSTRING_INDEX((NOW()), ':', 2)
AND SUBSTRING_INDEX(TIMESTAMP,':',2) >= SUBSTRING_INDEX((NOW() - INTERVAL 2 MINUTE),':',2))
GROUP BY ID,NAME;
Also try to catch exception by using below statement in stored procedure.
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE,
@errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
SET @full_error = CONCAT("ERROR ", @errno, " (", @sqlstate, "): ", @text);
SELECT @full_error;
INSERT INTO ERRORS_TABLE (Level,Code,Message,INSERT_TMST) VALUES ("ERROR ", @errno, @full_error, now());
END;
No event or stored procedure fail log or MySQL error log found. also checked for null values.
If I call the stored procedure manually for that missed minute record set manually then it returns the correct count.
Can you please help me how can I debug this issue, any other perfect select query to fetch 2 minutes record or i am missing something ?
Thanks in advance