每秒鍾處理完的請求數,每秒處理事務數,一次請求所需平均數,系統內處理的並發請求數。
Mysqlslap
Sysbench 數據庫專用測試工具
Jmeter
Sysbenc簡介
設置yum源
http://mirrors.aliyun.com/repo/Centos-7.repo
http://mirrors.163.com/.help/CentOS7-Base-163.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash
yum -y install sysbench
參數名稱 | 功能意義 |
--mysql-host | IP地址 |
--mysql-port | 端口號 |
--mysql-user | 用戶名 |
--mysql-password | 密碼 |
--oltp-test-mode | 執行模式 |
--oltp-tables-count | 測試表的數量 |
--oltp-tables-size | 測試表的記錄數 |
--threads | 並發線程數 |
--time | 測試執行時間 |
--report-interval | 生成報告單的間隔時間 |
prepare 准備測試數據
run 執行測試
cleanup 清楚測試數據
sysbench
/usr/share/sysbench/tests/include/oltp_legacy/oltp.lua
--mysql-host=127.0.0.1 --mysql-port=3306
--mysql-user=root --mysql-password=abc123456
--oltp-tables-count=10 --oltp-table-size=100000
prepare
索引使用
不要把select 寫成 select *。
謹慎使用like,會導致索引失效全表掃描。
order by 子句加索引排序快
少用 is null not null 會導致索引失效。null值無法排序,與null相關都會不走索引,使用 >=0 或者 <=0
少用不等於,二叉樹對於不等於無法使用索引 會導致掃描全表,可以寫成>0或者<0
盡量少用or運算符,or運算符后面的條件無法使用全表掃描,拆分成兩條sql語句,進行union all進行連接。
少用in和not in也不會走索引,同上改造。
避免條件語句中要進行數值轉換如where id = ‘1’ ,影響SQL執行速度。
表達式左側不要使用表達式和函數,會導致索引失效,where salary *12 >200 正確 where salary >200/12,where year(hiredate) >=2000 正確 where hiredate > '2000-01-01 00:00:00'
mysql參數優化
max_connections是最大並發連接數,默認51,最大連接數16834 設置為默認85%
show variables like 'max_connections'
show status like 'max_used_connections'
修改my.cnf
max_connections=5000
會為每個連接建立緩沖區
back_log存放執行的堆棧大小,如果請求數超過連接數會存放到堆棧中。
修改並發線程數
innodb_thread_concurrency =2,並發線程數量不能太大。
wait-timeout = 連接超時間,單位十秒。
innodb緩存機制,保存部分數據表和數據索引
innodb_buffer_pool_size是innodb的緩存容量,默認是128m可以設置成主機內存70%-80%
mysql慢查詢日志
慢查詢日志會把耗時超過規定的sql記錄下來,利用慢查詢日志,定位分析性能的瓶頸。
show variables like ‘slow_query%’
slow_query_log開啟或者關閉
long_query_time超過該時長的會記錄下來
采用explain進行分析,type表示掃描表類型,值為all表示全表掃描,where有值就是const,利用了索引后面會有key值