mysql巧用query cache解決連線逾時

來源:互聯網
上載者:User

應用日誌中狂報錯誤,擷取連線逾時:


同時應用報錯超出了資料庫的最大串連數:max connections:


這種情況很有可能是有慢sql佔用了串連池中的串連沒有釋放,導致後續進來的請求遲遲擷取不到串連池中的串連,導致請求報錯,登入資料庫排查發現如下sql出現執行非常的慢:

mysql> select * from user where md5(nick)=’3f5950f59ddf2a0d14a44166040e348f’;
Empty set (1.32 sec)

一眼可以看出在nick上使用了md5函數,導致user表中的索引不能使用,而全表掃描:

mysql> explain select * from user where md5(nick)=’3f5950f59ddf2a0d14a44166040e348f?
+—-+————-+——-+——+—————+——+———+——+——?+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+—————+——+———+——+——?+————-+
| 1 | SIMPLE | user | ALL | NULL | NULL | NULL | NULL | 256608 | Using where |
+—-+————-+——-+——+—————+——+———+——+——?+————-+
1 row in set (0.00 sec)

通常情況下需要使用者修改應用,將應用中的md5函數去掉,但是修改業務的方法還需要一段時間,不能夠立刻是業務恢複,所以看看資料庫還有沒有其他的方法,靈機一動,由於該應用的情境是讀多寫非常少的應用,想到了query cache:

mysql> show variables like ‘%query%’;
+——————————+——————————————-+
| Variable_name | Value |
+——————————+——————————————-+
| ft_query_expansion_limit | 20 |
| have_query_cache | YES |
| long_query_time | 1.000000 |
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 0 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
| slow_query_log | ON |
| slow_query_log_file | /home/mysql/data3003/mysql/slow_query.log |
+——————————+——————————————-+
12 rows in set (0.00 sec)

mysql> set global query_cache_size=1024*1024*32;
Query OK, 0 rows affected (0.02 sec)

 

mysql> Ctrl-C — exit!
Aborted

root@xxxx ~
# my 3003
Entry Port ==== 3003
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4601147
Server version: 5.1.61-Alibaba-3930-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> use cuxiaowang_db
Database changed
mysql> select * from user where md5(nick)=’3f5950f59ddf2a0d14a44166040e348f?’;
Empty set (1.32 sec)

mysql> select * from user where md5(nick)=’3f5950f59ddf2a0d14a44166040e348f?
Empty set (0.00 sec)

可以看到查詢能夠很快的返回,應用立刻恢複正常。

PS:query cache的開啟是有一定情境的,由於query cache並不適合更新,插入,刪除非常大的應用,所以開啟query cache一定要謹慎,筆者曾經看到由於其資料庫有大量的插入和更新的資料庫開啟query cache而導致整個資料庫都在等待query cache的故障:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.