1. 程式人生 > >配置查詢與執行緒追蹤函式|全方位認識 sys 系統庫

配置查詢與執行緒追蹤函式|全方位認識 sys 系統庫

不知不覺中,我們的"全方位認識 sys 系統庫" 系列文章已經接近尾聲了,在上一篇《字串與數字轉換函式|全方位認識 sys 系統庫》中,我們介紹了sys 系統庫中用於字串和數字格式化轉換的函式,本期的內容給大家介紹 sys 系統庫中的剩餘函式,這也是本系列文章的最後一篇。

PS:下文中如果函式定義文字較短的會列出部分函式的定義文字,以便大家更直觀地學習它們。過長的函式定義文字請自行按照《初相識|全方位認識 sys 系統庫》一文中介紹的下載路徑下載檢視。

 

1.ps_is_account_enabled()

檢查在performance_schema.setup_actors表中是否啟用了對應account的效能事件監控功能,返回YES或者NO

引數:

  • in_host VARCHAR(60):要檢查的帳戶的主機名

  • in_user VARCHAR(32):要檢查的帳戶的使用者名稱

返回值:一個列舉型別值,ENUM('YES','NO'),注意:返回值僅僅是依賴於在performance_schema.setup_actors表中找匹配記錄而不管該帳號是否在mysql.user表中存在,如果在setup_actors表中開啟了任意帳號的監控,那麼就算在mysql.user中不存在的使用者,也會返回YES

函式定義語句文字

DROP FUNCTION IF EXISTS ps_is_account_enabled;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION ps_is_account_enabled (
    in_host VARCHAR(60),
    in_user VARCHAR(32)
)
RETURNS ENUM('YES', 'NO')
COMMENT '
        Description
        -----------

        Determines whether instrumentation of an account is enabled 
        within Performance Schema.

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

        in_host VARCHAR(60): 
          The hostname of the account to check.
        in_user VARCHAR(32):
          The username of the account to check.

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

        ENUM(\'YES\', \'NO\', \'PARTIAL\')

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

        mysql> SELECT sys.ps_is_account_enabled(\'localhost\', \'root\');
        +------------------------------------------------+
        | sys.ps_is_account_enabled(\'localhost\', \'root\') |
        +------------------------------------------------+
        | YES                                            |
        +------------------------------------------------+
        1 row in set (0.01 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
READS SQL DATA
BEGIN
RETURN IF(EXISTS(SELECT 1
                  FROM performance_schema.setup_actors
                  WHERE (`HOST` = '%' OR in_host LIKE `HOST`)
                    AND (`USER` = '%' OR `USER` = in_user)
                    AND (`ENABLED` = 'YES')
                ),
          'YES', 'NO'
      );
END$$
DELIMITER ;

2.ps_is_consumer_enabled()

檢查指定的consumers配置知否真正生效。根據performance_schema.setup_consumers表中所有consumers的enabled欄位設定值,按照consumers生效優先順序依次檢查指定consumers所依賴的consumers配置項是否啟用,如果所依賴的consumers都啟用時才會返回該consumers為YES,而不僅僅是返回該指定的consumers在performance_schema.setup_consumers表中的enabled欄位值設定

  • 如果指定的consumers名稱無效,則返回NULL

引數:

  • in_consumer VARCHAR(64):要檢查是否啟用的consumers名稱字串

返回值:一個ENUM('YES','NO') 值

函式定義語句文字

DROP FUNCTION IF EXISTS ps_is_consumer_enabled;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION ps_is_consumer_enabled (
    in_consumer varchar(64)
)
RETURNS enum('YES', 'NO')
COMMENT '

        Determines whether a consumer is enabled (taking the consumer hierarchy into consideration)
        within the Performance Schema.

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

        in_consumer VARCHAR(64): 
          The name of the consumer to check.

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

        ENUM(\'YES\', \'NO\')

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

        mysql> SELECT sys.ps_is_consumer_enabled(\'events_stages_history\');
        +-----------------------------------------------------+
        | sys.ps_is_consumer_enabled(\'events_stages_history\') |
        +-----------------------------------------------------+
        | NO                                                  |
        +-----------------------------------------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
READS SQL DATA
BEGIN
RETURN (
    SELECT (CASE
              WHEN c.NAME = 'global_instrumentation' THEN c.ENABLED
              WHEN c.NAME = 'thread_instrumentation' THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
              WHEN c.NAME LIKE '%\_digest'          THEN IF(cg.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
              WHEN c.NAME LIKE '%\_current'          THEN IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES', 'YES', 'NO')
              ELSE IF(cg.ENABLED = 'YES' AND ct.ENABLED = 'YES' AND c.ENABLED = 'YES'
                      AND ( SELECT cc.ENABLED FROM performance_schema.setup_consumers cc WHERE NAME = CONCAT(SUBSTRING_INDEX(c.NAME, '_', 2), '_current')
                          ) = 'YES', 'YES', 'NO')
            END) AS IsEnabled
      FROM performance_schema.setup_consumers c
          INNER JOIN performance_schema.setup_consumers cg
          INNER JOIN performance_schema.setup_consumers ct
    WHERE cg.NAME      = 'global_instrumentation'
          AND ct.NAME  = 'thread_instrumentation'
          AND c.NAME    = in_consumer
  );
END$$
DELIMITER ;

3.ps_is_instrument_default_enabled()

在performance_schema.setup_instruments表中檢查指定instruments預設是否啟用,預設只啟用'wait/io/file/%'、'wait/io/table/%'、'statement/%'、'memory/performance_schema/%'、'wait/lock/table/sql/handler'、'idle'、'stage/innodb/%'、'stage/sql/copy to tmp table',傳入引數值不匹配這些instruments即表示是預設關閉的instruments(返回NO),如果匹配則表示是預設啟用的instruments(返回YES)

引數:

  • in_instrument VARCHAR(128):要檢查預設是否啟用的instruments的名稱字串

返回值:一個ENUM('YES','NO')值

函式定義語句文字

DROP FUNCTION IF EXISTS ps_is_instrument_default_enabled;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION ps_is_instrument_default_enabled (
    in_instrument VARCHAR(128)
)
RETURNS ENUM('YES', 'NO')
COMMENT '
        Description
        -----------

        Returns whether an instrument is enabled by default in this version of MySQL.

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

        in_instrument VARCHAR(128): 
          The instrument to check.

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

        ENUM(\'YES\', \'NO\')

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

        mysql> SELECT sys.ps_is_instrument_default_enabled(\'statement/sql/select\');
        +--------------------------------------------------------------+
        | sys.ps_is_instrument_default_enabled(\'statement/sql/select\') |
        +--------------------------------------------------------------+
        | YES                                                          |
        +--------------------------------------------------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE v_enabled ENUM('YES', 'NO');

-- Currently the same in all versions
SET v_enabled = IF(in_instrument LIKE 'wait/io/file/%'
                    OR in_instrument LIKE 'wait/io/table/%'
                    OR in_instrument LIKE 'statement/%'
                    OR in_instrument LIKE 'memory/performance_schema/%'
                    OR in_instrument IN ('wait/lock/table/sql/handler', 'idle')
          /*!50707
                    OR in_instrument LIKE 'stage/innodb/%'
                    OR in_instrument = 'stage/sql/copy to tmp table'
          */
                  ,
                  'YES',
                  'NO'
                );

RETURN v_enabled;
END$$
DELIMITER ;

4.ps_is_instrument_default_timed()

在performance_schema.setup_instruments表中檢查指定instruments是否啟用定時器功能,預設只啟用'wait/io/file/%'、'wait/io/table/%'、'statement/%'、'wait/lock/table/sql/handler'、'idle'、'stage/innodb/%'、'stage/sql/copy to tmp table'這些instruments的timed,傳入引數值不匹配這些instruments即表示是指定instruments的定時器功能預設是關閉的(返回NO),如果匹配則表示指定的instruments的定時器功能預設是啟用的(返回YES)

引數:

  • in_instrument VARCHAR(128):要檢查是否預設啟用定時器功能的instruments的名稱字串

返回值:一個ENUM('YES','NO')值

函式定義語句文字

DROP FUNCTION IF EXISTS ps_is_instrument_default_timed;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION ps_is_instrument_default_timed (
    in_instrument VARCHAR(128)
)
RETURNS ENUM('YES', 'NO')
COMMENT '
        Description
        -----------

        Returns whether an instrument is timed by default in this version of MySQL.

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

        in_instrument VARCHAR(128): 
          The instrument to check.

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

        ENUM(\'YES\', \'NO\')

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

        mysql> SELECT sys.ps_is_instrument_default_timed(\'statement/sql/select\');
        +------------------------------------------------------------+
        | sys.ps_is_instrument_default_timed(\'statement/sql/select\') |
        +------------------------------------------------------------+
        | YES                                                        |
        +------------------------------------------------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE v_timed ENUM('YES', 'NO');

-- Currently the same in all versions
SET v_timed = IF(in_instrument LIKE 'wait/io/file/%'
                    OR in_instrument LIKE 'wait/io/table/%'
                    OR in_instrument LIKE 'statement/%'
                    OR in_instrument IN ('wait/lock/table/sql/handler', 'idle')
          /*!50707
                    OR in_instrument LIKE 'stage/innodb/%'
                    OR in_instrument = 'stage/sql/copy to tmp table'
          */
                  ,
                  'YES',
                  'NO'
                );

RETURN v_timed;
END$$
DELIMITER ;

5.ps_is_thread_instrumented()

在performance_schema.threads表中檢查指定執行緒是否啟用了效能事件監測功能,給定引數值對應performance_schema.threads表中的PROCESSLIST_ID列或SHOW PROCESSLIST輸出的Id列值

  • 如果給定連線ID值在performance_schema.threads表中未查詢到,則返回UNKNOWN值,如果給定ID值為NULL則返回NULL

引數:

  • in_connection_id BIGINT UNSIGNED:連線ID。對應performance_schema.threads表中的PROCESSLIST_ID列值或SHOW PROCESSLIST輸出的Id列值

返回值:一個ENUM('YES','NO','UNKNOWN')值

函式定義語句文字

DROP FUNCTION IF EXISTS ps_is_thread_instrumented;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION ps_is_thread_instrumented (
    in_connection_id BIGINT UNSIGNED
) RETURNS ENUM('YES', 'NO', 'UNKNOWN')
COMMENT '
        Description
        -----------

        Checks whether the provided connection id is instrumented within Performance Schema.

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

        in_connection_id (BIGINT UNSIGNED):
          The id of the connection to check.

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

        ENUM(\'YES\', \'NO\', \'UNKNOWN\')

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

        mysql> SELECT sys.ps_is_thread_instrumented(CONNECTION_ID());
        +------------------------------------------------+
        | sys.ps_is_thread_instrumented(CONNECTION_ID()) |
        +------------------------------------------------+
        | YES                                            |
        +------------------------------------------------+
        '

SQL SECURITY INVOKER
NOT DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE v_enabled ENUM('YES', 'NO', 'UNKNOWN');

IF (in_connection_id IS NULL) THEN
    RETURN NULL;
END IF;

SELECT INSTRUMENTED INTO v_enabled
  FROM performance_schema.threads
WHERE PROCESSLIST_ID = in_connection_id;

IF (v_enabled IS NULL) THEN
    RETURN 'UNKNOWN';
ELSE
    RETURN v_enabled;
END IF;
END$$
DELIMITER ;

6.ps_thread_account()

在performance_schema.threads表中查詢並返回給定內部執行緒ID號相關聯的account名稱([email protected]_name)

  • 該函式在MySQL 5.7.9中新增

引數:

  • in_thread_id BIGINT UNSIGNED:指定一個內部執行緒ID,返回該內部執行緒ID相關聯的account名稱,該值與performance_schema.threads表中的thread_id列值對應

返回值:一個TEXT文字值

函式定義語句文字

DROP FUNCTION IF EXISTS ps_thread_account;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION ps_thread_account (
    in_thread_id BIGINT UNSIGNED
) RETURNS TEXT
COMMENT '
        Description
        -----------

        Return the [email protected] account for the given Performance Schema thread id.

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

        in_thread_id (BIGINT UNSIGNED):
          The id of the thread to return the account for.

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

        mysql> select thread_id, processlist_user, processlist_host from performance_schema.threads where type = ''foreground'';
          +-----------+------------------+------------------+
          | thread_id | processlist_user | processlist_host |
          +-----------+------------------+------------------+
          |        23 | NULL            | NULL            |
          |        30 | root            | localhost        |
          |        31 | msandbox        | localhost        |
          |        32 | msandbox        | localhost        |
          +-----------+------------------+------------------+
          4 rows in set (0.00 sec)

          mysql> select sys.ps_thread_account(31);
          +---------------------------+
          | sys.ps_thread_account(31) |
          +---------------------------+
          | [email protected]        |
          +---------------------------+
          1 row in set (0.00 sec)
        '

SQL SECURITY INVOKER
NOT DETERMINISTIC
READS SQL DATA
BEGIN
RETURN (SELECT IF(
                  type = 'FOREGROUND',
                  CONCAT(processlist_user, '@', processlist_host),
                  type
                ) AS account
          FROM `performance_schema`.`threads`
        WHERE thread_id = in_thread_id);
END$$
DELIMITER ;

7.ps_thread_id()

在performance_schema.threads表中查詢給定連線ID(processlist_id)的內部執行緒ID,如果給定連線ID為NULL值,則返回當前連線的內部執行緒ID

引數:

  • in_connection_id BIGINT UNSIGNED:要返回內部執行緒ID的連線的ID。 對應performance_schema.threads表中的PROCESSLIST_ID列或SHOW PROCESSLIST輸出的Id列值

返回值:一個BIGINT UNSIGNED值

函式定義語句文字

DROP FUNCTION IF EXISTS ps_thread_id;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION ps_thread_id (
    in_connection_id BIGINT UNSIGNED
) RETURNS BIGINT UNSIGNED
COMMENT '
        Description
        -----------

        Return the Performance Schema THREAD_ID for the specified connection ID.

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

        in_connection_id (BIGINT UNSIGNED):
          The id of the connection to return the thread id for. If NULL, the current
          connection thread id is returned.

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

        mysql> SELECT sys.ps_thread_id(79);
        +----------------------+
        | sys.ps_thread_id(79) |
        +----------------------+
        |                  98 |
        +----------------------+
        1 row in set (0.00 sec)

        mysql> SELECT sys.ps_thread_id(CONNECTION_ID());
        +-----------------------------------+
        | sys.ps_thread_id(CONNECTION_ID()) |
        +-----------------------------------+
        |                                98 |
        +-----------------------------------+
        1 row in set (0.00 sec)
        '

SQL SECURITY INVOKER
NOT DETERMINISTIC
READS SQL DATA
BEGIN
RETURN (SELECT THREAD_ID
          FROM `performance_schema`.`threads`
        WHERE PROCESSLIST_ID = IFNULL(in_connection_id, CONNECTION_ID())
      );
END$$
DELIMITER ;

8.ps_thread_stack()

在performance_schema下的events_statements_history_long、events_waits_history_long、events_stages_history_long表中查詢並返回指定內部執行緒ID的事件資訊(json格式返回),可以把這些事件資訊看作是該指定內部執行緒ID堆資訊

引數:

  • in_thread_id BIGINT:要跟蹤堆資訊的內部執行緒ID。該值對應performance_schema.threads表的THREAD_ID列值

  • in_verbose BOOLEAN:是否在輸出的事件堆資訊中包含事件的instruments所在的原始檔名和程式碼行號資訊

返回值:一個LONGTEXT CHARACTER SET latin1長字串值

示例

[email protected] : (none) 02:45:39> SELECT sys.ps_thread_stack(50, FALSE) AS thread_stack\G;
*************************** 1. row ***************************
thread_stack: {"rankdir": "LR","nodesep": "0.10","stack_created": "2017-09-09 14:47:50","mysql_version": "5.7.18-log","mysql_user": "[email protected]%","events": [{"nesting_event_id": "0", "event_id": "8", \
"timer_wait": 57.59, "event_info": "sql/commit", "wait_info": "commit\nerrors: 0\nwarnings: 0\nlock time: 0.00us\nrows affected: 0\nrows sent: 0\nrows examined: 0\ntmp tables: 0\ntmp disk tables: \
0\nselect scan: 0\nselect full join: 0\nselect full range join: 0\nselect range: 0\nselect range check: 0\nsort merge passes: 0\nsort rows: 0\nsort range: 0\nsort scan: 0\nno index used: FALSE\nno good index used: \
......
"event_type": "io/socket"},{"nesting_event_id": "42", "event_id": "87", "timer_wait": 0.81, "event_info": "sql/cleaning up", "wait_info": "", "source": "", "event_type": "stage"},{"nesting_event_id": "87", "event_id": \
"88", "timer_wait": 0.06, "event_info": "sql/THD::LOCK_thd_query\\n", "wait_info": "sql_class.cc:4320", "source": "sql_class.cc:4320", "event_type": "synch/mutex"}]}
1 row in set (0.00 sec)

9.ps_thread_trx_info()

在performance_schema下的events_transactions_current、events_transactions_history、events_statements_history表中查詢並返回指定內部執行緒ID的事務、語句事件資訊(json格式返回),這些事件資訊包括當前正在執行的事務以及已經執行完成的語句資訊(必須啟用events_transactions_current、events_transactions_history、events_statements_history對應的consumers配置才能夠獲取這些資訊)

  • 如果ps_thread_trx_info()函式輸出json格式字串長度超過預設的65535位元組長度,則返回json錯誤物件(如:{ "error": "Trx info truncated: Row 6 was cut by GROUP_CONCAT()" }),然後函式中會對該錯誤做進一步處理

  • 此函式在MySQL 5.7.9中新增

引數:

  • in_thread_id BIGINT UNSIGNED:用於返回事務、語句事件資訊的內部執行緒ID,該值與performance_schema.threads表中的THREAD_ID列值對應

配置選項:

  • ps_thread_trx_info.max_length,@sys.ps_thread_trx_info.max_length:控制ps_thread_trx_info()函式輸出的最大位元組長度,預設為65535位元組

返回值:一個LONGTEXT長文字值

示例

[email protected] : (none) 02:47:50> select sys.ps_thread_trx_info(50)\G
*************************** 1. row ***************************
sys.ps_thread_trx_info(50): [
{
"time": "10.99 m",
"state": "ACTIVE",
"mode": "READ WRITE",
"autocommitted": "NO",
"gtid": "AUTOMATIC",
"isolation": "READ COMMITTED",
"statements_executed": [
  {
    "sql_text": "select * from t_luoxiaobo limit 200",
    "time": "544.65 us",
    "schema": "luoxiaobo",
    "rows_examined": 200,
    "rows_affected": 0,
    "rows_sent": 200,
    "tmp_tables": 0,
    "tmp_disk_tables": 0,
    "sort_rows": 0,
    "sort_merge_passes": 0
   }
   ]
}
]
1 row in set (0.01 sec)

[email protected] : (none) 02:58:42> select sys.ps_thread_trx_info(50)\G
*************************** 1. row ***************************
sys.ps_thread_trx_info(50): [
{
"time": "12.51 m",
"state": "COMMITTED",
"mode": "READ WRITE",
"autocommitted": "NO",
"gtid": "AUTOMATIC",
"isolation": "READ COMMITTED",
"statements_executed": [
  {
    "sql_text": "select * from t_luoxiaobo limit 200",
    "time": "544.65 us",
    "schema": "luoxiaobo",
    "rows_examined": 200,
    "rows_affected": 0,
    "rows_sent": 200,
    "tmp_tables": 0,
    "tmp_disk_tables": 0,
    "sort_rows": 0,
    "sort_merge_passes": 0
  },
  {
    "sql_text": "commit",
    "time": "55.36 us",
    "schema": "luoxiaobo",
    "rows_examined": 0,
    "rows_affected": 0,
    "rows_sent": 0,
    "tmp_tables": 0,
    "tmp_disk_tables": 0,
    "sort_rows": 0,
    "sort_merge_passes": 0
  }
  ]
}
]
1 row in set (0.00 sec)

10.quote_identifier()

返回字串作為引用的識別符號,即給字串前後加上反撇,對於已存在的反撇直接替換為兩個反撇,當SQL語句文字中存在著保留字或者是反撇號(`)字元時,該函式可以快速幫助新增反撇

  • 該函式在MySQL 5.7.14中新增

引數:

  • in_identifier TEXT:要作為引用識別符號的文字字串

返回值:一個TEXT文字值

函式定義語句文字

DROP FUNCTION IF EXISTS quote_identifier;
DELIMITER $$
-- https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
-- Maximum supported length for any of the current identifiers in 5.7.5+ is 256 characters.
-- Before that, user variables could have any length.
--
-- Based on Paul Dubois' suggestion in Bug #78823/Bug #22011361.
CREATE DEFINER='root'@'localhost' FUNCTION quote_identifier(in_identifier TEXT)
RETURNS TEXT CHARSET UTF8
COMMENT '
        Description
        -----------

        Takes an unquoted identifier (schema name, table name, etc.) and
        returns the identifier quoted with backticks.

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

        in_identifier (TEXT):
          The identifier to quote.

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

        TEXT

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

        mysql> SELECT sys.quote_identifier(''my_identifier'') AS Identifier;
        +-----------------+
        | Identifier      |
        +-----------------+
        | `my_identifier` |
        +-----------------+
        1 row in set (0.00 sec)

        mysql> SELECT sys.quote_identifier(''my`idenfier'') AS Identifier;
        +----------------+
        | Identifier    |
        +----------------+
        | `my``idenfier` |
        +----------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
DETERMINISTIC
NO SQL
BEGIN
RETURN CONCAT('`', REPLACE(in_identifier, '`', '``'), '`');
END$$
DELIMITER ;

11.sys_get_config()

返回給定sys 系統庫配置選項名稱的設定值,兩個傳參,第一個是要檢視的配置變數名稱,第二個是傳入的預設值,如果在sys.sys_config表中查詢到了該變數的非null值,則直接返回,如果查詢到是null值,則使用第二個傳參返回(如果在sys.sys_config表中沒有查詢到第一個傳參的變數名---即可能傳入的配置選項名稱在sys.sys_config表中不存在,返回第二個傳參值)

  • 按照慣例,呼叫者在sys_get_config()函式之前需要先檢查相應的使用者定義變數是否存在並且是否非NULL。如果存在所需配置選項的自定義變數,且值不為NULL,該呼叫者直接使用自定義配置選項變數值而不是第哦啊用sys_get_config()函式讀取sys.sys_config表中的值,除此之外,其他情形都需要呼叫sys_get_config()函式讀取sys.sys_config表中的值並賦值給使用者自定義配置選項變數,以便下次優先使用自定義配置選項變數中的值而不是直接查詢sys.sys_config表中的值,關於配置選項和自定義配置選項變數詳見《配置表|全方位認識 sys 系統庫》

  • 當調這需要獲取配置選項值時,如果要檢查配置選項是否設定了自定義配置選項變數,那麼可以使用IFNULL(...)語句IF(...)THEN ... END IF;語句把這一些邏輯封裝在一個流程控制語句裡,但是,這兩個語句中,IFNULL(...)語句在需要反覆查詢一個配置選項值時其執行速度會顯著比IF(...)THEN ... END IF;語句慢,因為IFNULL(...)語句無法在加入一個幹活的邏輯在裡邊,IF(...)THEN ... END IF;語句可以把一些幹活的邏輯加入到裡邊,只在第一次呼叫時才需要去判斷自定義變數知否存在以及是否為NULL值(兩個語句如何使用詳見函式定義語句中的註釋示例文字)

引數:

  • in_variable_name VARCHAR(128):給定的配置選項名稱字串

  • in_default_value VARCHAR(128):如果在sys_config表中找不到給定的配置選項名稱,則返回該引數給定的值

返回值:一個VARCHAR(128)文字值

示例

-- Get the configuration value from sys.sys_config falling back on 128 if the option is not present in the table.
mysql> SELECT sys.sys_get_config(''statement_truncate_len'', 128) AS Value;
+-------+
| Value |
+-------+
| 64 |
+-------+
1 row in set (0.00 sec)

-- Check whether the option is already set, if not assign - IFNULL(...) one liner example.
mysql> SET @sys.statement_truncate_len = IFNULL(@sys.statement_truncate_len, sys.sys_get_config(''statement_truncate_len'', 64));
Query OK, 0 rows affected (0.00 sec)

-- Check whether the option is already set, if not assign - IF ... THEN ... END IF example.
IF (@sys.statement_truncate_len IS NULL) THEN
SET @sys.statement_truncate_len = sys.sys_get_config(''statement_truncate_len'', 64);
END IF;

12.version_major()

通過version()函式獲取並返回MySQL server的主版本號,該函式在MySQL 5.7.9中新增

  • 該函式呼叫時無需傳入任何引數

返回值:一個TINYINT UNSIGNED值

函式定義語句文字

DROP FUNCTION IF EXISTS version_major;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION version_major ()
RETURNS TINYINT UNSIGNED
COMMENT '
        Description
        -----------

        Returns the major version of MySQL Server.

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

        TINYINT UNSIGNED

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

        mysql> SELECT VERSION(), sys.version_major();
        +--------------------------------------+---------------------+
        | VERSION()                            | sys.version_major() |
        +--------------------------------------+---------------------+
        | 5.7.9-enterprise-commercial-advanced | 5                  |
        +--------------------------------------+---------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
NO SQL
BEGIN
RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', 1);
END$$
DELIMITER ;

13.version_minor()

通過version()函式獲取並返回MySQL server的次要版本號,該函式在MySQL 5.7.9中新增

  • 該函式執行時無需傳入任何引數

返回值:一個TINYINT UNSIGNED值

函式定義語句文字

DROP FUNCTION IF EXISTS version_minor;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION version_minor ()
RETURNS TINYINT UNSIGNED
COMMENT '
        Description
        -----------

        Returns the minor (release series) version of MySQL Server.

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

        TINYINT UNSIGNED

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

        mysql> SELECT VERSION(), sys.server_minor();
        +--------------------------------------+---------------------+
        | VERSION()                            | sys.version_minor() |
        +--------------------------------------+---------------------+
        | 5.7.9-enterprise-commercial-advanced | 7                  |
        +--------------------------------------+---------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
NO SQL
BEGIN
RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', 2), '.', -1);
END$$
DELIMITER ;

14.version_patch()

通過version()函式獲取並返回MySQL server的補丁版本號,該函式在MySQL 5.7.9中新增

  • 該函式執行時無需傳入任何引數

返回值:一個TINYINT UNSIGNED值

函式定義語句文字

DROP FUNCTION IF EXISTS version_patch;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION version_patch ()
RETURNS TINYINT UNSIGNED
COMMENT '
        Description
        -----------

        Returns the patch release version of MySQL Server.

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

        TINYINT UNSIGNED

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

        mysql> SELECT VERSION(), sys.version_patch();
        +--------------------------------------+---------------------+
        | VERSION()                            | sys.version_patch() |
        +--------------------------------------+---------------------+
        | 5.7.9-enterprise-commercial-advanced | 9                  |
        +--------------------------------------+---------------------+
        1 row in set (0.00 sec)
        '
SQL SECURITY INVOKER
NOT DETERMINISTIC
NO SQL
BEGIN
RETURN SUBSTRING_INDEX(SUBSTRING_INDEX(VERSION(), '-', 1), '.', -1);
END$$
DELIMITER ;

本期內容就介紹到這裡,本期內容參考連結如下:

  • https://dev.mysql.com/doc/refman/5.7/en/sys-version-patch.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-is-account-enabled.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-is-instrument-default-enabled.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-is-instrument-default-timed.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-is-thread-instrumented.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-thread-account.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-thread-id.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-thread-stack.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-thread-trx-info.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-quote-identifier.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-sys-get-config.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-version-major.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-version-minor.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-ps-is-consumer-enabled.html

     

 

| 作者簡介

羅小波·沃趣科技高階資料庫技術專家

IT從業多年,歷任運維工程師,高階運維工程師,運維經理,資料庫工程師,曾參與版本釋出系統,輕量級監控系統,運維管理平臺,資料庫管理平臺的設計與編寫,熟悉MySQL的體系結構時,InnoDB儲存引擎,喜好專研開源技術,追求完美。