性能测试如何监控数据库服务器,性能测试之数据库监控与分析

性能测试过程中,数据库相关性能对系统的影响是不可小觑的。以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

浏览器访问:项目地址/druid/sql.html

可以直接看到慢sql

e72fd55221129379c1a78db4ba28f9a7.png

通过慢日志获取慢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,可以直接查看。

注:推荐一款慢日志分析工具:pt-query-digest

6d3bb9ff341b82417eaaff716b974b36.png

分析慢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执行的全过程,和各个环节的时间耗费,方便定位问题

701001f04e33dcfac4c8d21d82281d55.png

2. explain 执行计划

7e0bfc23b737cc7270572295de9251fd.png

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

simple  简单的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

本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值