MySQL 包含内置的 SQL 函数,可用于格式化或检索 Performance Schema 数据,并且可以作为对应 sys 模式存储函数的等效函数使用。内置函数可以在任何模式中调用,不需要限定符,不像 sys 函数,需要 sys. 模式限定符或者 sys 必须是当前模式。MySQL includes built-in SQL functions that format or retrieve
Performance Schema data, and that may be used as equivalents for
the corresponding sys schema stored functions.
The built-in functions can be invoked in any schema and require no
qualifier, unlike the sys functions, which
require either a sys. schema qualifier or that
sys be the current schema.
表 14.31 Performance Schema 函数Table 14.31 Performance Schema Functions
| 名称Name | 说明Description |
|---|---|
FORMAT_BYTES() |
将字节数转换为带单位的数值Convert byte count to value with units |
FORMAT_PICO_TIME() |
将皮秒时间转换为带单位的数值Convert time in picoseconds to value with units |
PS_CURRENT_THREAD_ID() |
当前线程的 Performance Schema 线程 IDPerformance Schema thread ID for current thread |
PS_THREAD_ID() |
给定线程的 Performance Schema 线程 IDPerformance Schema thread ID for given thread |
内置函数取代了相应的sys函数,这些函数已被弃用;预计它们将在未来版本的 MySQL 中被移除。使用sys函数的应用程序应调整为改用内置函数,同时注意sys函数与内置函数之间的一些细微差别。有关这些差异的详细信息,请参阅本节中的函数描述。The built-in functions supersede the corresponding
sys functions, which are deprecated; expect
them to be removed in a future version of MySQL. Applications that
use the sys functions should be adjusted to use
the built-in functions instead, keeping in mind some minor
differences between the sys functions and the
built-in functions. For details about these differences, see the
function descriptions in this section.
给定一个数值字节数,将其转换为人类可读的格式,并返回一个由数值和单位标识组成的字符串。该字符串包含四舍五入到小数点后两位且至少有三个有效数字的字节数。小于 1024 字节的数字将以整数形式表示且不进行四舍五入。如果
count为NULL,则返回NULL。Given a numeric byte count, converts it to human-readable format and returns a string consisting of a value and a units indicator. The string contains the number of bytes rounded to 2 decimal places and a minimum of 3 significant digits. Numbers less than 1024 bytes are represented as whole numbers and are not rounded. ReturnsNULLifcountisNULL.单位指示器取决于字节计数参数的大小,如下表所示。The units indicator depends on the size of the byte-count argument as shown in the following table.
参数值Argument Value 结果单位Result Units 结果单位指示器Result Units Indicator 最多 1023Up to 1023 字节bytes 字节bytes 最多 10242 − 1Up to 10242 − 1 千字节(KiB,二进制千字节)kibibytes KiB 最多 10243 − 1Up to 10243 − 1 兆字节(MiB)mebibytes MiB 最多 10244 − 1Up to 10244 − 1 吉比字节gibibytes GiB 最多 10245 − 1Up to 10245 − 1 tebibytes TiB 最多 10246 − 1Up to 10246 − 1 佩比字节pebibytes PiB 10246及以上10246 and up 艾字节exbibytes EiB mysql>
SELECT FORMAT_BYTES(512), FORMAT_BYTES(18446644073709551615);+-------------------+------------------------------------+ | FORMAT_BYTES(512) | FORMAT_BYTES(18446644073709551615) | +-------------------+------------------------------------+ | 512 bytes | 16.00 EiB | +-------------------+------------------------------------+FORMAT_BYTES()可以代替sys模式下的format_bytes()函数,但需要注意以下区别:FORMAT_BYTES()may be used instead of thesysschemaformat_bytes()function, keeping in mind this difference:FORMAT_BYTES()使用EiB单位指标。sys.format_bytes()则不使用该指标。FORMAT_BYTES()uses theEiBunits indicator.sys.format_bytes()does not.
给定以皮秒为单位的数字性能架构延迟或等待时间,将其转换为人类可读的格式,并返回一个由数值和单位指示符组成的字符串。字符串包含四舍五入到两位小数的十进制时间,并且至少有三个有效数字。小于 1 纳秒的时间表示为整数,不进行四舍五入。Given a numeric Performance Schema latency or wait time in picoseconds, converts it to human-readable format and returns a string consisting of a value and a units indicator. The string contains the decimal time rounded to 2 decimal places and a minimum of 3 significant digits. Times under 1 nanosecond are represented as whole numbers and are not rounded.
如果
time_val是NULL,此函数返回NULL。Iftime_valisNULL, this function returnsNULL.单位指示器取决于时间值参数的大小,如下表所示。The units indicator depends on the size of the time-value argument as shown in the following table.
参数值Argument Value 结果单位Result Units 结果单位指示器Result Units Indicator 最高可达 103 − 1Up to 103 − 1 皮秒picoseconds 皮秒ps 最多 106 − 1Up to 106 − 1 纳秒nanoseconds ns 最高可达 109 − 1Up to 109 − 1 微秒microseconds 微秒 (us)us 最高可达 1012 − 1Up to 1012 − 1 毫秒milliseconds 毫秒ms 最多 60×1012 − 1Up to 60×1012 − 1 秒seconds ss 最多 3.6×1015 − 1Up to 3.6×1015 − 1 分钟minutes 分钟min 最多可达 8.64×1016 − 1Up to 8.64×1016 − 1 小时hours hh 8.64×1016及以上8.64×1016 and up 天days dd mysql>
SELECT FORMAT_PICO_TIME(3501), FORMAT_PICO_TIME(188732396662000);+------------------------+-----------------------------------+ | FORMAT_PICO_TIME(3501) | FORMAT_PICO_TIME(188732396662000) | +------------------------+-----------------------------------+ | 3.50 ns | 3.15 min | +------------------------+-----------------------------------+FORMAT_PICO_TIME()可以替代sysschema 的format_time()函数,但需要注意以下差异:FORMAT_PICO_TIME()may be used instead of thesysschemaformat_time()function, keeping in mind these differences:表示分钟时,
sys.format_time()使用m作为单位指示,而FORMAT_PICO_TIME()使用min。To indicate minutes,sys.format_time()uses themunits indicator, whereasFORMAT_PICO_TIME()usesmin.sys.format_time()使用w(周)单位指示符。FORMAT_PICO_TIME()不使用。sys.format_time()uses thew(weeks) units indicator.FORMAT_PICO_TIME()does not.
返回一个
BIGINT UNSIGNED值,表示分配给当前连接的 Performance Schema 线程 ID。Returns aBIGINT UNSIGNEDvalue representing the Performance Schema thread ID assigned to the current connection.线程 ID 返回值的类型与 Performance Schema 表中的
THREAD_ID列的类型相同。The thread ID return value is a value of the type given in theTHREAD_IDcolumn of Performance Schema tables.Performance Schema 配置对
PS_CURRENT_THREAD_ID()的影响与PS_THREAD_ID()相同。详情请参见该函数的描述。Performance Schema configuration affectsPS_CURRENT_THREAD_ID()the same way as forPS_THREAD_ID(). For details, see the description of that function.mysql>
SELECT PS_CURRENT_THREAD_ID();+------------------------+ | PS_CURRENT_THREAD_ID() | +------------------------+ | 52 | +------------------------+ mysql>SELECT PS_THREAD_ID(CONNECTION_ID());+-------------------------------+ | PS_THREAD_ID(CONNECTION_ID()) | +-------------------------------+ | 52 | +-------------------------------+PS_CURRENT_THREAD_ID()可用作调用sys模式下ps_thread_id()函数的快捷方式,其参数为NULL或CONNECTION_ID()。PS_CURRENT_THREAD_ID()may be used as a shortcut for invoking thesysschemaps_thread_id()function with an argument ofNULLorCONNECTION_ID().给定一个连接 ID,返回一个
BIGINT UNSIGNED值,表示分配给该连接 ID 的 Performance Schema 线程 ID,如果该连接 ID 没有线程 ID,则返回NULL。如果线程未被检测,或者connection_id为NULL,就可能出现后者情况。Given a connection ID, returns aBIGINT UNSIGNEDvalue representing the Performance Schema thread ID assigned to the connection ID, orNULLif no thread ID exists for the connection ID. The latter can occur for threads that are not instrumented, or ifconnection_idisNULL.连接 ID 参数是 Performance Schema
threads表中PROCESSLIST_ID列或Id输出中的SHOW PROCESSLIST列的类型值。The connection ID argument is a value of the type given in thePROCESSLIST_IDcolumn of the Performance Schemathreadstable or theIdcolumn ofSHOW PROCESSLISToutput.线程 ID 返回值的类型与 Performance Schema 表中的
THREAD_ID列的类型相同。The thread ID return value is a value of the type given in theTHREAD_IDcolumn of Performance Schema tables.Performance Schema 配置会影响
PS_THREAD_ID()的操作,如下所示。(这些说明也适用于PS_CURRENT_THREAD_ID()。)Performance Schema configuration affectsPS_THREAD_ID()operation as follows. (These remarks also apply toPS_CURRENT_THREAD_ID().)禁用
thread_instrumentation消费器会禁用在线程级别收集和汇总统计信息,但对PS_THREAD_ID()没有影响。Disabling thethread_instrumentationconsumer disables statistics from being collected and aggregated at the thread level, but has no effect onPS_THREAD_ID().如果
performance_schema_max_thread_instances不为 0,Performance Schema 会为线程统计分配内存,并为每个可用实例内存的线程分配一个内部 ID。如果有线程的实例内存不可用,PS_THREAD_ID()会返回NULL;此时,Performance_schema_thread_instances_lost不为零。Ifperformance_schema_max_thread_instancesis not 0, the Performance Schema allocates memory for thread statistics and assigns an internal ID to each thread for which instance memory is available. If there are threads for which instance memory is not available,PS_THREAD_ID()returnsNULL; in this case,Performance_schema_thread_instances_lostis nonzero.如果
performance_schema_max_thread_instances为 0,则 Performance Schema 不分配线程内存,并且PS_THREAD_ID()返回NULL。Ifperformance_schema_max_thread_instancesis 0, the Performance Schema allocates no thread memory andPS_THREAD_ID()returnsNULL.如果 Performance Schema 本身被禁用,
PS_THREAD_ID()会产生错误。If the Performance Schema itself is disabled,PS_THREAD_ID()produces an error.
mysql>
SELECT PS_THREAD_ID(6);+-----------------+ | PS_THREAD_ID(6) | +-----------------+ | 45 | +-----------------+PS_THREAD_ID()可以替代sys模式中的ps_thread_id()函数,但需要注意以下区别:PS_THREAD_ID()may be used instead of thesysschemaps_thread_id()function, keeping in mind this difference:当参数为
NULL时,sys.ps_thread_id()返回当前连接的线程 ID,而PS_THREAD_ID()返回NULL。要获取当前连接的线程 ID,请改用PS_CURRENT_THREAD_ID()。With an argument ofNULL,sys.ps_thread_id()returns the thread ID for the current connection, whereasPS_THREAD_ID()returnsNULL. To obtain the current connection thread ID, usePS_CURRENT_THREAD_ID()instead.