-
Notifications
You must be signed in to change notification settings - Fork 111
format_statement()
xiaoboluo768 edited this page Jun 9, 2020
·
2 revisions
-
将长SQL语句文本截断为固定长度,该长度由配置变量@sys.statement_truncate_len控制,默认值在sys_config表中为64字节,如果语句文本少于statement_truncate_len,@sys.statement_truncate_len配置选项的长度则语句不会被截断,如果大于了配置选项指定的长度,则语句会被截断,执行截取操作时,中间部分被省略号替换(截取前30字节+'... '+ 后30字节,然后把这64字节中包含的\n字符替换为空格),该函数在其他视图中大量使用
- 该函数在其他视图,存储过程中大量使用,用于把performance_schema中的超长语句格式化为固定的长度
-
参数:
- statement LONGTEXT:需要执行格式化的SQL语句文本
-
配置选项:可以使用以下配置选项或其相应的用户定义变量来修改format_statement()函数的截断最大长度
- statement_truncate_len,@sys.statement_truncate_len:format_statement()函数返回的语句文本的最大长度。超长的语句文本将被截断。默认值为64字节
-
返回值:一个LONGTEXT长文本值
-
定义语句
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_path()函数 | 下一篇: format_time()函数
- 验证、测试、整理:罗小波
- QQ:309969177
- 提示:本系列文章的主体结构遵循Oracle MySQL 官方 5.7 手册中,关于information_schema、mysql schema、performance_schema、sys schema的章节结构体系,并额外添加了一些验证、测试数据。鉴于本人精力和能力有限,难免出现一些纰漏,欢迎大家踊跃指正!