Foreword:
Keep the empty cup spirit, use performance analysis, focus on measuring where the server’s time is spent, think about 1. How to confirm whether the server has reached the optimal performance state, 2. Why are statements not fast enough? Diagnosis of some intermittent and difficult faults described by users as "pauses, accumulations, and stucks";
Next, we will introduce some tools and techniques to optimize the performance of the entire machine and optimize the execution of a single statement Speed, diagnose and solve problems that are difficult to observe, show how to measure the system and generate profiling reports, and how to analyze the system stack;
Performance: required to complete a certain task Time measurement, in other words Performance is response time
Throughput: Query data per unit time (reciprocal of performance definition)
Step one:Clear the time Where is it, where is the time spent
If the answer is not found through measurement, the measurement method is wrong or not perfect enough, Only measure the activities that need to be optimized
Do not start or stop the test at the wrong time. What is measured is the aggregated information rather than the target activity itself; Effective optimization
3.1.1 Optimization through performance profilingPerformance profiling: the main method of measuring and analyzing where time is spent
1. Measuring tasks Time spent; 2. Statistics and sorting of results (important front row) Can group and summarize similar tasks, and obtain the required results through the performance analysis report; the report will list all tasks, and record one task per line : Task name, execution time, consumption time, average execution time, execution percentage of the total time; sorted in descending order by task consumption time; Performance analysis type: Analysis based on execution time: Which task has the longest execution timeAnalysis based on waiting: Determine where the task is blocked for the longest time3.1.2 Understanding performance analysis Missing but important information in performance analysis: 1. Queries worth optimizing Queries that account for a small proportion of the total response time are not worth optimizing; if the cost is greater than the benefit, optimization should be stopped 2, abnormal situation There must be optimized if it is not obvious. If the number of executions is small, but every time it is particularly slow,## 3, unknown unknown
Lost time: The difference between the total task time and the actual measured time. Even if no discovery is made, we should pay attention to the possibility of such problems.
4. Hidden details
Unable to display the distribution of all response times, more information, histograms, percentages, standard deviations, and deviation indexes
5. Unable to perform interactive analysis at higher levels of the stack
3.2 Performance analysis of applications: top-down
Influencing factors of performance bottlenecks:
1. External resources, calling external web services or search engines
3. Execute expensive operations in a loop: Abuse regularity
4. Use inefficient algorithm: brute force search algorithm
Recommendation: New projects should consider including performance profiling code
3.2.1 Measuring PHP application: empty
3.3 Profiling MySQL queries
3.3.1 Profiling the server Load
Analyze query log
From top to bottom, first generate the analysis report (pt-query-digest) and view the parts of special concern
3.3.2 Analyze a single query
After using SHOW PROFILE:MySQL5.1
Disabled by default, enabled: set profiling =1; then execute the statement on the server (turn off set profiling=off;)
Syntax:SHOW PROFILE [type [, type] ... ] [FOR QUERY n] [LIMIT row_count [OFFSET offset]] type: ALL --显示所有的开销信息 | BLOCK IO --显示块IO相关开销 | CONTEXT SWITCHES --上下文切换相关开销 | CPU --显示CPU相关开销信息 | IPC --显示发送和接收相关开销信息 | MEMORY --显示内存相关开销信息 | PAGE FAULTS --显示页面错误相关开销信息 | SOURCE --显示和Source_function,Source_file,Source_line相关的开销信息 | SWAPS --显示交换次数相关开销的信息
实质是这些开销信息被记录到information_schema.profiling表
show profiles;查看 show profile for query 2; 获取指定查询的开销(第二条查询开销明细) show profile cpu for query 2 ;查看特定部分的开销,如下为CPU部分的开销 show profile block io,cpu for query 2; 同时查看不同资源开销
Use SHOW STATUS: counter
Global show global status, based on a certain connection session level, the scope should pay attention to the counter shows the frequency of activity, commonly used: handle counter, temporary file, table counter
Use slow query log: [Source][Source]
Exceed the response time in MySQL
Exceed thethreshold The long_query_time statement is recorded in the slow query log (the log can be written to a file or database table. If the performance requirements are high, it is recommended to write a file). The default is 10s and needs to be turned on manually.
(1)slow_query_log的值为ON为开启慢查询日志,OFF则为关闭慢查询日志。
(2)slow_query_log_file 的值是记录的慢查询日志到文件中(注意:默认名为主机名.log,慢查询日志是否写入指定文件中,需要指定慢查询的输出日志格式为文件,相关命令为:show variables like ‘%log_output%’;去查看输出的格式)。
(3)long_query_time 指定了慢查询的阈值,即如果执行语句的时间超过该阈值则为慢查询语句,默认值为10秒。
(4)log_queries_not_using_indexes 如果值设置为ON,则会记录所有没有利用索引的查询(注意:如果只是将log_queries_not_using_indexes设置为ON,而将slow_query_log设置为OFF,此时该设置也不会生效,即该设置生效的前提是slow_query_log的值设置为ON),一般在性能调优的时候会暂时开启,开启后使用full index scan的sql也会被记录到慢查询日志。
//上述命令只对当前生效,当MySQL重启失效,如果要永久生效,需要配置my.cnf 查看输出格式:文件?表show variables like ‘%log_output%’; 开启通用日志查询: set global general_log=on; 关闭通用日志查询: set globalgeneral_log=off; 设置通用日志输出为表方式: set globallog_output=’TABLE’; 设置通用日志输出为文件方式: set globallog_output=’FILE’; 设置通用日志输出为表和文件方式:set global log_output=’FILE,TABLE’; 查询慢查询语句的个数:show global status like ‘%slow%’;
哪条语句导致慢查询(sql_text),该慢查询语句的查询时间(query_time),锁表时间(Lock_time),以及扫描过的行数(rows_examined)等信息。
perl mysqldumpslow –s c –t 10 slow-query.log
-s 表示按何种方式排序,c、t、l、r分别是按照记录次数、时间、查询时间、返回的记录数来排序,ac、at、al、ar,表示相应的倒叙;-t 表示top的意思,后面跟着的数据表示返回前面多少条;-g 后面可以写正则表达式匹配,大小写不敏感。
监视MySQL服务器,收集性能参数,且表的存储引擎PERFORMANCE_SCHEMA,低耗能
本地服务器,表是内存表,表内容在服务器启动时重新填充,关闭时丢弃,更改不会被复制或写入二进制日志
特性:
性能方案配置可被动态的执行SQL修改,立即影响到数据收集
监控服务事件:事件是服务做并被感知到的任何事,时间信息可被收集
数据库性能方案,提供对运行时数据库服务进行内部检查的方式,关注性能数据
特定于一个数据库服务,数据库表关联到数据服务,修改不会被备份也不写进二进制日志
存储引擎用“感知点”收集事件数据,且存储在performance_schema数据库,可通过select语句进行查询
mysql
包含权限配置,事件,存储引擎状态,主从信息,日志,时区信息,用户权限配置等
information_schema
对数据库元数据的抽象分析,由此提供了SQL语句方式来查询数据库运行时状态,每次对information_schema的查询都产生对metadata的互斥访问,影响其他数据库的访问性能。
performance_schema
内存型数据库,使用performance_schema 存储引擎,通过事件机制将mysql服务的运行时状态采集并存储在performace_schema数据库。注意,两个单词之间用下划线连接时,表示performance_schema是一个数据库;用空格分开时,表示一个数据库性能方案,也表示一个存储引擎。
相关文章:
The above is the detailed content of [MySQL Database] Interpretation of Chapter 3: Server Performance Analysis (Part 1). For more information, please follow other related articles on the PHP Chinese website!