## 12.22 性能模式函数 [](<>)[](<>) 从 MySQL 8.0.16 开始,MySQL 包含内置 SQL 函数,用于格式化或检索 Performance Schema 数据,并且可以用作相应的等价物`系统`模式存储函数。内置函数可以在任何模式中调用,不需要限定符,不像`系统`功能,这需要要么`系统。`架构限定符或那个`系统`成为当前架构。 [](<>) **表 12.27 性能模式函数** | 姓名 | 描述 | 介绍 | | --- | --- | --- | | [`FORMAT_BYTES()`](performance-schema-functions.html#function_format-bytes) | 将字节数转换为单位值 | 8.0.16 | | [`FORMAT_PICO_TIME()`](performance-schema-functions.html#function_format-pico-time) | 以皮秒为单位将时间转换为单位值 | 8.0.16 | | [`PS_CURRENT_THREAD_ID()`](performance-schema-functions.html#function_ps-current-thread-id) | 当前线程的性能架构线程 ID | 8.0.16 | | [`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id) | 给定线程的性能架构线程 ID | 8.0.16 | 内置函数取代了相应的`系统`已弃用的功能;期望它们在 MySQL 的未来版本中被删除。使用`系统`应该调整函数以使用内置函数,记住两者之间的一些细微差别`系统`函数和内置函数。有关这些差异的详细信息,请参见本节中的功能说明。 - [](<>) [`FORMAT_BYTES(*`数数`*)`](performance-schema-functions.html#function_format-bytes) [](<>) 给定一个数字字节数,将其转换为人类可读的格式并返回一个由一个值和一个单位指示符组成的字符串。该字符串包含四舍五入到小数点后 2 位的字节数和最少 3 位有效数字。小于 1024 字节的数字表示为整数且不四舍五入。 单位指示符取决于字节计数参数的大小,如下表所示。 | 参数值 | 结果单位 | 结果单位指示符 | | --- | ---- | ------- | | 高达 1023 | 字节 | 字节 | | 高达 10242− 1 | 千字节 | 千字节 | | 高达 10243− 1 | 兆字节 | 小米B | | 高达 10244− 1 | 千兆字节 | 千兆位 | | 高达 10245− 1 | 太字节 | 钛B | | 高达 10246− 1 | PB | PiB | | 10246及以上 | 亿字节 | EiB | ``` mysql> SELECT FORMAT_BYTES(512), FORMAT_BYTES(18446644073709551615); +-------------------+------------------------------------+ | FORMAT_BYTES(512) | FORMAT_BYTES(18446644073709551615) | +-------------------+------------------------------------+ | 512 bytes | 16.00 EiB | +-------------------+------------------------------------+ ``` [`FORMAT_BYTES()`](performance-schema-functions.html#function_format-bytes)在 MySQL 8.0.16 中添加。它可以用来代替`系统`图式[`格式字节()`](sys-format-bytes.html)函数,记住这个区别: - [`FORMAT_BYTES()`](performance-schema-functions.html#function_format-bytes)使用`EiB`单位指标。[`sys.format_bytes()`](sys-format-bytes.html)才不是。 - [](<>) [`FORMAT_PICO_TIME(*`time_val`*)`](performance-schema-functions.html#function_format-pico-time) [](<>) 给定一个以皮秒为单位的数字性能模式延迟或等待时间,将其转换为人类可读的格式并返回一个由值和单位指示符组成的字符串。该字符串包含四舍五入到小数点后 2 位的小数时间和至少 3 位有效数字。1 纳秒以下的时间表示为整数,不四舍五入。 单位指示符取决于时间值参数的大小,如下表所示。 | 参数值 | 结果单位 | 结果单位指示符 | | --- | ---- | ------- | | 高达103− 1 | 皮秒 | ps | | 高达106− 1 | 纳秒 | ns | | 高达109− 1 | 微秒 | 我们 | | 高达1012 − 1 | 毫秒 | 小姐 | | 高达 60×1012 − 1 | 秒 | s | | 高达 3.6×1015 − 1 | 分钟 | 分钟 | | 高达 8.64×1016 − 1 | 小时 | h | | 8.64×1016及以上 | 天 | d | ``` 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()`](performance-schema-functions.html#function_format-pico-time)在 MySQL 8.0.16 中添加。它可以用来代替`系统`图式[`格式时间()`](sys-format-time.html)功能,请记住以下差异: - 为了表示分钟,[`sys.format_time()`](sys-format-time.html)使用`米`单位指标,而[`FORMAT_PICO_TIME()`](performance-schema-functions.html#function_format-pico-time)用途`分钟`. - [`sys.format_time()`](sys-format-time.html)使用`w`(周)单位指标。[`FORMAT_PICO_TIME()`](performance-schema-functions.html#function_format-pico-time)才不是。 - [](<>) [`PS_CURRENT_THREAD_ID()`](performance-schema-functions.html#function_ps-current-thread-id) [](<>) 返回一个`BIGINT 未签名`表示分配给当前连接的性能模式线程 ID 的值。 线程 ID 返回值是`THREAD_ID`Performance Schema 表的列。 性能架构配置影响[`PS_CURRENT_THREAD_ID()`](performance-schema-functions.html#function_ps-current-thread-id)同样的方式[`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id).有关详细信息,请参阅该功能的说明。 ``` 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()`](performance-schema-functions.html#function_ps-current-thread-id)在 MySQL 8.0.16 中添加。它可以用作调用`系统`图式[`ps_thread_id()`](sys-ps-thread-id.html)带参数的函数`空值`要么[`CONNECTION_ID()`](information-functions.html#function_connection-id). - [](<>) [`PS_THREAD_ID(*`连接ID`*)`](performance-schema-functions.html#function_ps-thread-id) [](<>) 给定一个连接 ID,返回一个`BIGINT 未签名`表示分配给连接 ID 的性能模式线程 ID 的值,或`空值`如果连接 ID 不存在线程 ID。后者可能发生在未检测的线程上。 连接 ID 参数是在`PROCESSLIST_ID`性能模式的列[`线程`](performance-schema-threads-table.html)表或`Id`列[`显示进程列表`](show-processlist.html)输出。 线程 ID 返回值是`THREAD_ID`Performance Schema 表的列。 性能架构配置影响[`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id)操作如下。(这些注释也适用于[`PS_CURRENT_THREAD_ID()`](performance-schema-functions.html#function_ps-current-thread-id).) - 禁用`线程仪表`消费者禁止在线程级别收集和聚合统计信息,但对[`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id). - 如果[`performance_schema_max_thread_instances`](performance-schema-system-variables.html#sysvar_performance_schema_max_thread_instances)不为 0,Performance Schema 为线程统计分配内存,并为每个实例内存可用的线程分配一个内部 ID。如果有实例内存不可用的线程,[`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id)返回`空值`;在这种情况下,[`Performance_schema_thread_instances_lost`](performance-schema-status-variables.html#statvar_Performance_schema_thread_instances_lost)是非零的。 - 如果[`performance_schema_max_thread_instances`](performance-schema-system-variables.html#sysvar_performance_schema_max_thread_instances)为 0,Performance Schema 不分配线程内存,并且[`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id)返回`空值`. - 如果 Performance Schema 本身被禁用,[`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id)产生错误。 ``` mysql> SELECT PS_THREAD_ID(6); +-----------------+ | PS_THREAD_ID(6) | +-----------------+ | 45 | +-----------------+ ``` [`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id)在 MySQL 8.0.16 中添加。它可以用来代替`系统`图式[`ps_thread_id()`](sys-ps-thread-id.html)函数,记住这个区别: - 有一个论点`空值`,[`sys.ps_thread_id()`](sys-ps-thread-id.html)返回当前连接的线程 ID,而[`PS_THREAD_ID()`](performance-schema-functions.html#function_ps-thread-id)返回`空值`.要获取当前连接线程 ID,请使用[`PS_CURRENT_THREAD_ID()`](performance-schema-functions.html#function_ps-current-thread-id)反而。