简体   繁体   English

使用MySQL触发器的优点/缺点

[英]Advantages/disadvantages of using MySQL triggers

What are the advantages/disadvantages of using MySQL triggers to keep data synced between one table and another and even update aggregate/report tables as data changes? 使用MySQL触发器保持数据在一个表和另一个表之间同步,甚至在数据更改时更新聚合/​​报告表有哪些优点/缺点?

By aggregate/report tables I mean tables that summarise data that exists in other tables such as: 通过聚合/报告表,我指的是汇总其他表中存在的数据的表,例如:

tbl_user_location:
1 Mike  New York
2 John  London
3 Emily Paris
4 Jack  New York

tbl_summary:
New York 2
London 1
Paris 1

As data changes real-time in tbl_user_location the summary is updated. 随着数据在tbl_user_location实时更改,摘要将更新。

What are alternatives to this approach? 这种方法有哪些替代方案?

In advantage : SQL triggers provide an alternative way to run scheduled tasks. 有利 :SQL触发器提供了另一种运行计划任务的方法。 triggers are invoked automatically before or after a change is made to the data in tables. 在对表中的数据进行更改之前或之后,将自动调用触发器。

In disadvantage : However, SQL triggers are invoked and executed invisibly from client-applications therefore it is difficult to figure out what happen in the database layer. 缺点 :但是,SQL触发器是从客户端应用程序中无形地调用和执行的,因此很难弄清楚数据库层中发生了什么。 This may increase the overhead of the database server. 这可能会增加数据库服务器的开销。

If you are talking about high velocity data don't even think about triggers. 如果你在谈论高速数据,甚至不考虑触发器。 I suggest you to go with stream processing technique. 我建议你选择流处理技术。 You can you mysql bin-logs to capture events and add to the stream and then process. 您可以使用mysql bin-logs捕获事件并添加到流中然后进行处理。 Simply triggers is not a good idea for real time data aggregation. 简单触发对于实时数据聚合来说不是一个好主意。

Try something like below, you might need to do some searching on these Ex: (mysql binlogs) maxwell->kafka->spark (processed data) 尝试类似下面的内容,您可能需要对这些Ex进行搜索:(mysql binlogs)maxwell-> kafka-> spark(已处理数据)

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

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