mysql最佳化思路

來源:互聯網
上載者:User

標籤:tab   分析   exe   set   miss   應該   使用   sel   故障   

通過指令碼,重新整理觀察mysql的status,觀察是否有周期性故障活波動,
一般由訪問高峰或者緩衝失效引起,家緩衝並更改緩衝失效策略,是失效時間分散或頁面定時失,

SHOW PROCESSLIST顯示哪些線程正在運行。

 

您也可以使用mysqladmin processlist語句得到此資訊。如果您有SUPER許可權,您可以看到所有線程。否則,您只能看到您自己的線程
mysql 開啟慢查詢日誌

slow_query_log 這個參數設定為ON,可以捕獲執行時間超過一定數值的SQL語句。long_query_time 當SQL語句執行時間超過此數值時,就會被記錄到日誌中,建議設定為1或者更短slow_query_log_file 記錄日誌的檔案名稱。log_queries_not_using_indexes 這個參數設定為ON,可以捕獲到所有未使用索引的SQL語句,儘管這個SQL語句有可能執行得挺快。

 

使用profiler來分析一條query的執行時間和效能瓶頸,
開啟 profiling ;

set profiling=1;

 

隨便執行一條語句 select count(*) from user where id>2;

show profiles;

 

得到

+----------+------------+--------------------------------------+| Query_ID | Duration   | Query                                |+----------+------------+--------------------------------------+|        2 | 0.00009200 | set profiling=1                      ||        5 | 0.02003525 | select count(*) from user where id>2 |+----------+------------+--------------------------------------+

 

包含一個query_id和執行時間和query語句
通過query_id可以查看到更詳細的資訊;

show profile cpu ,block io  for query 5;

 

+----------------------+----------+----------+------------+--------------+---------------+| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |+----------------------+----------+----------+------------+--------------+---------------+| starting             | 0.000170 | 0.000000 |   0.000000 |            0 |             0 || checking permissions | 0.000019 | 0.000000 |   0.000000 |            0 |             0 || Opening tables       | 0.000033 | 0.000000 |   0.000000 |            0 |             0 || init                 | 0.000061 | 0.000000 |   0.000000 |            0 |             0 || System lock          | 0.000021 | 0.000000 |   0.000000 |            0 |             0 || optimizing           | 0.000021 | 0.000000 |   0.000000 |            0 |             0 || statistics           | 0.010826 | 0.000000 |   0.000000 |          184 |             0 || preparing            | 0.000041 | 0.000000 |   0.000000 |            0 |             0 || executing            | 0.000005 | 0.000000 |   0.000000 |            0 |             0 || Sending data         | 0.008731 | 0.008000 |   0.000000 |            0 |             0 || end                  | 0.000020 | 0.000000 |   0.000000 |            0 |             0 || query end            | 0.000018 | 0.000000 |   0.000000 |            0 |             0 || closing tables       | 0.000012 | 0.000000 |   0.000000 |            0 |             0 || freeing items        | 0.000032 | 0.000000 |   0.000000 |            0 |             0 || cleaning up          | 0.000027 | 0.000000 |   0.000000 |            0 |             0 |+----------------------+----------+----------+------------+--------------+---------------+

 


對mysql伺服器的最佳化不要一上來就去最佳化sql語句,應該首先觀察全域情況,至少要先搞清楚
問題出在哪,應該使用指令碼來觀察伺服器一段時間(一天或更長)的健康情況,比如cpu,io,進程串連數等
最後才分析具體原因處在哪裡;針對解決

mysql最佳化思路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.