Applications that monitor databases may make frequent use of Performance Schema tables. 监视数据库的应用程序可能频繁使用性能模式表。To write queries for these tables most efficiently, take advantage of their indexes. 要最有效地为这些表编写查询,请利用它们的索引。For example, include a 例如,包含WHERE
clause that restricts retrieved rows based on comparison to specific values in an indexed column.WHERE
子句,该子句基于与索引列中特定值的比较来限制检索到的行。
Most Performance Schema tables have indexes. 大多数性能模式表都有索引。Tables that do not are those that normally contain few rows or are unlikely to be queried frequently. 不包含行的表通常是包含很少行或不太可能经常查询的表。Performance Schema indexes give the optimizer access to execution plans other than full table scans. 性能模式索引使优化器能够访问除完整表扫描之外的执行计划。These indexes also improve performance for related objects, such as 这些索引还可以提高相关对象的性能,例如使用这些表的sys
schema views that use those tables.sys
架构视图。
To see whether a given Performance Schema table has indexes and what they are, use 要查看给定的性能架构表是否具有索引以及它们是什么,请使用SHOW INDEX
or SHOW CREATE TABLE
:SHOW INDEX
或SHOW CREATE TABLE
:
mysql>SHOW INDEX FROM performance_schema.accounts\G
*************************** 1. row *************************** Table: accounts Non_unique: 0 Key_name: ACCOUNT Seq_in_index: 1 Column_name: USER Collation: NULL Cardinality: NULL Sub_part: NULL Packed: NULL Null: YES Index_type: HASH Comment: Index_comment: Visible: YES *************************** 2. row *************************** Table: accounts Non_unique: 0 Key_name: ACCOUNT Seq_in_index: 2 Column_name: HOST Collation: NULL Cardinality: NULL Sub_part: NULL Packed: NULL Null: YES Index_type: HASH Comment: Index_comment: Visible: YES mysql>SHOW CREATE TABLE performance_schema.rwlock_instances\G
*************************** 1. row *************************** Table: rwlock_instances Create Table: CREATE TABLE `rwlock_instances` ( `NAME` varchar(128) NOT NULL, `OBJECT_INSTANCE_BEGIN` bigint(20) unsigned NOT NULL, `WRITE_LOCKED_BY_THREAD_ID` bigint(20) unsigned DEFAULT NULL, `READ_LOCKED_BY_COUNT` int(10) unsigned NOT NULL, PRIMARY KEY (`OBJECT_INSTANCE_BEGIN`), KEY `NAME` (`NAME`), KEY `WRITE_LOCKED_BY_THREAD_ID` (`WRITE_LOCKED_BY_THREAD_ID`) ) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
To see the execution plan for a Performance Schema query and whether it uses any indexes, use 要查看性能模式查询的执行计划以及它是否使用任何索引,请使用EXPLAIN
:EXPLAIN
:
mysql>EXPLAIN SELECT * FROM performance_schema.accounts
WHERE (USER,HOST) = ('root','localhost')\G
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: accounts partitions: NULL type: const possible_keys: ACCOUNT key: ACCOUNT key_len: 278 ref: const,const rows: 1 filtered: 100.00 Extra: NULL
The EXPLAIN
output indicates that the optimizer uses the accounts
table ACCOUNT
index that comprises the USER
and HOST
columns.EXPLAIN
输出表明优化器使用accounts
表ACCOUNT
索引,该索引包含USER
列和HOST
列。
Performance Schema indexes are virtual: They are a construct of the Performance Schema storage engine and use no memory or disk storage. 性能模式索引是虚拟的:它们是性能模式存储引擎的构造,不使用内存或磁盘存储。The Performance Schema reports index information to the optimizer so that it can construct efficient execution plans. 性能模式向优化器报告索引信息,以便优化器能够构建高效的执行计划。The Performance Schema in turn uses optimizer information about what to look for (for example, a particular key value), so that it can perform efficient lookups without building actual index structures. 性能模式反过来使用优化器关于要查找的内容的信息(例如,特定的键值),这样它就可以在不构建实际索引结构的情况下执行有效的查找。This implementation provides two important benefits:此实现提供了两个重要的好处:
It entirely avoids the maintenance cost normally incurred for tables that undergo frequent updates.它完全避免了频繁更新的表通常会产生的维护成本。
It reduces at an early stage of query execution the amount of data retrieved. 它在查询执行的早期阶段减少了检索到的数据量。For conditions on the indexed columns, the Performance Schema efficiently returns only table rows that satisfy the query conditions. 对于索引列上的条件,性能模式仅有效地返回满足查询条件的表行。Without an index, the Performance Schema would return all rows in the table, requiring that the optimizer later evaluate the conditions against each row to produce the final result.如果没有索引,性能模式将返回表中的所有行,要求优化器稍后针对每一行评估条件以生成最终结果。
Performance Schema indexes are predefined and cannot be dropped, added, or altered.性能架构索引是预定义的,不能删除、添加或更改。
Performance Schema indexes are similar to hash indexes. 性能模式索引类似于散列索引。For example:例如:
They are used only for equality comparisons that use the 它们仅用于使用=
or <=>
operators.=
或<=>
运算符的等于比较。
They are unordered. 它们是无序的。If a query result must have specific row ordering characteristics, include an 如果查询结果必须具有特定的行排序特征,请包含ORDER BY
clause.ORDER BY
子句。
For additional information about hash indexes, see Section 8.3.9, “Comparison of B-Tree and Hash Indexes”.有关散列索引的更多信息,请参阅第8.3.9节,“B-树和散列索引的比较”。