加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

为什么MySQL存储过程、函数和触发器对性能不好

发布时间:2018-08-19 15:53:15 所属栏目:MySql教程 来源:佚名
导读:副标题#e# 技术沙龙 | 邀您于8月25日与国美/AWS/转转三位专家共同探讨小程序电商实战 MySQL存储过程、函数和触发器是应用程序开发人员的诱人构造。但是,正如我所发现的,使用MySQL存储例程会影响数据库性能。由于不能完全确定在客户访问期间看到了什么,我

4.The sys.ps_trace_thread collected the data (for 10 seconds, during which I ran the ), then it finished its collection and created the dot file:

  1. +-----------------------------------------------------------------------+  
  2. | Info                                                                  |  
  3. +-----------------------------------------------------------------------+  
  4. | Stack trace written to /var/lib/mysql-files/stack-func3nope-new12.dot |  
  5. +-----------------------------------------------------------------------+ 
  6. 1 row in set (9.21 sec)  
  7. +-------------------------------------------------------------------------------+  
  8. | Convert to PDF                                                                |  
  9. +-------------------------------------------------------------------------------+  
  10. | dot -Tpdf -o /tmp/stack_49.pdf /var/lib/mysql-files/stack-func3nope-new12.dot |  
  11. +-------------------------------------------------------------------------------+  
  12. 1 row in set (9.21 sec)  
  13. +-------------------------------------------------------------------------------+  
  14. | Convert to PNG                                                                |  
  15. +-------------------------------------------------------------------------------+  
  16. | dot -Tpng -o /tmp/stack_49.png /var/lib/mysql-files/stack-func3nope-new12.dot |  
  17. +-------------------------------------------------------------------------------+  
  18. 1 row in set (9.21 sec)  
  19. Query OK, 0 rows affected (9.45 sec)  

I repeated these steps for all the functions above and then created charts of the commands.

Here are the results:

Func1()

为什么MySQL存储过程、函数和触发器对性能不好

Func2()

为什么MySQL存储过程、函数和触发器对性能不好

Func3()

为什么MySQL存储过程、函数和触发器对性能不好

As we can see, there is a sp/jump_if_not call for every "if" check followed by an opening tables statement (which is quite interesting). So parsing the "IF" condition made a difference.

For MySQL 8.0 we can also see MySQL source code documentation for stored routines which documents how it is implemented. It reads:

Flow Analysis OptimizationsAfter code is generated, the low level sp_instr instructions are optimized. The optimization focuses on two areas:

Dead code removal,Jump shortcut resolution.These two optimizations are performed together, as they both are a problem involving flow analysis in the graph that represents the generated code.

The code that implements these optimizations is sp_head::optimize().

However, this does not explain why it executes "opening tables." I have filed a bug.

When Slow Functions Actually Make a Difference

Well, if we do not plan to run one million of those stored functions, we will never even notice the difference. However, where it will make a difference is ... inside a trigger. Let's say that we have a trigger on a table: every time we update that table it executes a trigger to update another field. Here is an example: let's say we have a table called "form" and we simply need to update its creation date:

  1. mysql> update form set form_created_date = NOW() where form_id > 5000;  
  2. Query OK, 65536 rows affected (0.31 sec)  
  3. Rows matched: 65536  Changed: 65536  Warnings: 0  

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读