String and number conversion functions | A comprehensive understanding of the sys system library

In the previous articles of this series, we introduced the shortcut views and functions of the sys system library. At the beginning of this issue, we will introduce you to the functions of the sys system library.

PS: In the following, if the function definition text is shorter, the definition text of some functions will be listed, so that you can learn them more intuitively. If the function definition text is too long, please follow the download path introduced in the article "Getting to know the sys system library" .

| extract_schema_from_file_name()

Extract the schema name from the absolute path of the given file, this function is called in the sys.x$ps_schema_table_statistics_io view, of course you can also call it in a custom view

  • This function assumes that all the given data files are located in the datadir directory. Therefore, if the table is a partitioned table or a separate table option is used to define its own DATA_DIRECTORY path, then although the name of the database can be returned correctly, this will be used later When the database name is used in conjunction with other views, the corresponding table data file cannot be found under datadir

  • This function uses this path as an input parameter to extract the file I/O information in performance_schema when it has the full path of a data file. It is very practical. It provides a convenient way to get the schema name, which is easier to understand than the full path name. , And the returned schema name string value can be used for join query later

parameter:

  • path VARCHAR(512): A complete data file path used to extract the schema name

Return value: is a VARCHAR(64) string, that is, the schema name string

Function definition statement text

DROP FUNCTION IF EXISTS extract_schema_from_file_name;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION extract_schema_from_file_name (
    path VARCHAR(512)
)
RETURNS VARCHAR(64)
COMMENT '
        Description
        -----------

        Takes a raw file path, and attempts to extract the schema name from it.

        Useful for when interacting with Performance Schema data 
        concerning IO statistics, for example.

        Currently relies on the fact that a table data file will be within a 
        specified database directory (will not work with partitions or tables
        that specify an individual DATA_DIRECTORY).

        Parameters
        -----------

        path (VARCHAR(512)):
          The full file path to a data file to extract the schema name from.

        Returns
        -----------

        VARCHAR(64)

        Example
        -----------

        mysql> SELECT sys.extract_schema_from_file_name(\'/var/lib/mysql/employees/employee.ibd\');
        +----------------------------------------------------------------------------+
        | sys.extract_schema_from_file_name(\'/var/lib/mysql/employees/employee.ibd\') |
        +----------------------------------------------------------------------------+
        | employees                                                                  |
        +----------------------------------------------------------------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
NO SQL
BEGIN
RETURN LEFT(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(path, '\\', '/'), '/', -2), '/', 1), 64);
END$$
DELIMITER ;

| extract_table_from_file_name()

Extract the table name from the given file absolute path name, this function is called in the sys.x$ps_schema_table_statistics_io view, of course you can also call it in a custom view

  • This function uses this path as an incoming parameter to extract the file I/O information in performance_schema when it has the full path of a data file. It is very practical. It provides a convenient way to get the table name, which is easier to understand than the full path name. , And the returned table name string value can be used for join query later

parameter:

  • path VARCHAR(512): A complete data file path used to extract the table name

Return value: is a VARCHAR(64) string, that is, the table name string

Function definition statement text

