mysql语句优化

利用 Explain和Profiling来优化

1.Explain

MySQL Query Optimizer(即MySQL Query优化器) 通过让们执行EXPLAIN 命令来告诉我们他将使用一个什么样的执行计划来优化我们的Query。所以,可以说Explain是在优化Query 时最直接有效的验证我们想法的工具。一个好的SQL Performance Tuner 在动手优化一个Query 之前,头脑中就应该已经有一个好的执行计划,后面的优化工作只是为实现该执行计划而作出各种调整。

explain使用参考:https://dev.mysql.com/doc/refman/5.7/en/explain.html

2.Profiling

Profiling是从 mysql5.0.3版本以后才开放的。但是在mysql5.7之后,profile信息将逐渐被废弃,mysql推荐使用performance schema(MySQL新增的性能优化引擎)。使用select version()查看mysql版本,以便使用合适的优化工具。
Query Profiler为MySQL Query诊断分析工具,通过该工具可以获取一条
Query 在整个执行过程中多种资源的消耗情况,如CPU,IO,IPC,SWAP 、PAGE FAULTS、CONTEXT SWITCHE 等等,可以帮助分析性能瓶颈。(SQL 语句执行所消耗的最大两部分资源就是IO和CPU)

在开启Query Profiler 功能之后,MySQL就会自动记录所有执行的语句的profile 信息了,包括错误语句的分析信息,但是不包括show profile和show profiles。

show profiles及show profile 显示语句的分析信息,这些信息指示当前会话过程中执行的语句的资源使用情况。

性能分析是基于每一个session的,一旦session结束,它的profing信息就将丢失。(原文:Profiling is enabled per session. When a session ends, its profiling information is lost.)

profiling功能对应于过程而不是线程。 这意味着,服务器内部活动的线程也会影响展示给你的profiling信息。(原文:profiling is per process and not per thread. This means that activity on threads within the server other than your own may affect the timing information that you see.)

除了show profiles、show profile,profiling信息还可以从information_schema数据库的profiling表获取,这个表是一个非标准的information_schema数据库的表,profiling表的内容对应于show profiles和show profile语句生成的信息,故以下两个语句是等价的。
语句一
show profile for query 2;
语句二
select state, format(duration, 6) as duration
from information_schema.profiling
where query_id = 2 order by seq;

术语

Profiling:性能分析
profiler: 性能分析器

profile相关命令

查看profile相关参数
show variables like '%profil%';

查看变量(session变量)
select @@profiling;

开启profileing功能
set profiling=1;

关闭profileing功能
set profiling=0;

设置保留Profiling的数目
set profiling_history_size=100;(其大小如设为0,则具有关闭profiling效果,最大值为100)

查看所有的profiles
show profiles;

查看profile的语法
SHOW PROFILE [type [, type] ... ]
    [FOR QUERY n]
    [LIMIT row_count [OFFSET offset]]

type: {
    ALL                                       displays all information
  | BLOCK IO                            displays counts for block input and output operations
  | CONTEXT SWITCHES        displays counts for voluntary and involuntary context switches
  | CPU                                       displays user and system CPU usage times
  | IPC                                        displays counts for messages sent and received
  | MEMORY                              not currently implemented
  | PAGE FAULTS                      displays counts for major and minor page faults
  | SOURCE                               displays the names of functions from the source code, together with the name and line number of the file in which the function occurs
  | SWAPS                                  displays swap counts
}

profile常见用法

show profile for query 2;
show profile cpu, block io for query 2;
show profile all for query 2;

3.Query Profiling Using Performance Schema

参照官方文档开启和使用profiling,涉及以下四个表。
setup_actors:描述主机、用户、帐户配置
setup_instruments:描述各种事件
setup_consumers:描述数据库下的表名以及开启监控的状态
events_statements_history_long:描述历史记录

首次使用前,执行3.1~3.3开启profiling功能。

3.1 performance_schema库的setup_actors表可用于限制主机、用户或帐户对历史事件的收集,以减少运行时开销和历史表中收集的数据量。
SELECT * FROM performance_schema.setup_actors;
INSERT INTO performance_schema.setup_actors (HOST,USER,ROLE,ENABLED,HISTORY) VALUES('localhost','test_user','%','YES','YES');
3.2 更新performance_schema库的setup_instruments表确保statement 和stage instrumentation启用。一些instruments可能默认就是启用的。
UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME LIKE '%statement/%';
UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME LIKE '%stage/%';
3.3 更新performance_schema库的setup_consumers表确保events_statements_* 和 events_stages_* consumers启用。一些consumers可能默认就是启用的。
UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%events_statements_%';
UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%events_stages_%';
3.4 在正在监视的用户帐户下,运行要profiling的语句。
image.png
3.5 通过查询events_statements_history_long表来标识语句的event ID。此步骤类似于运行SHOW PROFILES来标识query ID。下面的查询生成与SHOW PROFILES类似的输出。
SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%10001%';
image.png
3.6 查询events_stages_history_long表来检索语句的stage events
SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=31;
image.png

参考:《MySQL性能调优与架构设计》
参考:https://dev.mysql.com/doc/refman/5.7/en/show-profile.html
参考:https://dev.mysql.com/doc/refman/5.7/en/profiling-table.html
参考:https://dev.mysql.com/doc/refman/5.7/en/performance-schema-query-profiling.html

你可能感兴趣的:(mysql语句优化)