mysql 效能優化

2021-09-26 04:14:11 字數 3134 閱讀 7206

1.centos系統引數優化-提公升mysql伺服器效能

2.mysql引數優化

有些時候我們需要了解mysql的伺服器狀態資訊,譬如當前mysql啟動後的執行時間,當前mysql的客戶端會話連線數,當前mysql伺服器執行的慢查詢數,當前mysql執行了多少select語句、執行了多少update/delete/insert語句等統計資訊,從而便於我們根據當前mysql伺服器的執行狀態進行對應的調整或優化工作。

在mysql中,我們可以使用show status指令語句來檢視mysql伺服器的狀態資訊。

####mysql 效能監控

show status;

-- 1、查詢連線mysql伺服器次數

show status like 'connections';

-- 2、查詢當前mysql本次啟動後的執行統計時間

show status like 'uptime';

-- 3、查詢慢查詢次數

show status like 'slow_queries'

-- 4、 查詢自當前mysql啟動後所有連線執行的select語句總數

show global status like 'com_select';

-- 5、檢視mysql伺服器的執行緒資訊

show status like 'thread_%';

-- 6、--檢視insert語句的執行數

show status like 'com_insert';

show global status like 'com_insert';

-- 7、查詢本次mysql啟動後執行的select語句的次數

show status like 'com_select';

-- 8、查詢本次mysql啟動後執行的select語句的次數

show status like 'com_update';

show status like 'com_delete';

-- 10、innodb操作次數

show status like 'innodb_rows_%';

通過上面資訊可以修改mysql伺服器的配置引數,或是否考慮讀寫分類等

使用show profiles分析sql效能

開啟profile

show processlist 資料執行狀態分析

按客戶端 ip 分組,看哪個客戶端的鏈結數最多

select client_ip,count(client_ip) as client_num from (select substring_index(host,':' ,1) as client_ip from information_schema.processlist ) as connect_info group by client_ip order by client_num desc;
檢視正在執行的執行緒,並按 time 倒排序,看看有沒有執行時間特別長的執行緒
select * from information_schema.processlist where command != 'sleep' order by time desc;
找出所有執行時間超過 5 分鐘的執行緒,拼湊出 kill 語句,方便後面查殺
select concat('kill ', id, ';') from information_schema.processlist where command != 'sleep' and time > 300 order by time desc;
3.檢視慢查詢日誌使用pt-query-digest分析慢查詢日誌

4.根據慢查詢日誌,explain/desc sql是用來分析sql語句,幫助優化的乙個命令。

explain的語法如下:

列的說明:

id: select的識別符,這是select的查詢序列號。

select_type: select型別,有以下幾種不同的型別

table 表示是哪個表的資料。

type : 表示表的連線型別,效能由好到差的型別型別為

type比較重要。表示鏈結的型別。鏈結型別由好到壞的,依次是 system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > all

一般情況,至少要達到 range 級別,最好是 ref 級別。否則可能會有效能問題。

possible_keys 是指可能用到該錶的索引,如果為null則沒有。

key 是指用到的索引。

key_len 是索引的長度,在不影響查詢精度的情況下,值越小越好 。

ref 是指索引的那一列被使用了。一般會是個常數。

rows mysql認為必須檢查的用來返回請求資料的行數 。

extra 是指額外的資訊。也是比較重要的 。壞的例子是using temporary和using filesort,意思mysql根本不能使用索引,結果是檢索會很慢。引數的資訊非常多有幾十種,常用的有

show status like  "%handler_read_rnd_next%";

5.定期清理表delete刪除資料後,mysql表檔案大小保持不變

optimize table:可以去除刪除操作後留下的資料檔案碎片,減小檔案尺寸,加快未來的讀寫操作.

optimize table只對myisam, bdb和innodb表起作用

6.分庫,分表,分割槽,讀寫分離

mysql效能優化 mysql效能優化

優化方式 1.空間換時間 冗餘 2.時間換空間 字段優先使用型別 int date char varchar text 索引型別 btree索引 hash索引 索引的葉子下,存放乙個資訊指向所在行的資料位址。btree有利於範圍查詢,hash有利於精確查詢。btree用的更多一些。btree索引的常...

mysql的效能優化 mysql效能優化

檢視安裝指令碼 select version 非互動式超時時間,如jdbc show global variables like wait timeout 互動式超時時間,如資料庫工具 show global variables like interactive timeout show sessi...

mysql 效能優化 命令 mysql效能優化

發現問題 當發現程式執行比較慢的時候,首先排除物力資源問題之後,就將注意力轉向mysq資料庫 1 首先確定執行慢的sql語句 mysql show full processlist 2 確認低效的查詢 多次執行第一步發現time耗費大的sql語句。檢視耗費的時間。3 分析效能 為sql生成乙個執行計...