技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

MySQL 中的查询统计信息

mysqldatabasebig data analytics

针对执行的查询收集的重要统计信息(包括时间、临时表、索引、连接等)都收集在查询统计信息 SQL 编辑器结果选项卡中(见以下两张图)。

要求

  • 启用查询并收集性能模式统计信息。

  • performance_schema 已启用语句检测。

SQL 编辑器:查询统计信息

可视化解释计划

通过利用增强型 JSON 格式中包含的附加数据,可视化解释功能可以创建并呈现 MySQL EXPLAIN 语句的可视化描述。所有 EXPLAIN 格式(包括标准格式、原始扩展 JSON 和可视化查询计划)均可在 MySQL Workbench 中用于执行查询。

以可视化方式演示使用

在 SQL 编辑器中执行查询,然后从查询结果菜单中选择"执行过程"以获取执行计划的可视化解释。除了默认的可视化解释视图外,执行计划还包含一个表格解释视图,该视图类似于您在 MySQL 客户端中运行 EXPLAIN 时看到的内容。有关 MySQL 如何执行语句的详细信息,请参阅使用 EXPLAIN 优化查询。

可视化解释约定

应从下到上、从左到右阅读可视化解释图。后面的图表示例概述了用于表示可视化解释计划的各个元素的图形、文本和信息约定。要了解更多详细信息,请参阅 −

  • 图形标准

  • 信息和文本约定

第一个图的视觉解释图提供了以下查询的视觉描述。

select  * FROM employee_table

可视化解释示例

图形约定

  • 标准框:表格

  • 圆角框:GROUP 和 SORT 等操作

  • 带框框:子查询

  • 菱形框:连接

文本和信息约定

  • 框下方的标准文本:表格(或别名)名称

  • 框下方的粗体文本:使用的键/索引

  • 右上角的数字框:过滤后从表中使用的行数

  • 框左上角的数字:访问该表的相对成本(需要 MySQL 5.7 或更高版本)

  • 嵌套循环(或哈希连接)菱形右侧的数字:JOIN 生成的行数

  • 菱形上方的数字:JOIN 的相对成本(需要 MySQL 5.7 或更高版本)

下表显示了可视化解释图中使用的相关颜色和描述。有关成本估算的更多信息,请参阅优化器成本模型。

可视化解释图信息

System Name

Color

Text on Visual Diagram

Tooltip Related Information

SYSTEM

Blue

Single row: system constant

Very low cost

CONST

Blue

Single row: constant

Very low cost

EQ_REF

Green

Unique Key Lookup

Low cost -- The optimizer is able to find an index that it can use to retrieve the required records. It is fast because the index search directly leads to the page with all the row data

REF

Green

Non-Unique Key Lookup

Low-medium -- Low if the number of matching rows is small; higher as the number of rows increases

FULLTEXT

Yellow

Fulltext Index Search

Specialized FULLTEXT search. Low -- for this specialized search requirement

REF_OR_NULL

Green

Key Lookup + Fetch NULL Values

Low-medium -- if the number of matching rows is small; higher as the number of rows increases

INDEX_MERGE

Green

Index Merge

Medium -- look for a better index selection in the query to improve performance

UNIQUE_SUBQUERY

Orange

Unique Key Lookup into table of subquery

Low -- Used for efficient Subquery processing

INDEX_SUBQUERY

Orange

Non-Unique Key Lookup into table of subquery

Low -- Used for efficient Subquery processing

RANGE

Orange

Index Range Scan

Medium -- partial index scan

INDEX

Red

Full Index Scan

High -- especially for large indexes

ALL

Red

Full Table Scan

Very High − very costly for large tables, but less of an impact for small ones. No usable indexes were found for the table, which forces the optimizer to search every row. This could also mean that the search range is so broad that the index would be useless.

UNKNOWN

Black

unknown

Note: This is the default, in case a match cannot be determined

结论

在本文中,我们了解了如何在 MySQL Workbench 中获取查询统计信息,以及它的不同特点。


相关文章