最近在使用doris,由于在执行存储过程中出现了很多异常意想不到的情况。我单独在库里创建一个表来记录存储过程执行情况。
一、创建存储过程执行记录表
doris中创建表
CREATE TABLE IF NOT EXISTS procedure_check_log (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`procedure_name` VARCHAR(1024) DEFAULT NULL COMMENT '执行存储过程名称',
`procedure_step` VARCHAR(1024) DEFAULT NULL COMMENT '执行存储过程步骤',
`log_text` VARCHAR(1024) DEFAULT NULL COMMENT '日志内容',
`log_time` DATETIME DEFAULT NULL COMMENT '执行时间'
)
UNIQUE KEY(`id`)
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 3"
);
如果是在mysql中,可以考虑把log_time的DEFAULT设置为now()。在doris中目前不支持。
二、插入数据
插入4条测试记录数据
-- 插入日志记录:开始插入员工记录
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'Before Insert',
'Starting to insert a new employee record',
'2024-10-14 10:00:00'
);
-- 插入日志记录:员工记录插入成功
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'After Insert',
'Employee record inserted successfully',
'2024-10-14 10:05:00'
);
-- 插入日志记录:开始插入员工记录
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'Before Insert',
'Starting to insert a new employee record',
NOW()
);
-- 插入日志记录:员工记录插入成功
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'After Insert',
'Employee record inserted successfully',
NOW()
);
三、创建存储过来进行数据插入
InsertProcedureCheckLog存储过程
该存储过程不传入时间,使用系统的now()作为时间参数。
DELIMITER $$
CREATE PROCEDURE InsertProcedureCheckLog (
IN p_procedure_name VARCHAR(1024),
IN p_procedure_step VARCHAR(1024),
IN p_log_text VARCHAR(1024)
)
BEGIN
INSERT INTO procedure_check_log (procedure_name, procedure_step, log_text, log_time)
VALUES (p_procedure_name, p_procedure_step, p_log_text, NOW() );
END $$
DELIMITER ;
InsertProcedureCheckLog2存储过程
该存储过程手动传入了时间,这里的参数是'2024-10-14 10:08:00'的字符串哦。
DELIMITER $$
CREATE PROCEDURE InsertProcedureCheckLog2 (
IN p_procedure_name VARCHAR(1024),
IN p_procedure_step VARCHAR(1024),
IN p_log_text VARCHAR(1024),
IN p_log_time DATETIME
)
BEGIN
INSERT INTO procedure_check_log (procedure_name, procedure_step, log_text, log_time)
VALUES (p_procedure_name, p_procedure_step, p_log_text, p_log_time );
END $$
DELIMITER ;
四、日入日志的语句就有如下
这样写入日志表就有以下4种方式,2个SQL插入,2个存储过程调用的方式。
INSERT INTO procedure_check_log (procedure_name, procedure_step, log_text, log_time) VALUES ('InsertEmployee', 'Before Insert', 'Starting to insert a new employee record', '2024-10-14 10:00:00');
INSERT INTO procedure_check_log (procedure_name, procedure_step, log_text, log_time) VALUES ('InsertEmployee', 'Before Insert', 'Starting to insert a new employee record', NOW() );
CALL InsertProcedureCheckLog('InsertEmployee', '步骤1', '日志内容1');
CALL InsertProcedureCheckLog2('InsertEmployee','步骤A', '日志内容A', '2024-10-14 10:08:00');
//下面这个是错误的哦。
CALL InsertProcedureCheckLog2('InsertEmployee','步骤B', '日志内容A', NOW());
//这里其实有错,写入了步骤B和日志内容A,但是没有把时间写入。
五、查询记录日志表数据
完整的SQL文本
CREATE TABLE IF NOT EXISTS procedure_check_log (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`procedure_name` VARCHAR(1024) DEFAULT NULL COMMENT '执行存储过程名称',
`procedure_step` VARCHAR(1024) DEFAULT NULL COMMENT '执行存储过程步骤',
`log_text` VARCHAR(1024) DEFAULT NULL COMMENT '日志内容',
`log_time` DATETIME DEFAULT NULL COMMENT '执行时间'
)
UNIQUE KEY(`id`)
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 3"
);
-- 插入日志记录:开始插入员工记录
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'Before Insert',
'Starting to insert a new employee record',
NOW()
);
-- 插入日志记录:员工记录插入成功
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'After Insert',
'Employee record inserted successfully',
NOW()
);
######################################################
-- 创建存储过程1
DELIMITER $$
CREATE PROCEDURE InsertProcedureCheckLog (
IN p_procedure_name VARCHAR(1024),
IN p_procedure_step VARCHAR(1024),
IN p_log_text VARCHAR(1024)
)
BEGIN
INSERT INTO procedure_check_log (procedure_name, procedure_step, log_text, log_time)
VALUES (p_procedure_name, p_procedure_step, p_log_text, NOW() );
END $$
DELIMITER ;
######################################################
-- 创建存储过程2
DELIMITER $$
CREATE PROCEDURE InsertProcedureCheckLog2 (
IN p_procedure_name VARCHAR(1024),
IN p_procedure_step VARCHAR(1024),
IN p_log_text VARCHAR(1024),
IN p_log_time DATETIME
)
BEGIN
INSERT INTO procedure_check_log (procedure_name, procedure_step, log_text, log_time)
VALUES (p_procedure_name, p_procedure_step, p_log_text, p_log_time );
END $$
DELIMITER ;
######################################################
-- 插入日志记录:开始插入员工记录
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'Before Insert',
'Starting to insert a new employee record',
'2024-10-14 10:00:00'
);
-- 插入日志记录:员工记录插入成功
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'After Insert',
'Employee record inserted successfully',
'2024-10-14 10:05:00'
);
-- 插入日志记录:开始插入员工记录
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'Before Insert',
'Starting to insert a new employee record',
NOW()
);
-- 插入日志记录:员工记录插入成功
INSERT INTO procedure_check_log (
procedure_name,
procedure_step,
log_text,
log_time
) VALUES (
'InsertEmployee',
'After Insert',
'Employee record inserted successfully',
NOW()
);
######################################################
-- 下面4执行插入日志的SQL语句
INSERT INTO procedure_check_log (procedure_name, procedure_step, log_text, log_time) VALUES ('InsertEmployee', 'Before Insert', 'Starting to insert a new employee record', '2024-10-14 10:00:00');
INSERT INTO procedure_check_log (procedure_name, procedure_step, log_text, log_time) VALUES ('InsertEmployee', 'Before Insert', 'Starting to insert a new employee record', NOW() );
CALL InsertProcedureCheckLog('InsertEmployee', '步骤1', '日志内容1');
CALL InsertProcedureCheckLog2('InsertEmployee','步骤A', '日志内容A', '2024-10-14 10:08:00');
//下面这个是错误的哦。
CALL InsertProcedureCheckLog2('InsertEmployee','步骤B', '日志内容A', NOW());
//这里其实有错,写入了步骤B和日志内容A,但是没有把时间写入。