dongyanpai2701 2013-05-10 18:55
浏览 25
已采纳

何时使用存储过程和触发器与应用层

I have a dilemma, which I hope you will have some expert opinions on.

I have a table called CARDS with a column STATUS. If a record's status changes from 'download' to 'publish', I have to insert the record reference into another table called CARD_ASSIGNMENTS. Additionally, the record needs to be added into CARD_ASSIGNMENTS as many times as there are active records in SCANNERS.

In other words, if there are two active scanners, I will end up with two records in CARD_ASSIGNMENTS as below:

ID   CARD_ID   SCANNER_ID   STATUS_ID
1    1         1            4
2    1         2            4

My dilemma is that I'm not quite sure what would be the most efficient way to execute the above. I've considered the following options:

  1. From PHP - Do one UPDATE query and then the INSERT queries.
  2. Create a stored procedure, which will take care of updating the CARDS record and adding records into the CARD_ASSIGNMENTS. Then, just call that stored procedure from PHP.
  3. Create an ON UPDATE trigger for the CARDS table which will take care of processing INSERTS into the CARD_ASSIGNMENTS table.

PS. A simplified version of my database is available on MySQL Fiddle

Thanks,
Kate

  • 写回答

2条回答 默认 最新

  • douxiawei9318 2013-05-10 19:21
    关注

    Interesting question.

    I'm going to give you clues about how to approach the problem.

    So, you have to start by defining precisely three things:

    1. the expected functionality
    2. the access policy to the functionality
    3. the technical upgrade policy

    Here I'll detail these points.

    So, the first point is that you have to define your functionality. By doing so, you will be able to tell whether adding a card implies always, in all the possible paradigms (sorry for the pedantic word I can't find a more proper one) of your information system, that this card MUST exist in the other table according to the specifications you provided. This 1-1 functional link must be said TRUE or FALSE. This is really important. Said with other words, if there's at least one possibility that one day you don't want to copy that record to the other table, it means the trigger is a wrong solution, or at least it should be thought with an emergency mode (for example a variable inside that allows it to not get executed in some conditions) setup on.

    Then comes the second point, about the access policy. You have to know whether the allowed accessing systems will do so by using your application layer or if they could develop their own (SAAS style). If so, your php layer will be useless and the stored procedure is an excellent option, since every single technical and business layer will go trough it yes or yes.

    The last thing to know is whether you're possibly going to upgrade your php layer one day. In most of the cases the answer is yes. If so, you might have to modify the part containing this sql logic you're talking about. Then, having everything into a stored procedure vs storing it hardcoded into the php will definitely save you time, and improve stability.


    Left brain right brain, I'm going to tell you my personal opinion afterall. I really love going with stored procedures but not using any triggers. If the environment allows it, I would go for an underlying batch, calling a set of defined stored procedures, concentrating the activity outside of the online scope.

    The advantages are the following:

    • none or less risks of interruption of the online workflow since you reduce the number of operations
    • different schedule to alliviate the database load
    • more secure policy since executing the stored procedure requires only one grant, while using the same sql with php would require insert/update grants
    • better logging quality: you can have a log per job
    • better emergency response: when a job fails (if well thought) you can restart it, and that's it.

    Long post, but that was interesting and I really wanted to share these ideas.

    Cheers!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上