我有一个存储过程,每天对cron作业执行一次。存储过程由多个SQL查询触发,以便根据我们从其他来源获得的每日更新来完成数据协调。
我的目标是捕获在存储过程中执行的每个SQL查询的START_TIME和END_TIME,以便我可以分析和优化该过程。
CREATE TABLE `SP_LOG` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`SP_NAME` varchar(255) NOT NULL,
`IN_PARAMS` varchar(4000) DEFAULT NULL,
`START_TIME` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`END_TIME` timestamp(6) NULL DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `IDX.SP_LOG.SP_NAME` (`SP_NAME`)
);
CREATE TABLE `SP_LOG_DETAIL` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`SP_ID` bigint(20) NOT NULL,
`MESSAGE` varchar(255) NOT NULL,
`START_TIME` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`END_TIME` timestamp(6) NULL DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `IDX.SP_LOG_DETAIL.SP_ID` (`SP_ID`),
CONSTRAINT `FK.SP_LOG_DETAIL.SP_ID` FOREIGN KEY (`SP_ID`)
REFERENCES `SP_LOG` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
);
DROP PROCEDURE IF EXISTS `SP_PROCESS_USERS`;
DELIMITER //
CREATE PROCEDURE `SP_PROCESS_USERS`(IN spLocation VARCHAR(60))
BEGIN
/*
* -- Handle Exception --
*/
DECLARE spLogId BIGINT(20);
DECLARE spLogDetailId BIGINT(20);
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1 @p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
INSERT INTO SP_LOG(SP_NAME, IN_PARAMS, END_TIME) VALUES ('SP_PROCESS_USERS', spMarket, NOW(6));
SELECT LAST_INSERT_ID() INTO spLogId;
INSERT INTO SP_LOG_DETAIL(SP_ID, MESSAGE, END_TIME) VALUES (spLogId, CONCAT('SQLEXCEPTION==>', @p1, ':', @p2), NOW(6));
END;
INSERT INTO SP_LOG(SP_NAME, IN_PARAMS) VALUES ('SP_PROCESS_USERS', spLocation);
SELECT LAST_INSERT_ID() INTO spLogId;
/*
* -- Process Users Part 1 --
*/
INSERT INTO SP_LOG_DETAIL(SP_ID, MESSAGE) VALUES (spLogId, 'Process Users #1');
SELECT LAST_INSERT_ID() INTO spLogDetailId;
... Logic ...
UPDATE SP_LOG_DETAIL SET END_TIME = NOW(6) WHERE ID = spLogDetailId;
... Part 2, 3, ... n
UPDATE SQ_5G_SP_LOG SET END_TIME = NOW(6) WHERE ID = spLogId;
END //
当我将日志插入表中时,该表会自动生成CURRENT_TIMESTAMP并获取准确的时间。
在使用NOW(6)完成处理后,当我尝试使用CURRENT_TIMESTAMP更新现有记录时,总是会找到与START_TIME已捕获的相同值;下降到毫秒。
如何找到准确的时间?