DROP FUNCTION IF EXISTS extract_table_from_file_name;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION extract_table_from_file_name (
    path VARCHAR(512)
)
RETURNS VARCHAR(64)
COMMENT '
        Description
        -----------

        Takes a raw file path, and extracts the table name from it.

        Useful for when interacting with Performance Schema data 
        concerning IO statistics, for example.

        Parameters
        -----------

        path (VARCHAR(512)):
          The full file path to a data file to extract the table name from.

        Returns
        -----------

        VARCHAR(64)

        Example
        -----------

        mysql> SELECT sys.extract_table_from_file_name(\'/var/lib/mysql/employees/employee.ibd\');
        +---------------------------------------------------------------------------+
        | sys.extract_table_from_file_name(\'/var/lib/mysql/employees/employee.ibd\') |
        +---------------------------------------------------------------------------+
        | employee                                                                  |
        +---------------------------------------------------------------------------+
        1 row in set (0.02 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
NO SQL
BEGIN
RETURN LEFT(SUBSTRING_INDEX(REPLACE(SUBSTRING_INDEX(REPLACE(path, '\\', '/'), '/', -1), '@0024', '$'), '.', 1), 64);
END$$
DELIMITER ;

| format_bytes()

Convert byte values ​​to values ​​in other units (a more readable format), and automatically convert them into readable units such as KB, MB, GB, TB, and PB according to the size of the byte value (the return value is the converted value + Unit composition), this function is used extensively in other views

parameter:

  • bytes TEXT: the byte text value to be formatted

Return value: a TEXT text value

Function definition statement text

 DROP FUNCTION IF EXISTS format_bytes;
 DELIMITER $$
 CREATE DEFINER='root'@'localhost' FUNCTION format_bytes (
    -- We feed in and return TEXT here, as aggregates of
    -- bytes can return numbers larger than BIGINT UNSIGNED
    bytes TEXT
)
RETURNS TEXT
COMMENT '
        Description
        -----------

        Takes a raw bytes value, and converts it to a human readable format.

        Parameters
        -----------

        bytes (TEXT):
          A raw bytes value.

        Returns
        -----------

        TEXT

        Example
        -----------

        mysql> SELECT sys.format_bytes(2348723492723746) AS size;
        +----------+
        | size    |
        +----------+
        | 2.09 PiB |
        +----------+
        1 row in set (0.00 sec)

        mysql> SELECT sys.format_bytes(2348723492723) AS size;
        +----------+
        | size    |
        +----------+
        | 2.14 TiB |
        +----------+
        1 row in set (0.00 sec)

        mysql> SELECT sys.format_bytes(23487234) AS size;
        +-----------+
        | size      |
        +-----------+
        | 22.40 MiB |
        +-----------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
NO SQL
BEGIN
IF bytes IS NULL THEN RETURN NULL;
ELSEIF bytes >= 1125899906842624 THEN RETURN CONCAT(ROUND(bytes / 1125899906842624, 2), ' PiB');
ELSEIF bytes >= 1099511627776 THEN RETURN CONCAT(ROUND(bytes / 1099511627776, 2), ' TiB');
ELSEIF bytes >= 1073741824 THEN RETURN CONCAT(ROUND(bytes / 1073741824, 2), ' GiB');
ELSEIF bytes >= 1048576 THEN RETURN CONCAT(ROUND(bytes / 1048576, 2), ' MiB');
ELSEIF bytes >= 1024 THEN RETURN CONCAT(ROUND(bytes / 1024, 2), ' KiB');
ELSE RETURN CONCAT(ROUND(bytes, 0), ' bytes');
END IF;
END$$
DELIMITER ;

| format_path()

Replace the matching datadir, tmpdir, slave_load_tmpdir, innodb_data_home_dir, innodb_log_group_home_dir, basedir, innodb_undo_directory system variable values ​​in the path name with the variable symbol value. The given null value returns null, if the given value does not match, the original value is returned directly. This function is used in other Used extensively in the view

  • Before MySQL 5.7.14, the backslash () in the Windows path name will be converted to a forward slash (/) in the return value

parameter:

  • path VARCHAR(512): The full path name to be formatted and converted

Return value: a VARCHAR(512) CHARACTER SET utf8 value

Example

mysql> SELECT format_path('/usr/local/mysql/data/world/City.ibd');
+-----------------------------------------------------+
| format_path('/usr/local/mysql/data/world/City.ibd') |
+-----------------------------------------------------+
| @@datadir/world/City.ibd                            |
+-----------------------------------------------------+

| format_statement()

The long SQL statement text is truncated to a fixed length. The length is controlled by the configuration variable @sys.statement_truncate_len. The default value is 64 bytes in the sys_config table. If the statement text is less than statement_truncate_len, the statement is not the length of the @sys.statement_truncate_len configuration option It will be truncated. If it is greater than the length specified by the configuration option, the statement will be truncated. When the interception operation is performed, the middle part is replaced by an ellipsis (intercept the first 30 bytes +'... '+ the last 30 bytes, and then put this The \n characters contained in 64 bytes are replaced with spaces), this function is used extensively in other views

  • This function is widely used in other views and stored procedures to format the very long statements in performance_schema to a fixed length

parameter:

  • statement LONGTEXT: need to execute formatted SQL statement text

Configuration options: You can use the following configuration options or their corresponding user-defined variables to modify the maximum truncation length of the format_statement() function

  • statement_truncate_len, @sys.statement_truncate_len: The maximum length of the statement text returned by the format_statement() function. Very long sentence text will be truncated. The default value is 64 bytes

Return value: a LONGTEXT long text value

Function definition statement text

DROP FUNCTION IF EXISTS format_statement;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION format_statement (
    statement LONGTEXT
)
RETURNS LONGTEXT
COMMENT '
        Description
        -----------

        Formats a normalized statement, truncating it if it is > 64 characters long by default.

        To configure the length to truncate the statement to by default, update the `statement_truncate_len`
        variable with `sys_config` table to a different value. Alternatively, to change it just for just 
        your particular session, use `SET @sys.statement_truncate_len := <some new value>`.

        Useful for printing statement related data from Performance Schema from 
        the command line.

        Parameters
        -----------

        statement (LONGTEXT): 
          The statement to format.

        Returns
        -----------

        LONGTEXT

        Example
        -----------

        mysql> SELECT sys.format_statement(digest_text)
            ->  FROM performance_schema.events_statements_summary_by_digest
            ->  ORDER by sum_timer_wait DESC limit 5;
        +-------------------------------------------------------------------+
        | sys.format_statement(digest_text)                                |
        +-------------------------------------------------------------------+
        | CREATE SQL SECURITY INVOKER VI ... KE ? AND `variable_value` > ?  |
        | CREATE SQL SECURITY INVOKER VI ... ait` IS NOT NULL , `esc` . ... |
        | CREATE SQL SECURITY INVOKER VI ... ait` IS NOT NULL , `sys` . ... |
        | CREATE SQL SECURITY INVOKER VI ...  , `compressed_size` ) ) DESC  |
        | CREATE SQL SECURITY INVOKER VI ... LIKE ? ORDER BY `timer_start`  |
        +-------------------------------------------------------------------+
        5 rows in set (0.00 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
NO SQL
BEGIN
-- Check if we have the configured length, if not, init it
IF @sys.statement_truncate_len IS NULL THEN
  SET @sys.statement_truncate_len = sys_get_config('statement_truncate_len', 64);
END IF;

IF CHAR_LENGTH(statement) > @sys.statement_truncate_len THEN
  RETURN REPLACE(CONCAT(LEFT(statement, (@sys.statement_truncate_len/2)-2), ' ... ', RIGHT(statement, (@sys.statement_truncate_len/2)-2)), '\n', ' ');
ELSE
  RETURN REPLACE(statement, '\n', ' ');
END IF;
END$$
DELIMITER ;

| format_time()

Convert the picosecond value to other more readable unit values. According to the size of the picosecond value, it is automatically converted into readable units such as ns, us, ms, s, m, h, d, and w (the return value is converted by Value + unit composition), this function is used extensively in other views

parameter:

  • picoseconds TEXT: picoseconds text value to be converted into units

Return value: a TEXT text value

Function definition statement text

DROP FUNCTION IF EXISTS format_time;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION format_time (
    -- We feed in and return TEXT here, as aggregates of
    -- picoseconds can return numbers larger than BIGINT UNSIGNED
    picoseconds TEXT
)
RETURNS TEXT CHARSET UTF8
COMMENT '
        Description
        -----------

        Takes a raw picoseconds value, and converts it to a human readable form.

        Picoseconds are the precision that all latency values are printed in 
        within Performance Schema, however are not user friendly when wanting
        to scan output from the command line.

        Parameters
        -----------

        picoseconds (TEXT): 
          The raw picoseconds value to convert.

        Returns
        -----------

        TEXT

        Example
        -----------

        mysql> select format_time(342342342342345);
        +------------------------------+
        | format_time(342342342342345) |
        +------------------------------+
        | 00:05:42                    |
        +------------------------------+
        1 row in set (0.00 sec)

        mysql> select format_time(342342342);
        +------------------------+
        | format_time(342342342) |
        +------------------------+
        | 342.34 us              |
        +------------------------+
        1 row in set (0.00 sec)

        mysql> select format_time(34234);
          +--------------------+
        | format_time(34234) |
        +--------------------+
        | 34.23 ns          |
        +--------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
NO SQL
BEGIN
IF picoseconds IS NULL THEN RETURN NULL;
ELSEIF picoseconds >= 604800000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 604800000000000000, 2), ' w');
ELSEIF picoseconds >= 86400000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 86400000000000000, 2), ' d');
ELSEIF picoseconds >= 3600000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 3600000000000000, 2), ' h');
ELSEIF picoseconds >= 60000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 60000000000000, 2), ' m');
ELSEIF picoseconds >= 1000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000000, 2), ' s');
ELSEIF picoseconds >= 1000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000, 2), ' ms');
ELSEIF picoseconds >= 1000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000, 2), ' us');
ELSEIF picoseconds >= 1000 THEN RETURN CONCAT(ROUND(picoseconds / 1000, 2), ' ns');
ELSE RETURN CONCAT(picoseconds, ' ps');
END IF;
END$$
DELIMITER ;

| list_add()

Add the specified text to a list with a specified name, defined by two passing parameters, passing in a list and a string, and the return value is a list with the incoming string added to the incoming list, such as adding a session Level of sql_mode: set sql_mode=select sys.list_add(@@sql_mode,'ANSI_QUOTES'); Using this function to manipulate some list-type system variables can reduce manual assignment errors

  • This function and the list_drop() function together can be used to add and delete system variable values ​​with list type values, for example: sql_mode and optimizer_switch system variable values ​​have a comma-separated list value

  • This function was added in MySQL 5.7.9

parameter:

  • in_list TEXT: The name of the list variable whose value is to be modified. Note: This parameter can only be passed in the variable type value, not a string value. For details, see the example in the comment section of the function definition statement

  • in_add_value TEXT: the specific string value to be added to the list variable

Return value: a TEXT text value

Function definition statement text

DROP FUNCTION IF EXISTS list_add;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION list_add (
    in_list TEXT,
    in_add_value TEXT
)
RETURNS TEXT
COMMENT '
        Description
        -----------

        Takes a list, and a value to add to the list, and returns the resulting list.

        Useful for altering certain session variables, like sql_mode or optimizer_switch for instance.

        Parameters
        -----------

        in_list (TEXT):
          The comma separated list to add a value to

        in_add_value (TEXT):
          The value to add to the input list

        Returns
        -----------

        TEXT

        Example
        --------

        mysql> select @@sql_mode;
        +-----------------------------------------------------------------------------------+
        | @@sql_mode                                                                        |
        +-----------------------------------------------------------------------------------+
        | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
        +-----------------------------------------------------------------------------------+
        1 row in set (0.00 sec)

        mysql> set sql_mode = sys.list_add(@@sql_mode, ''ANSI_QUOTES'');
        Query OK, 0 rows affected (0.06 sec)

        mysql> select @@sql_mode;
        +-----------------------------------------------------------------------------------------------+
        | @@sql_mode                                                                                    |
        +-----------------------------------------------------------------------------------------------+
        | ANSI_QUOTES,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
        +-----------------------------------------------------------------------------------------------+
        1 row in set (0.00 sec)

        '
SQL SECURITY INVOKER
DETERMINISTIC
CONTAINS SQL
BEGIN

IF (in_add_value IS NULL) THEN
    SIGNAL SQLSTATE '02200'
      SET MESSAGE_TEXT = 'Function sys.list_add: in_add_value input variable should not be NULL',
          MYSQL_ERRNO = 1138;
END IF;

IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
    -- return the new value as a single value list
    RETURN in_add_value;
END IF;

RETURN (SELECT CONCAT(TRIM(BOTH ',' FROM TRIM(in_list)), ',', in_add_value));
END$$
DELIMITER ;

| list_drop()

Similar to the list_add() function, except that the incoming string is deleted from the incoming list

  • This function was added in MySQL 5.7.9

parameter:

  • in_list TEXT: The name of the list variable whose value is to be modified. Note: This parameter can only be passed in the variable type value, not a string value. For details, see the example in the comment section of the function definition statement

  • in_drop_value TEXT: The text value to be removed from the list variable

Return value: a TEXT text value

Function definition statement text

DROP FUNCTION IF EXISTS list_drop;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION list_drop (
    in_list TEXT,
    in_drop_value TEXT
)
RETURNS TEXT
COMMENT '
        Description
        -----------

        Takes a list, and a value to attempt to remove from the list, and returns the resulting list.

        Useful for altering certain session variables, like sql_mode or optimizer_switch for instance.

        Parameters
        -----------

        in_list (TEXT):
          The comma separated list to drop a value from

        in_drop_value (TEXT):
          The value to drop from the input list

        Returns
        -----------

        TEXT

        Example
        --------

        mysql> select @@sql_mode;
        +-----------------------------------------------------------------------------------------------+
        | @@sql_mode                                                                                    |
        +-----------------------------------------------------------------------------------------------+
        | ANSI_QUOTES,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
        +-----------------------------------------------------------------------------------------------+
        1 row in set (0.00 sec)

        mysql> set sql_mode = sys.list_drop(@@sql_mode, ''ONLY_FULL_GROUP_BY'');
        Query OK, 0 rows affected (0.03 sec)

        mysql> select @@sql_mode;
        +----------------------------------------------------------------------------+
        | @@sql_mode                                                                |
        +----------------------------------------------------------------------------+
        | ANSI_QUOTES,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
        +----------------------------------------------------------------------------+
        1 row in set (0.00 sec)

        '
