英文:
mysql trigger to keep track of how often a column was updated
问题
我有一列名为"processed_at"的表格。这个列可以从代码的多个地方被重置,以指示需要处理这一行的作业。我想知道如何将"processed_at"设置为null。
最简单的方法是什么?理想情况下,我想知道按行ID,这种情况发生多频繁,但也可以只知道在一定时间段内所有行的次数。
可以这样做吗:
创建一个触发器,以响应更新操作,然后将ID和重置时间戳存储到一个单独的表中?
这会对原始查询的性能产生明显影响吗?
英文:
I have a column "processed_at" on table. This can get reset from multiple places in the code in order to indicate to a job that this row needs to be processed. I would like to find out how processed_at is set to null.
What is the easiest way to do this? Ideally I would know how often this happens by row id, but it would also be ok to just know a number for all rows combined over a certain period.
Can this be done like this:
A trigger that reacts to the update and then stores id and reset timestamp to a separate table?
Would this have a noticeable effect on the performance of the original query?
答案1
得分: 0
create table mytable_resets (
id serial primary key,
mytable_id bigint unsigned not null,
reset_at datetime not null
);
delimiter ;;
create trigger t after update on mytable
for each row begin
if NEW.processed_at is null then
insert into mytable_resets values (default, NEW.id, NOW());
end if;
end;;
delimiter ;
Yes, it will impact the performance of the original query.
The cost of database writes is roughly proportional to the number of indexes it updates. If your query execute a trigger to insert into another table, it adds another index update. In this case, the primary key index of the mytable_resets
table.
But it shouldn't be significantly greater overhead than if your mytable
table had one more index.
英文:
Something like this:
create table mytable_resets (
id serial primary key,
mytable_id bigint unsigned not null,
reset_at datetime not null
);
delimiter ;;
create trigger t after update on mytable
for each row begin
if NEW.processed_at is null then
insert into mytable_resets values (default, NEW.id, NOW());
end if;
end;;
delimiter ;
Yes, it will impact the performance of the original query.
The cost of database writes is roughly proportional to the number of indexes it updates. If your query execute a trigger to insert into another table, it adds another index update. In this case, the primary key index of the mytable_resets
table.
But it shouldn't be significantly greater overhead than if your mytable
table had one more index.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论