加入收藏 | 设为首页 | 会员中心 | 我要投稿 河北网 (https://www.hebeiwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

为什么MySQL存储进程、函数和触发器对机能欠好

发布时间:2018-08-18 09:10:23 所属栏目:编程 来源:佚名
导读:技能沙龙 | 邀您于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) 

(编辑:河北网)

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

热点阅读