PostgreSQL - BEFORE 触发器比 AFTER 触发器更有效吗?
我刚刚阅读了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
PostgreSQL
的MVCC
架构,每次操作都会增加系统中记录的数据量,甚至是DELETE
。因此,如果您只需要检查输入并在检查失败时回滚事务,那么最好在保存输入数据之前执行此操作。
Due to
PostgreSQL
'sMVCC
architecture, each operation increases the amount of data recorded in the system, evenDELETE
.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.
对于更新触发器,我发现我的系统上没有可测量的差异:
使用“之前”触发器:
使用“之后”触发器:
但由于某种原因,我在使用“之前”时得到了非常明显的降级 >插入触发器与“之后”插入触发器或控件相比
For an update trigger, I found no measurable difference on my system:
with 'before' trigger:
with 'after' trigger:
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
你要以这种或那种方式证明它的唯一方法就是测试它,看看它对你正在做的事情是否重要。
从高层次进行逻辑思考……如果您采取额外的步骤来保留更多信息,而不是不采取额外的步骤,那么当然其中一个比另一个需要更多的工作。就像多走一步就会增加工作量一样,尽管这可能不会花费您明显的时间差异。例如,用 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.