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

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

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

MySQL存储过程、函数和触发器是应用程序开发人员的诱人构造。但是,正如我所发现的,使用MySQL存储例程会影响数据库性能。由于不能完全确定在客户访问期间看到了什么,我开始创建一些简单的测试来度量触发器对数据库性能的影响。结果可能会让你大吃一惊。

为什么存储例程在性能上不是最佳的:短版本?

最近,我与一位客户合作,了解触发器和存储例程的性能。我对存储例程的了解是:“死”代码(分支中的代码永远不会运行)仍然可以显著降低函数/过程/触发器的响应时间。我们需要小心地清理我们不需要的东西。

Profiling MySQL Stored Functions

Let's compare these four simple stored functions (in MySQL 5.7):

Function 1

  1. CREATE DEFINER=`root`@`localhost` FUNCTION `func1`() RETURNS int(11) 
  2. BEGIN 
  3. declare r int default 0; 
  4. RETURN r; 
  5. END 

This function simply declares a variable and returns it. It is a dummy function.

Function 2

  1. CREATE DEFINER=`root`@`localhost` FUNCTION `func2`() RETURNS int(11) 
  2. BEGIN 
  3.     declare r int default 0; 
  4.     IF 1=2 
  5.     THEN 
  6. select levenshtein_limit_n('test finc', 'test func', 1000) into r; 
  7.     END IF; 
  8. RETURN r; 
  9. END 

This function calls another function, levenshtein_limit_n (calculates levenshtein distance). But wait: this code will never run — the condition IF 1=2 will never be true. So that is the same as function 1.

Function 3

  1. CREATE DEFINER=`root`@`localhost` FUNCTION `func3`() RETURNS int(11) 
  2. BEGIN 
  3.     declare r int default 0; 
  4.     IF 1=2 THEN 
  5. select levenshtein_limit_n('test finc', 'test func', 1) into r; 
  6.     END IF; 
  7.     IF 2=3 THEN 
  8. select levenshtein_limit_n('test finc', 'test func', 10) into r; 
  9.     END IF; 
  10.     IF 3=4 THEN 
  11. select levenshtein_limit_n('test finc', 'test func', 100) into r; 
  12.     END IF; 
  13.     IF 4=5 THEN 
  14. select levenshtein_limit_n('test finc', 'test func', 1000) into r; 
  15.     END IF; 
  16. RETURN r; 
  17. END 

Here there are four conditions and none of these conditions will be true: there are 4 calls of "dead" code. The result of the function call for function 3 will be the same as function 2 and function 1.

Function 4

  1. CREATE DEFINER=`root`@`localhost` FUNCTION `func3_nope`() RETURNS int(11) 
  2. BEGIN 
  3.     declare r int default 0; 
  4.     IF 1=2 THEN 
  5. select does_not_exit('test finc', 'test func', 1) into r; 
  6.     END IF; 
  7.     IF 2=3 THEN 
  8. select does_not_exit('test finc', 'test func', 10) into r; 
  9.     END IF; 
  10.     IF 3=4 THEN 
  11. select does_not_exit('test finc', 'test func', 100) into r; 
  12.     END IF; 
  13.     IF 4=5 THEN 
  14. select does_not_exit('test finc', 'test func', 1000) into r; 
  15.     END IF; 
  16. RETURN r; 
  17. END 

This is the same as function 3, but the function we are running does not exist. Well, it does not matter as the selectdoes_not_exit will never run.

So, all the functions will always return 0. We expect that the performance of these functions will be the same or very similar. Surprisingly, that is not the case! To measure the performance, I used the "benchmark" function to run the same function 1M times. Here are the results:

  1. +-----------------------------+ 
  2. | benchmark(1000000, func1()) | 
  3. +-----------------------------+ 
  4. |                           0 | 
  5. +-----------------------------+ 
  6. 1 row in set (1.75 sec) 
  7. +-----------------------------+ 
  8. | benchmark(1000000, func2()) | 
  9. +-----------------------------+ 
  10. |                           0 | 
  11. +-----------------------------+ 
  12. 1 row in set (2.45 sec) 
  13. +-----------------------------+ 
  14. | benchmark(1000000, func3()) | 
  15. +-----------------------------+ 
  16. |                           0 | 
  17. +-----------------------------+ 
  18. 1 row in set (3.85 sec) 
  19. +----------------------------------+ 
  20. | benchmark(1000000, func3_nope()) | 
  21. +----------------------------------+ 
  22. |                                0 | 
  23. +----------------------------------+ 
  24. 1 row in set (3.85 sec) 

(编辑:常州站长网)

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

热点阅读