Performance Schema Functions
The sys
schema was created to make it easier to use the Performance Schema. This included several functions, for example to convert the picoseconds used by the Performance Schema into human readable strings. In MySQL 8.0.16, three of these functions have been implemented as native functions in MySQL Server.
Why do away with the sys
schema functions? There are two reasons: performance and ease of use. The native functions are written in C++ like the rest of the server whereas the sys
schema functions were written as stored functions. Function written in C++ are inherently faster than stored functions. Additionally, that the functions are native means you no longer need to prefix them with sys.
to tell MySQL where to find the functions.
The table below shows the mapping from the sys
schema functions to the new native functions.
sys Schema | Native | Description |
format_bytes() | FORMAT_BYTES() | Scale bytes, for example 1024 bytes to 1 kiB. |
format_time() | FORMAT_PICO_TIME() | Convert picoseconds to human readable strings. |
ps_thread_id() | PS_THREAD_ID() PS_CURRENT_THREAD_ID() | Find the Performance Schema thread ID belonging to a connection. The PS_CURRENT_THREAD_ID() function does not take any arguments and returns – as the name suggest, the thread ID for the connection itself. |
A simple example using the new functions is:
mysql> SELECT PS_THREAD_ID(CONNECTION_ID()),
PS_CURRENT_THREAD_ID(),
TIMER_WAIT,
FORMAT_PICO_TIME(TIMER_WAIT),
SUM(CURRENT_NUMBER_OF_BYTES_USED),
FORMAT_BYTES(SUM(CURRENT_NUMBER_OF_BYTES_USED))
FROM events_statements_current
INNER JOIN memory_summary_by_thread_by_event_name
USING (THREAD_ID)
WHERE THREAD_ID = PS_CURRENT_THREAD_ID()
GROUP BY THREAD_ID, EVENT_ID\G
*************************** 1. row ***********************
PS_THREAD_ID(CONNECTION_ID()): 59
PS_CURRENT_THREAD_ID(): 59
TIMER_WAIT: 346074677
FORMAT_PICO_TIME(TIMER_WAIT): 346.07 us
SUM(CURRENT_NUMBER_OF_BYTES_USED): 1423655
FORMAT_BYTES(SUM(CURRENT_NUMBER_OF_BYTES_USED)): 1.36 MiB
1 row in set (0.0011 sec)
The example requires the performance_schema
to be the default schema.
As you can see, the PS_THREAD_ID()
function with the result of the CONNECTION_ID()
(returning the connection ID for the current connection) is the same as using PS_CURRENT_THREAD_ID()
. You can of course use the function with any connection ID. If the connection ID does not exist, the function returns NULL
.
The FORMAT_PICO_TIME()
function returns 346.07 us (u is used instead of here instead of μ for microseconds) instead of 346074677 which is the raw value for TIMER_WAIT
(the duration of the query at the time the data is retrieved). Similarly FORMAT_BYTES()
converts the bytes to MiB.
If you use the sys.ps_thread_id()
or sys.format_bytes()
functions, then the first time for each connection, there will be a warning that the function name is the same as for a native function, for example:
mysql> SELECT sys.format_bytes(1423655);
+---------------------------+
| sys.format_bytes(1423655) |
+---------------------------+
| 1.36 MiB |
+---------------------------+
1 row in set, 1 warning (0.0008 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Note
Code: 1585
Message: This function 'format_bytes' has the same name as a native function
1 row in set (0.0003 sec)
If you get these warnings from your own objects, you should update them to use the new native functions.
That's it. Hope you like the new functions.
Leave a Reply