有没有办法在 MySQL 中的单独线程中运行触发器?
我有一个 PHP Web 应用程序,当插入表中时,我编写了一个触发器来计算一些分数,这需要时间。我的 PHP 应用程序等待触发器完成。 该触发器是否可以在单独的线程中运行,以便我的应用程序不会等待触发器完成?
I have a PHP web application and when inserted in a table, I have written a trigger to calculate some scores and this takes time. My PHP application waits till the trigger completes.
Can this trigger run in a separate thread such that my application will not wait for the completion of the trigger?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。
但正如比尔·克林顿所说,“我会告诉你原因。”
触发器的一大优点是触发器的操作包含在触发触发器的原始语句的事务中。从关系思维方式来看,这是一件好事,因为我们喜欢 ACID 合规性。
现在,如果您不需要 ACID 合规性,并且触发的操作“最终”生效是可以的,那么删除触发器并创建一个“操作”表。然后,触发器可以将某些内容放入操作表中,或者您的 PHP 代码也可以执行此操作。您可以让一个作业每秒查询该表一次左右,如果发现任何内容,就会执行它。不要担心守护进程的性能,对通常为空的表进行一次/秒的查询没什么意义。
No.
But as Bill Clinton would have said, "and I'll tell you why."
One of the big advantages of a trigger is that the actions of the trigger are contained within the transaction of the original statement that fired the trigger. This is a Good Thing from the Relational mindset because we like ACID compliance.
Now if you do not need that ACID compliance, and it is ok for the triggered actions to take effect "eventually", then remove the trigger and make an 'actions' table. Then either the trigger can drop something into the actions table or your PHP code might do it. You can have a job querying this table once/second or so, and if it finds anything it executes it. Don't worry about performance on the daemon, a once/second query of a table that is usually empty is nothing.