效能測試之資料庫監控與分析 1

2021-10-09 15:06:04 字數 2872 閱讀 4649

效能測試過程中,資料庫相關效能對系統的影響是不可小覷的。以mysql為例,做乙個簡單介紹。

影響資料庫效能的因素

伺服器硬體

mysql引數配置

show variables like '%query_cache%';  查詢快取

show variables like '%read_buffer_size%';讀入緩衝區大小

show variables like '%max_connections%';連線數

show variables like '%tmp_table_size%';臨時表大小

慢sql語句…

效能測試如何獲取慢sql

mysql伺服器硬體和mysql引數配置,可能在我們日常分析工作中會有占用分析時間會小於慢sql語句,那如何獲取慢sql?

使用連線池工具直接獲取

比如較常用的連線池——druid

可以直接看到慢sql

通過慢日誌獲取慢sql

-- 慢查詢日誌是否開啟

show variables like "slow_query_log";

-- 慢sql的時間定義

show variables like 'slow_launch_time';

-- 慢日誌輸出方式file/table

show variables like 'slow_query_log_file';

set可以設定如上變數。

慢sql的檔案位址在slow_query_log_file,可以直接檢視。

分析慢sql

profiling

show variables like '%profiling%'; 

set profiling=1; 開啟profiling

執行你從慢日誌中看到的語句

show profiles;

show profile all for query 10; 10為show profiles中慢sql的執行id

profiling可以看到sql執行的全過程,和各個環節的時間耗費,方便定位問題

2. explain 執行計畫

explain  慢sql語句; 檢視sql的執行計畫

執行計畫中各字段的含義

type:訪問方式 (最重要)

效能越靠下越高

all 全表掃瞄

index 全索引表掃瞄

range 索引進行範圍掃瞄

index_merge 合併索引,使用多個單列索引掃瞄

ref_or_null

ref 非唯一索引掃瞄

eq_ref 唯一索引掃瞄

system

const

null

table:正在訪問的表名

possible_keys:可能使用的索引

key_len:mysql中使用索引位元組長度

rows:預估為了找到所需的行而要讀取的行數

select_type: 解釋 示例sql

****** 簡單的select select * from tb_student

primary 需要union或者子查詢 select (select name from tb_student where id =1) from tb_student

union union操作 select * from tb_student union select * from tb_student

dependent union 查詢與外部相關(mysql優化器會將in優化成exists) select * from tb_student where id in(select id from tb_student union select id from tb_student) select * from tb_student a where exists (select 1 from tb_student where id = a.id union select id from tb_student where id = a.id)

union result union結果集 select * from tb_student union select * from tb_student

subquery 除了from包含的子查詢 select (select name from tb_student where id =1) from tb_student

depend subquery 類似depend union select (select name from test.tb_student a where a.id=b.id) from test.tb_student b

derived 派生表 select * from (select * from tb_student) t

掃一掃,關注我

效能測試之資料庫監控與分析 PMM使用

pmm percona monitoring and management 是乙個用於管理和監控資料庫效能的開源平台。它能提供全面的基於時間和各類監控指標 計數器的分析。效能測試中,是做資料庫監控的較好工具。官網 支援的資料庫 原理分析 以mysql為例,整個工作的基本原理簡單說 pmm clien...

效能測試之資料庫監控分析工具PMM

pmm percona monitoring and management 是乙個用於管理和監控資料庫效能的開源平台。它能提供全面的基於時間和各類監控指標 計數器的分析。效能測試中,是做資料庫監控的較好工具。官網 支援的資料庫 原理分析 以mysql為例,整個工作的基本原理簡單說 pmm clien...

效能測試 監控Mysql資料庫方式

1,進入到 etc目錄下,開啟my.cnf檔案,在檔案最後新增幾行 slow query log on 開啟慢查詢開關 slow query log file usr udev slow.log 慢查詢監控日誌存放位置 long query time 2 大於等於2秒的sql語句記錄到日誌中 2,修...