(1)mysql最佳化之sql效能問題定位

來源:互聯網
上載者:User

標籤:mysql   最佳化   效能   定位   

概述

我們面對一個問題的時候,首先是發現問題,然後才是解決問題。在這篇文章中,主要解決如何定位問題。

解決方案 1.通過show status瞭解各種sql執行頻率
show status [like ‘com_%‘];

Com_xxx表示每個xxx語句執行的次數。
具體參數,參見:
http://lxneng.iteye.com/blog/451985
http://www.sandzhang.com/blog/2010/04/07/mysql-show-status-explained-detail/

2.通過explain分析低效的SQL
explain sql_statement;


參數關係

參數 解釋
select_type 表示查詢的類型 simple-簡單表,primary-主查詢,union-union中的第二個查詢,subquery-子查詢
table 查詢的表 -
type 訪問類型 all-全表掃描,index-索引全掃描,range-索引範圍掃描,ref-使用非唯一索引或(唯一索引的首碼)掃描,eq_ref-唯一索引掃描,const/system-單表最多有一行匹配,null-不用訪問表或索引,就能直接得到結果
possible_keys 查詢時候可能使用到的索引 -
key 實際使用的索引 -
key_len 使用索引欄位長度 -
rows 掃描行的數量 -
extra 執行情況說明和描述 -
3.explain extended 和 show warnings
explain extended sql_statement;show warnings;



explain extended輸出結果相比explain多了filtered欄位(所有結果行數/查詢結果行數*100),show warning的message欄位可以看到sql最佳化器最佳化的結果。

4.通過 show profile分享sql
#查看是否mysql支援profileSELECT @@have_profiling;#查看是否開啟profilingselect @@profiling;#查看profileshow profiles;#查看某一個具體的query的profile,n-查詢id;show profile for query n;


Sending data狀態表示mysql線程開始訪問資料行並把結果返回給用戶端,而不僅僅是返回結果給用戶端。

5.通過trace分析器分析
#開啟trace,設定格式為json,設定trace能使用的最大記憶體大小。set optimizer_trace="enabled=on",end_markers_in_json=on;set optimizer_trace_max_mem_size=100000;#檢查traceSELECT * FROM information_schema.OPTIMIZER_TRACE;

6.慢查詢日誌

(1)mysql最佳化之sql效能問題定位

聯繫我們

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