成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

為什么MySQL存儲過程、函數和觸發器對性能不好

數據庫 MySQL
MySQL存儲過程、函數和觸發器是應用程序開發人員的誘人構造。但是,正如我所發現的,使用MySQL存儲例程會影響數據庫性能。由于不能完全確定在客戶訪問期間看到了什么,我開始創建一些簡單的測試來度量觸發器對數據庫。

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) 

As we can see, func3 (with four dead code calls that will never be executed, otherwise identical to func1) runs almost 3x slower compared to func1(); func3_nope() is identical in terms of response time to func3().

Visualizing All System Calls From Functions

To figure out what is happening inside the function calls, I used performance_schema/sys schema to create a trace with ps_trace_thread() procedure.

1.Get the thread_id for the MySQL connection: 

  1. mysql> select THREAD_ID from performance_schema.threads where processlist_id = connection_id();  
  2. +-----------+  
  3. | THREAD_ID |  
  4. +-----------+  
  5. |        49 |  
  6. +-----------+  
  7. 1 row in set (0.00 sec)  

2.Run ps_trace_thread in another connection passing the thread_id=49: 

  1. mysql> CALL sys.ps_trace_thread(49, concat('/var/lib/mysql-files/stack-func1-run1.dot'), 10, 0, TRUETRUETRUE);  
  2. +--------------------+  
  3. | summary            |  
  4. +--------------------+  
  5. | Disabled 0 threads |  
  6. +--------------------+  
  7. 1 row in set (0.00 sec)  
  8. +---------------------------------------------+  
  9. | Info                                        |  
  10. +---------------------------------------------+  
  11. | Data collection starting for THREAD_ID = 49 |  
  12. +---------------------------------------------+  
  13. 1 row in set (0.00 sec)  

3.At that point I switched to the original connection (thread_id=49) and run: 

  1. mysql> select func1();  
  2. +---------+  
  3. | func1() |  
  4. +---------+  
  5. |       0 |  
  6. +---------+  
  7. 1 row in set (0.00 sec)  

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() 

Func2() 

 

Func3() 

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  

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

Potentially, even if the code will never run, MySQL will still need to parse the stored routine-or trigger-code for every execution, which can potentially lead to a memory leak, as described in this bug.

結論

存儲的例程和觸發器事件在執行時被解析。即使是永遠不會運行的“死”代碼也會顯著影響批量操作的性能(例如,在觸發器中運行時)。這也意味著通過設置“標志”(例如)禁用觸發器仍然會影響批量操作的性能。 

責任編輯:龐桂玉 來源: 代碼人生
相關推薦

2010-05-26 17:57:44

MySQL 觸發器

2024-01-19 09:37:19

MySQL數據庫

2019-04-30 15:28:46

數據庫存儲過程觸發器

2010-05-19 11:25:46

MySQL觸發器

2010-04-26 14:12:23

Oracle使用游標觸

2019-01-14 14:41:27

Mysql存儲觸發器

2021-08-05 12:41:57

高并發性能CAS

2023-02-28 11:29:09

存儲函數MySQL

2024-08-05 00:00:00

RocketMQKafka磁盤

2024-04-25 09:43:42

PostgreSQL數據庫關系型數據庫

2010-07-16 10:19:31

2011-05-20 14:06:25

Oracle觸發器

2021-07-30 10:33:57

MySQL觸發器數據

2010-05-18 15:58:39

MySQL觸發器

2010-05-31 18:06:07

MySQL 觸發器

2010-10-12 10:04:15

MySQL觸發器

2010-10-12 10:24:58

mysql觸發器

2010-04-09 09:07:43

Oracle游標觸發器

2011-07-21 15:42:53

SQL觸發器存儲過程

2010-05-18 15:36:44

MySQL觸發器
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产高潮好爽受不了了夜色 | 亚洲视频自拍 | 久久久精品网站 | 亚洲精品乱码久久久久久久久久 | 亚洲精品一区二区三区在线 | 华人黄网站大全 | 精品久久久久久亚洲精品 | 亚洲精品在 | 一区二区免费高清视频 | av手机免费在线观看 | 男人天堂网址 | 午夜专区 | 成人精品久久日伦片大全免费 | 精品96久久久久久中文字幕无 | www.9191.com| 国产成人av在线播放 | 亚洲在线一区 | 男女午夜激情视频 | 免费激情 | 少妇一区在线观看 | 国产日产精品一区二区三区四区 | 一区二区三区不卡视频 | 国产女人叫床高潮大片免费 | 免费美女网站 | 国产精品99久久久久久久久久久久 | 日韩另类| 国产精品不卡一区二区三区 | 亚洲精品 在线播放 | a级片网站| 欧美性一级 | 粉色午夜视频 | 韩日精品一区 | 伊人亚洲 | 永久网站 | 国产91在线观看 | 欧美不卡一区二区三区 | 中文字幕在线观看视频网站 | 欧美日韩国产三级 | 欧美精品福利 | 国产二区精品视频 | 亚洲国产高清在线观看 |