Documentation Home
MySQL 9.4 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 41.2Mb
PDF (A4) - 41.3Mb
Man Pages (TGZ) - 262.8Kb
Man Pages (Zip) - 368.8Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


MySQL 9.4 Reference Manual  / Functions and Operators  /  Performance Schema Functions

14.22 Performance Schema Functions

MySQL includes built-in SQL functions that format or retrieve Performance Schema data, and that may be used as equivalents for the correspondingsys schema stored functions. The built-in functions can be invoked in any schema and require no qualifier, unlike thesys functions, which require either asys. schema qualifier or thatsys be the current schema.

Table 14.32 Performance Schema Functions

NameDescription
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 thread ID for current thread
PS_THREAD_ID() Performance Schema thread ID for given thread

The built-in functions supersede the correspondingsys functions, which are deprecated; expect them to be removed in a future version of MySQL. Applications that use thesys functions should be adjusted to use the built-in functions instead, keeping in mind some minor differences between thesys functions and the built-in functions. For details about these differences, see the function descriptions in this section.

  • FORMAT_BYTES(count)

    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. ReturnsNULL ifcount isNULL.

    The units indicator depends on the size of the byte-count argument as shown in the following table.

    Argument ValueResult UnitsResult Units Indicator
    Up to 1023bytesbytes
    Up to 10242 − 1kibibytesKiB
    Up to 10243 − 1mebibytesMiB
    Up to 10244 − 1gibibytesGiB
    Up to 10245 − 1tebibytesTiB
    Up to 10246 − 1pebibytesPiB
    10246 and upexbibytesEiB
    mysql> SELECT FORMAT_BYTES(512), FORMAT_BYTES(18446644073709551615);+-------------------+------------------------------------+| FORMAT_BYTES(512) | FORMAT_BYTES(18446644073709551615) |+-------------------+------------------------------------+|  512 bytes        | 16.00 EiB                          |+-------------------+------------------------------------+

    FORMAT_BYTES() may be used instead of thesys schemaformat_bytes() function, keeping in mind this difference:

  • FORMAT_PICO_TIME(time_val)

    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.

    Iftime_val isNULL, this function returnsNULL.

    The units indicator depends on the size of the time-value argument as shown in the following table.

    Argument ValueResult UnitsResult Units Indicator
    Up to 103 − 1picosecondsps
    Up to 106 − 1nanosecondsns
    Up to 109 − 1microsecondsus
    Up to 1012 − 1millisecondsms
    Up to 60×1012 − 1secondss
    Up to 3.6×1015 − 1minutesmin
    Up to 8.64×1016 − 1hoursh
    8.64×1016 and updaysd
    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() may be used instead of thesys schemaformat_time() function, keeping in mind these differences:

  • PS_CURRENT_THREAD_ID()

    Returns aBIGINT UNSIGNED value representing the Performance Schema thread ID assigned to the current connection.

    The thread ID return value is a value of the type given in theTHREAD_ID column of Performance Schema tables.

    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() may be used as a shortcut for invoking thesys schemaps_thread_id() function with an argument ofNULL orCONNECTION_ID().

  • PS_THREAD_ID(connection_id)

    Given a connection ID, returns aBIGINT UNSIGNED value representing the Performance Schema thread ID assigned to the connection ID, orNULL if no thread ID exists for the connection ID. The latter can occur for threads that are not instrumented, or ifconnection_id isNULL.

    The connection ID argument is a value of the type given in thePROCESSLIST_ID column of the Performance Schemathreads table or theId column ofSHOW PROCESSLIST output.

    The thread ID return value is a value of the type given in theTHREAD_ID column of Performance Schema tables.

    Performance Schema configuration affectsPS_THREAD_ID() operation as follows. (These remarks also apply toPS_CURRENT_THREAD_ID().)

    mysql> SELECT PS_THREAD_ID(6);+-----------------+| PS_THREAD_ID(6) |+-----------------+|              45 |+-----------------+

    PS_THREAD_ID() may be used instead of thesys schemaps_thread_id() function, keeping in mind this difference: