简体   繁体   English

如何在 MySQL 中获取存储过程的执行历史

[英]How to get execution history of a stored procedure in MySQL

I have created a stored procedure in MySQL named: inventory_procedure(fromDateTime)我在 MySQL 中创建了一个存储过程,名为:inventory_procedure(fromDateTime)

A third party system executes this stored procedure at regular 1 hour interval by passing fromDateTime in dd-mm-yyyy hh:mm:ss format.第三方系统通过以 dd-mm-yyyy hh:mm:ss 格式传递 fromDateTime 以固定的 1 小时间隔执行此存储过程。

Sometimes, third party systems reports that stored procedure didn't returned any data.有时,第三方系统报告存储过程没有返回任何数据。

When I hit the stored procedure it returns results for me.当我点击存储过程时,它会为我返回结果。

is there any way where I can fetch information like:有什么方法可以获取如下信息:

  1. When stored procedure was executed?什么时候执行存储过程?
  2. What was the fromDateTime passed in storedProcedure?在 storedProcedure 中传递的 fromDateTime 是什么?
  3. How many rows stored procedure returned?存储过程返回了多少行?

Create a log table like this one:创建一个像这样的日志表:

create table inventory_procedure_log (
  id int unsigned auto_increment primary key,
  ts timestamp,
  fromDateTime datetime,
  rows_returned int
);

You will probably want to include some indexes for fast searches.您可能希望包含一些索引以进行快速搜索。

Insert the log entries within your procedure right after the final SELECT:在最终的 SELECT 之后在您的过程中插入日志条目:

create procedure inventory_procedure (fromDateTime datetime)
begin
  -- random dummy result set
  select n
  from (select 1 n union select 2 union select 3) as x
  where rand() < 0.5;
  
  -- insert log entry
  insert into inventory_procedure_log(ts, fromDateTime, rows_returned) values (
    now(),
    fromDateTime,
    found_rows()
  );
end

Now you can search in your log table.现在您可以在日志表中进行搜索。 For example - show all entries for the last 24 hours:例如 - 显示过去 24 小时的所有条目:

select *
from inventory_procedure_log
where ts > now() - interval 24 hour;

Se demo on dbfiddle.ukdbfiddle.uk上查看演示

You can extend the log table for more info (like session ID) as needed.您可以根据需要扩展日志表以获取更多信息(例如 session ID)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM