Valerii Against Everyone
不管對於哪種服務,對於其優化,無非是從兩個方面著手,第一個是對於硬體方面的優化,第二個是對系統以及服務本身的優化。
一、常用查詢
1.1 查詢連結MySQL伺服器的次數
mysql> show status like 'connections';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections | 616 |
+---------------+-------+
1.2 查詢MySQL伺服器執行時間
mysql> show status like 'uptime'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Uptime | 3534 | +---------------+-------+ #單位為秒
1.3 查詢操作的次數
mysql> show status like 'com_select';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select | 2 |
+---------------+-------+
1.4 查詢插入操作的次數
mysql> show status like 'com_insert'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Com_insert | 2 | +---------------+-------+ #即使插入的操作是錯誤的,依然會增加次數
1.5 更新操作的次數
mysql> show status like 'com_update';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_update | 1 |
+---------------+-------+
1.6 刪除操作的次數
mysql> show status like 'com_delete'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Com_delete | 0 | +---------------+-------+
1.7 查詢慢查詢次數
mysql> show status like 'slow_queries';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 10 |
+---------------+-------+
二、對SQL語句進行分析
2.1 使用explain關鍵字進行分析
mysql> explain select * from stu_info\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE #表示select語句的型別 其中simple 是簡單查詢(不包括連線查詢和子查詢) Primary 主查詢 Union 連線查詢
table: stu_info
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL #使用哪個列或常數與索引一起使用來查詢記錄
rows: 3
filtered: 100.00
Extra: NULL
2.2 使用索引提高查詢效率
mysql> explain select * from stu_info where id=3\G
###沒有索引時的查詢結果分析如下
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: stu_info
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 3 #需要查詢三行才能查到(這個表資料總共也就三行)
filtered: 33.33
Extra: Using where
mysql> create index index_01 on stu_info(id);
#建立索引
mysql> explain select * from stu_info where id=3\G
#再次查詢
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: stu_info
partitions: NULL
type: ref
possible_keys: index_01 #使用的是哪個索引名稱
key: index_01
key_len: 5
ref: const
rows: 1 #建立索引後,查詢1行就查到可
filtered: 100.00
Extra: NULL
使用索引注意事項:
- 建立索引之後,用 like ‘xx%’ %不在第一位查詢效率最高;
- 若使用多欄位索引,除了第一欄位查詢最快,其餘不會按索引來,索引不生效;
- 若建立索引所設定的欄位,查詢索引組合 or 左右邊的值都是屬於索引設定欄位下的值;
關於索引的注意事項可以參考MySQL索引
三、profiling分析查詢
通過慢日誌查詢可以知道哪些SQL語句執行效率低下,通過explain我們可以得知SQL語句的具體執行情況,索引使用等,還可以結合show命令檢視執行狀態。如果覺得explain的資訊不夠詳細,可以通過profiling命令得到更準確的SQL執行消耗系統資源的資訊。 profiling預設是關閉的。可以通過以下語句檢視:
3.1 查詢profiling是否開啟
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | OFF | #OFF表示未開啟
| profiling_history_size | 15 |
+------------------------+-------+
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
#0表示沒有開啟
3.2 開啟profiling
mysql> set profiling=1; #開啟
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | ON |
| profiling_history_size | 15 |
+------------------------+-------+
3.3 執行要測試的SQL語句
mysql> select * from stu_info;
+------+------+
| id | name |
+------+------+
| 1 | lv |
| 2 | lv1 |
| 3 | l1 |
+------+------+
3.4 檢視SQL語句對應的ID,對其進行分析
mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------+
| 1 | 0.00024900 | select @@profiling |
| 2 | 0.00126350 | show variables like '%profiling%' |
| 3 | 0.00033450 | select * from stu_info |
+----------+------------+-----------------------------------+
mysql> show profile for query 3; #查詢sql語句的詳細分析
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000085 |
| checking permissions | 0.000009 |
| Opening tables | 0.000035 |
| init | 0.000015 |
| System lock | 0.000006 |
| optimizing | 0.000003 |
| statistics | 0.000028 |
| preparing | 0.000010 |
| executing | 0.000002 |
| Sending data | 0.000108 |
| end | 0.000006 |
| query end | 0.000005 |
| closing tables | 0.000005 |
| freeing items | 0.000008 |
| cleaning up | 0.000009 |
+----------------------+----------+
#status是profile裡的狀態,duration是status狀態下的耗時
因此我們關注的就是哪個狀態最耗時,這些狀態中哪些可以優化,當然也可以檢視更多的資訊,比如:CPU等。語法如下:
mysql> show profile block io for query 7\G
mysql> show profile all for query 7\G
除了上面的block io和all以外,還可以換成cpu(顯示使用者cpu時間、系統cpu時間)、ipc(顯示傳送和接收相關開銷資訊)、page faults(顯示頁面錯誤相關開銷資訊)、swaps(顯示交換次數相關開銷的資訊)。
注意:測試完成之後,記得要關閉除錯功能,以免影響資料庫的正常使用!
四、對資料表結構進行優化
對資料表結構的優化大概從以下幾個方面入手:
- 將欄位很多的表分解成多個表,儘量避免表字段過多;
- 增加中間表,合理增加冗餘欄位;
- 優化插入記錄的速度;
- 在插入資料之前禁用索引,會讓建立索引不生效,命令:
ALTER TABLE table_name DISABLE KEYS;
- 根據實際情況來定,在插入記錄之前禁用唯一性檢查,命令:
set unique_checks=0;
- 多條插入資料的命令最好整合為一條;
- 使用load data infle批量插入資料;
對於innodb引擎的表來說,以下幾點可以進行優化:
- 禁用唯一性檢查:set unique_checks=0;
- 禁用外來鍵檢查:set foreign_key_checks=0;
- 禁用自動提交:set autocommit=0;
所謂分析表,就是分析關鍵字的分佈,檢查表就是檢查是否存在錯誤,優化表就是刪除或更新造成的空間浪費。
4.1 分析表
分析表可以一次分析一個或多個表,在分析期間只能讀,不能進行插入和更新操作。分析表的語法如下:
mysql> analyze table stu_info;
+-----------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------------+---------+----------+----------+
| sbtest.stu_info | analyze | status | OK |
+-----------------+---------+----------+----------+
對於上述返回的結果解釋:Table是表名 ,op執行的操作是什麼, msg_type 資訊級別(status是正常狀態,info是資訊,note注意,warning警告,error錯誤), msg_text 是顯示資訊。
4.2 檢查表
檢查是否存在錯誤,關鍵字統計,檢查檢視是否有錯誤 Check table 表名 option ={quick |fast | medium|extended |changed} Quick
不掃描行,不檢查錯誤連線 Fast 只檢查沒有被正確關閉的表 Medium 掃描行驗證被刪除的連線是有效的,也可以計算各行的關鍵字校驗和。 Extended 對每行所有關鍵字進行全面的關鍵字查詢,Changed 只檢查上次檢查後被更改的表和沒有被正確關閉的表,Option只對myisam 有效 對innodb表無效,在執行時會給表加上只讀鎖。
mysql> check table stu_info;
+-----------------+-------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------------+-------+----------+----------+
| sbtest.stu_info | check | status | OK |
+-----------------+-------+----------+----------+
4.3 優化表
消除刪除或更新造成的空間浪費,命令語法格式為:Optimize [local |no_write_to_binlog] table tb1_name
…., 優化myisam的表和innodb的表都有效, 但是隻能優化表中的varchar\text\blob數字型別, 執行過程中上只讀鎖。
mysql> optimize table stu_info\G
*************************** 1. row ***************************
Table: sbtest.stu_info
Op: optimize
Msg_type: note
Msg_text: Table does not support optimize, doing recreate + analyze instead
*************************** 2. row ***************************
Table: sbtest.stu_info
Op: optimize
Msg_type: status
Msg_text: OK