SQL SECURITY INVOKER
DETERMINISTIC
CONTAINS SQL
BEGIN

IF (in_drop_value IS NULL) THEN
    SIGNAL SQLSTATE '02200'
      SET MESSAGE_TEXT = 'Function sys.list_drop: in_drop_value input variable should not be NULL',
          MYSQL_ERRNO = 1138;
END IF;

IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
    -- return the list as it was passed in
    RETURN in_list;
END IF;

-- ensure that leading / trailing commas are remove, support values with either spaces or not between commas
RETURN (SELECT TRIM(BOTH ',' FROM REPLACE(REPLACE(CONCAT(',', in_list), CONCAT(',', in_drop_value), ''), CONCAT(', ', in_drop_value), '')));
END$$
DELIMITER ;

The content of this issue is introduced here, and the reference link for the content of this issue is as follows:

https://dev.mysql.com/doc/refman/5.7/en/sys-list-drop.html

https://dev.mysql.com/doc/refman/5.7/en/sys-extract-schema-from-file-name.html

https://dev.mysql.com/doc/refman/5.7/en/sys-format-bytes.html

https://dev.mysql.com/doc/refman/5.7/en/sys-format-path.html

https://dev.mysql.com/doc/refman/5.7/en/sys-format-statement.html

https://dev.mysql.com/doc/refman/5.7/en/sys-format-time.html

https://dev.mysql.com/doc/refman/5.7/en/sys-list-add.html

https://dev.mysql.com/doc/refman/5.7/en/sys-extract-table-from-file-name.html

| About the author

Luo Xiaobo·Database Technology Expert

One of the authors of "A Thousand Gold Recipes-MySQL Performance Optimization Pyramid Rule", "Data Ecology: MySQL Replication Technology and Production Practice". Familiar with MySQL architecture, good at overall database tuning, like to specialize in open source technology, and keen on the promotion of open source technology, have done many public database topic sharing online and offline, and published nearly 100 database-related research articles.

The full text is over.

Enjoy MySQL :)

Teacher Ye's "MySQL Core Optimization" class has been upgraded to MySQL 8.0, scan the code to start the journey of MySQL 8.0 practice

Guess you like

Origin blog.csdn.net/n88Lpo/article/details/110411928