PostgreSQL - BEFORE 触发器比 AFTER 触发器更有效吗?

发布于 2024-10-27 04:36:41 字数 313 浏览 2 评论 0原文

我刚刚阅读了 PostgreSQL 文档 - 触发器行为概述, BEFORE 触发器比 AFTER 触发器“更有效”:

如果您没有具体原因 之前或之后的触发器,之前 情况更有效,因为 有关操作的信息 不必保存到结束 声明。

我不明白这是否属实或这对我意味着什么。有人可以启发我吗?这只是顺势疗法的性能改进吗?

I just read in the PostgreSQL Documentation - Overview of Trigger behavior, that BEFORE triggers are "more effecient" than AFTER triggers:

If you have no specific reason to make
a trigger before or after, the before
case is more efficient, since the
information about the operation
doesn't have to be saved until end of
statement.

I do not understand if this is true or what it means for me. Can someone enlighten me? Is this just a homeopatic performance improvement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无法回应 2024-11-03 04:36:41

由于PostgreSQLMVCC架构,每次操作都会增加系统中记录的数据量,甚至是DELETE

因此,如果您只需要检查输入并在检查失败时回滚事务,那么最好在保存输入数据之前执行此操作。

Due to PostgreSQL's MVCC architecture, each operation increases the amount of data recorded in the system, even DELETE.

So if you just need to make checks of your input and rollback the transaction if the checks fail, you better do it before the input data are saved.

夏末染殇 2024-11-03 04:36:41

对于更新触发器,我发现我的系统上没有可测量的差异:

使用“之前”触发器:

begin;

create function f() returns trigger language plpgsql as $
begin 
  new.time_of_day:=old.time_of_day+'1d'::interval; 
  return new; 
end;$;

create table t(time_of_day timestamp);

insert into t(time_of_day)
select timeofday()::timestamp from generate_series(1,100000);

update t set time_of_day = timeofday()::timestamp;

select max(time_of_day)-min(time_of_day) from t;

    ?column?
-----------------
 00:00:47

create trigger trig before insert on t for each row execute procedure f();

update t set time_of_day = timeofday()::timestamp;

select max(time_of_day)-min(time_of_day) from t;

    ?column?
-----------------
 00:00:47.432173

rollback;

使用“之后”触发器:

create function f() returns trigger language plpgsql as $
begin 
  new.time_of_day:=old.time_of_day+'1d'::interval; 
  return new; 
end;$;

create table t(time_of_day timestamp);

insert into t(time_of_day)
select timeofday()::timestamp from generate_series(1,100000);

update t set time_of_day = timeofday()::timestamp;

select max(time_of_day)-min(time_of_day) from t;

    ?column?
-----------------
 00:00:48.566558

create trigger trig after insert on t for each row execute procedure f();

update t set time_of_day = timeofday()::timestamp;

select max(time_of_day)-min(time_of_day) from t;

    ?column?
-----------------
 00:00:48.922441

但由于某种原因,我在使用“之前”时得到了非常明显的降级 >插入触发器与“之后”插入触发器或控件相比

For an update trigger, I found no measurable difference on my system:

with 'before' trigger:

begin;

create function f() returns trigger language plpgsql as $
begin 
  new.time_of_day:=old.time_of_day+'1d'::interval; 
  return new; 
end;$;

create table t(time_of_day timestamp);

insert into t(time_of_day)
select timeofday()::timestamp from generate_series(1,100000);

update t set time_of_day = timeofday()::timestamp;

select max(time_of_day)-min(time_of_day) from t;

    ?column?
-----------------
 00:00:47

create trigger trig before insert on t for each row execute procedure f();

update t set time_of_day = timeofday()::timestamp;

select max(time_of_day)-min(time_of_day) from t;

    ?column?
-----------------
 00:00:47.432173

rollback;

with 'after' trigger:

create function f() returns trigger language plpgsql as $
begin 
  new.time_of_day:=old.time_of_day+'1d'::interval; 
  return new; 
end;$;

create table t(time_of_day timestamp);

insert into t(time_of_day)
select timeofday()::timestamp from generate_series(1,100000);

update t set time_of_day = timeofday()::timestamp;

select max(time_of_day)-min(time_of_day) from t;

    ?column?
-----------------
 00:00:48.566558

create trigger trig after insert on t for each row execute procedure f();

update t set time_of_day = timeofday()::timestamp;

select max(time_of_day)-min(time_of_day) from t;

    ?column?
-----------------
 00:00:48.922441

But for some reason I get a very noticeable degradation with a 'before' insert trigger as compared to an 'after' insert trigger or a control

梦萦几度 2024-11-03 04:36:41

你要以这种或那种方式证明它的唯一方法就是测试它,看看它对你正在做的事情是否重要。

从高层次进行逻辑思考……如果您采取额外的步骤来保留更多信息,而不是不采取额外的步骤,那么当然其中一个比另一个需要更多的工作。就像多走一步就会增加工作量一样,尽管这可能不会花费您明显的时间差异。例如,用 10 步步行 10 英尺与用 11 步步行。

The only way you are going to prove it one way or another is to test it and see if it matters for what you are doing.

Thinking logically at the high level... if you are taking an extra step to retain more information vs not taking the extra step of course one is more work than the other. Just as walking one extra step is more work even though it may not take you a noticeable time difference. For example to walk 10 feet with 10 steps vs 11 steps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文