-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.txt
212 lines (160 loc) · 8.33 KB
/
profile.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
set profiling=1;
set profiling_history_size = 100;
/*
select * from information_schema.profiling where query_id = 6
*/
show profiles;
show profile for query 6;
show profile ALL for query 6;
Status Sending data
Duration 360.204197
CPU_user 430.896494
CPU_system 1.367792
Context_voluntary 12949
Context_involuntary 18036
Block_ops_in 16
Block_ops_out 77328
Messages_sent 0
Messages_received 0
Page_faults_major 0
Page_faults_minor 18905
Swaps 0
Source_function exec
Source_file sql_executor.cc
Source_line 190
登录帐号需要performance_schema库的查询权限;
需要开启performance_schema;
show variables like 'performance_schema';
performance_schema=on;
如果处于off状态,需要在你的my.cnf文件中增加如下配置,然后重启生效;
[mysqld]
performance_schema=ON
开启statment_digest
update setup_consumers set ENABLED='YES' where name='statment_digest';
开启events_statements_history
update setup_consumers set ENABLED='YES' where name='events_statements_history';
建议开启events_statements_history_long
update setup_consumers set ENABLED='YES' where name='events_statements_history';
背景信息
目前暂时仅支持自建数据库 MySQL 5.6.24以上版本。
开启performance_schema约有10%左右的性能消耗
二 执行过程
set profiling=1;
三 结果
1 查看执行过程耗时
show profile for query 1
checking permissions:检查权限
opening tables:打开表
init : 初始化
system lock :系统锁
optimizing : 优化
statistics : 统计
preparing :准备
executing :执行
sending data :发送数据
sorting result :排序
end :结束
query end :查询 结束
closing tables : 关闭表 /去除tmp 表
freeing items : 释放物品
cleaning up :清理
2 查看资源消耗
show profile all for query 2
duration: 持续时间
cpu_user: cpu用户
cpu_system: cpu系统
context_voluntary:上下文主动切换
context_involuntary: 上下文被动切换
block_ops_in: 阻塞的输入操作
block_ops_out: 阻塞的输出操作
page_faults_major: 主分页错误
page_faults_minor: 次分页错误
值得关注的值 1 cpu相关值 2 context 相关值 3 block相关值
----------------------------------
EXPLAIN:
1,使用方式:
explain SQL;
2,返回结果:
1,ID:执行查询的序列号;
2,select_type:使用的查询类型
1,DEPENDENT SUBQUERY:子查询中内层的第一个SELECT,依赖于外部查询的结果集;
2,DEPENDENT UNION:子查询中的UNION,且为UNION 中从第二个SELECT 开始的后面所有SELECT,同样依赖于外部查询的结果集;
3,PRIMARY:子查询中的最外层查询,注意并不是主键查询;
4,SIMPLE:除子查询或者UNION 之外的其他查询;
5,SUBQUERY:子查询内层查询的第一个SELECT,结果不依赖于外部查询结果集;
6,UNCACHEABLE SUBQUERY:结果集无法缓存的子查询;
7,UNION:UNION 语句中第二个SELECT 开始的后面所有SELECT,第一个SELECT 为PRIMARY
8,UNION RESULT:UNION 中的合并结果;
3,table:这次查询访问的数据表;
4,type:对表所使用的访问方式:
1,all:全表扫描
2,const:读常量,且最多只会有一条记录匹配,由于是常量,所以实际上只需要读一次;
3,eq_ref:最多只会有一条匹配结果,一般是通过主键或者唯一键索引来访问;
4,fulltext:全文检索,针对full text索引列;
5,index:全索引扫描;
6,index_merge:查询中同时使用两个(或更多)索引,然后对索引结果进行merge 之后再读取表数据;
7,index_subquery:子查询中的返回结果字段组合是一个索引(或索引组合),但不是一个主键或者唯一索引;
8,rang:索引范围扫描;
9,ref:Join 语句中被驱动表索引引用查询;
10,ref_or_null:与ref 的唯一区别就是在使用索引引用查询之外再增加一个空值的查询;
11,system:系统表,表中只有一行数据;
12,unique_subquery:子查询中的返回结果字段组合是主键或者唯一约束;
5,possible_keys:可选的索引;如果没有使用索引,为null;
6,key:最终选择的索引;
7,key_len:被选择的索引长度;
8,ref:过滤的方式,比如const(常量),column(join),func(某个函数);
9,rows:查询优化器通过收集到的统计信息估算出的查询条数;
10,Extra:查询中每一步实现的额外细节信息
1,Distinct:查找distinct 值,所以当mysql 找到了第一条匹配的结果后,将停止该值的查询而转为后面其他值的查询;
2,Full scan on NULL key:子查询中的一种优化方式,主要在遇到无法通过索引访问null值的使用使用;
3,Impossible WHERE noticed after reading const tables:MySQL Query Optimizer 通过收集到的统计信息判断出不可能存在结果;
4,No tables:Query 语句中使用FROM DUAL 或者不包含任何FROM 子句;
5,Not exists:在某些左连接中MySQL Query Optimizer 所通过改变原有Query 的组成而使用的优化方法,可以部分减少数据访问次数;
6,Select tables optimized away:当我们使用某些聚合函数来访问存在索引的某个字段的时候,MySQL Query Optimizer 会通过索引而直接一次定位到所需的数据行完成整个查询。当然,前提是在Query 中不能有GROUP BY 操作。如使用MIN()或者MAX()的时候;
7,Using filesort:当我们的Query 中包含ORDER BY 操作,而且无法利用索引完成排序操作的时候,MySQL Query Optimizer 不得不选择相应的排序算法来实现。
8,Using index:所需要的数据只需要在Index 即可全部获得而不需要再到表中取数据;
9,Using index for group-by:数据访问和Using index 一样,所需数据只需要读取索引即可,而当Query 中使用了GROUP BY 或者DISTINCT 子句的时候,如果分组字段也在索引中,Extra 中的信息就会是Using index for group-by;
10,Using temporary:当MySQL 在某些操作中必须使用临时表的时候,在Extra 信息中就会出现Using temporary 。主要常见于GROUP BY 和ORDER BY 等操作中。
11,Using where:如果我们不是读取表的所有数据,或者不是仅仅通过索引就可以获取所有需要的数据,则会出现Using where 信息;
12,Using where with pushed condition:这是一个仅仅在NDBCluster 存储引擎中才会出现的信息,而且还需要通过打开Condition Pushdown 优化功能才可能会被使用。控制参数为engine_condition_pushdown。
sending data表示收集+发送数据,通常产生的情况有一下几种:
存在大字段或返回数据量过大导致数据传输过慢。
sql可能没有走索引,扫了大量数据,从大量数据中找这一条记录。
数据库服务器网络问题。
show global status like 'open_files'; -- 15
show variables like 'open_files_limit'; 5000
open_files / open_files_limit * 100% <= 75%
select concat(15 / 5000 * 100, '%')
mysql [(none)]> show global status like 'create%tables';
| created_tmp_disk_tables | 11 |
| created_tmp_tables | 175 |
Created_tmp_disk_tables / Created_tmp_tables * 100% <= 25%比如上面的服务器Created_tmp_disk_tables / Created_tmp_tables * 100% =1.20%,应该相当好了
tmp_table_size=200M
show global status like 'sort%';
sort_merge_passes 49 --磁盘多次排序 Sort_buffer_size
sort_range 2
sort_rows 345472
sort_scan 42
mysql> SET GLOBAL sort_buffer_size = 1024*1024;
show global status like 'open%tables%';
Open_tables 179
Opened_tables 216
show variables like 'table_open_cache%';
| table_open_cache | 2000 |
| table_open_cache_instances | 16 |
open_tables / opened_tables * 100% >= 85%
open_tables / table_cache * 100% <= 95%
select 179 / 216 * 100 -- 32.6003 wrong >= 85%
select 252 / 2000 * 100 -- 12.6000 ok <= 95%
show full processlist;
show variables like '%max_connections%';
-- Sending data
show variables like '%buffer_pool_size';
show variables like '%query_cache%'
改配置文件设置 query_cache_type=YES,后发现查询速度依然很慢,所以这个方案失效
show variables like 'innodb_buffer_pool%';
query_cache_size=128M
query_cache_type=1
set profiling=on;
show profiles
SELECT * FROM INFORMATION_SCHEMA.PROFILING