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

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

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

That is good and fast. Now we create a trigger which will call our dummy func1():

  1. CREATE DEFINER=`root`@`localhost` TRIGGER `test`.`form_AFTER_UPDATE`  
  2. AFTER UPDATE ON `form`  
  3. FOR EACH ROW  
  4. BEGIN  
  5. declare r int default 0;  
  6. select func1() into r;  
  7. END  

Now repeat the update. Remember: it does not change the result of the update as we do not really do anything inside the trigger.

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

Just adding a dummy trigger will add 2x overhead: the next trigger, which does not even run a function, introduces a slowdown:

  1. CREATE DEFINER=`root`@`localhost` TRIGGER `test`.`form_AFTER_UPDATE`  
  2. AFTER UPDATE ON `form`  
  3. FOR EACH ROW  
  4. BEGIN  
  5. declare r int default 0;  
  6. END 
  1. mysql> update form set form_created_date = NOW() where form_id > 5000;   
  2. Query OK, 65536 rows affected (0.52 sec)   
  3. Rows matched: 65536  Changed: 65536  Warnings: 0  

Now, lets use func3 (which has "dead" code and is equivalent to func1):

  1. CREATE DEFINER=`root`@`localhost` TRIGGER `test`.`form_AFTER_UPDATE`  
  2. AFTER UPDATE ON `form`  
  3. FOR EACH ROW  
  4. BEGIN  
  5. declare r int default 0;  
  6. select func3() into r;  
  7. END  
  1. mysql> update form set form_created_date = NOW() where form_id > 5000;   
  2. Query OK, 65536 rows affected (1.06 sec)   
  3. Rows matched: 65536  Changed: 65536  Warnings: 0  

However, running the code from the func3 inside the trigger (instead of calling a function) will speed up the update:

  1. CREATE DEFINER=`root`@`localhost` TRIGGER `test`.`form_AFTER_UPDATE`  
  2. AFTER UPDATE ON `form`  
  3. FOR EACH ROW  
  4. BEGIN  
  5.     declare r int default 0;  
  6.     IF 1=2 THEN  
  7. select levenshtein_limit_n('test finc', 'test func', 1) into r;  
  8.     END IF;  
  9.     IF 2=3 THEN  
  10. select levenshtein_limit_n('test finc', 'test func', 10) into r;  
  11.     END IF;  
  12.     IF 3=4 THEN  
  13. select levenshtein_limit_n('test finc', 'test func', 100) into r;  
  14.     END IF;  
  15.     IF 4=5 THEN  
  16. select levenshtein_limit_n('test finc', 'test func', 1000) into r;  
  17.     END IF; 
  18. END   
  1. mysql> update form set form_created_date = NOW() where form_id > 5000; 
  2. Query OK, 65536 rows affected (0.66 sec) 
  3. Rows matched: 65536  Changed: 65536  Warnings: 0  

Memory Allocation

(编辑:晋中站长网)

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

热点阅读