mysql压力测试和sql优化
每秒钟处理完的请求数,每秒处理事务数,一次请求所需平均数,系统内处理的并发请求数。
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值
浙公网安备 33010602011771号