简单语句(非存储过程)的慢查询和general log等语句的来源为同源
- 词法解析前的SQL文本来源
parser_state.init(thd, thd->query().str, thd->query().length)
其来自thd->query().str - 慢查询
query_logger.slow_log_write(thd, thd->query().str, thd->query().length);
其来自thd->query().str - general log
query_logger.general_log_write(thd, COM_QUERY,thd->query().str, qlen);
3者来源相同为thd->query().str,而词法解析前的语句通常为原始语句。
而thd->query().str来源为:
thd->get_protocol()->get_command(&com_data, &command)
其来自传输协议中读取的文本,而传输协议不太可能做语句的转换,因此判断general log/慢查询中记录的简单语句(非存储过程)实际为应用传递过来的SQL语句。
调用栈:
- 慢查询
#0 File_query_log::write_slow (this=0x2f3b660, thd=0x7ffecc000b70, current_utime=1702170792812207, query_start_arg=1702127637748466, user_host=0x7fffe422d140 "root[root] @ localhost []",
user_host_len=25, query_utime=43155063741, lock_utime=43153706776, is_command=false, sql_text=0x7ffecc005bf0 "select * from t1", sql_text_len=16)
at /opt/percona-server-locks-detail-5.7.22/sql/log.cc:860
#1 0x000000000145c3eb in Log_to_file_event_handler::log_slow (this=0x2f3b1a0, thd=0x7ffecc000b70, current_utime=1702170792812207, query_start_arg=1702127637748466,
user_host=0x7fffe422d140 "root[root] @ localhost []", user_host_len=25, query_utime=43155063741, lock_utime=43153706776, is_command=false, sql_text=0x7ffecc005bf0 "select * from t1",
sql_text_len=16) at /opt/percona-server-locks-detail-5.7.22/sql/log.cc:1434
#2 0x000000000145c9dc in Query_logger::slow_log_write (this=0x2d89840 <query_logger>, thd=0x7ffecc000b70, query=0x7ffecc005bf0 "select * from t1", query_length=16)
at /opt/percona-server-locks-detail-5.7.22/sql/log.cc:1536
#3 0x000000000145dd24 in log_slow_do (thd=0x7ffecc000b70) at /opt/percona-server-locks-detail-5.7.22/sql/log.cc:1992
#4 0x000000000145dd53 in log_slow_statement (thd=0x7ffecc000b70) at /opt/percona-server-locks-detail-5.7.22/sql/log.cc:1999
#5 0x0000000001567c58 in dispatch_command (thd=0x7ffecc000b70, com_data=0x7fffe422dd90, command=COM_QUERY) at /opt/percona-server-locks-detail-5.7.22/sql/sql_parse.cc:1928
#6 0x00000000015655c5 in do_command (thd=0x7ffecc000b70) at /opt/percona-server-locks-detail-5.7.22/sql/sql_parse.cc:1021
#7 0x00000000016a635c in handle_connection (arg=0x8b63710) at /opt/percona-server-locks-detail-5.7.22/sql/conn_handler/connection_handler_per_thread.cc:312
#8 0x00000000018ce0f6 in pfs_spawn_thread (arg=0x711dac0) at /opt/percona-server-locks-detail-5.7.22/storage/perfschema/pfs.cc:2190
#9 0x00007ffff7bc6ea5 in start_thread () from /lib64/libpthread.so.0
#10 0x00007ffff66748dd in clone () from /lib64/libc.so.6
- general log
#0 Query_logger::general_log_write (this=0x2d89840 <query_logger>, thd=0x7ffecc000b70, command=COM_QUERY, query=0x7ffecc005bf0 "select * from t1", query_length=16)
at /opt/percona-server-locks-detail-5.7.22/sql/log.cc:1578
#1 0x000000000157199b in mysql_parse (thd=0x7ffecc000b70, parser_state=0x7fffe422d5b0) at /opt/percona-server-locks-detail-5.7.22/sql/sql_parse.cc:5843
#2 0x000000000156673d in dispatch_command (thd=0x7ffecc000b70, com_data=0x7fffe422dd90, command=COM_QUERY) at /opt/percona-server-locks-detail-5.7.22/sql/sql_parse.cc:1490
#3 0x00000000015655c5 in do_command (thd=0x7ffecc000b70) at /opt/percona-server-locks-detail-5.7.22/sql/sql_parse.cc:1021
#4 0x00000000016a635c in handle_connection (arg=0x8b63710) at /opt/percona-server-locks-detail-5.7.22/sql/conn_handler/connection_handler_per_thread.cc:312
#5 0x00000000018ce0f6 in pfs_spawn_thread (arg=0x711dac0) at /opt/percona-server-locks-detail-5.7.22/storage/perfschema/pfs.cc:2190
#6 0x00007ffff7bc6ea5 in start_thread () from /lib64/libpthread.so.0
#7 0x00007ffff66748dd in clone () from /lib64/libc.so.6
- 词法分析
#0 Lex_input_stream::reset (this=0x7fffe422d5b8, buffer=0x7ffecc005bf0 "select * from t1", length=16) at /opt/percona-server-locks-detail-5.7.22/sql/sql_lex.cc:214
#1 0x0000000001530b71 in Lex_input_stream::init (this=0x7fffe422d5b8, thd=0x7ffecc000b70, buff=0x7ffecc005bf0 "select * from t1", length=16)
at /opt/percona-server-locks-detail-5.7.22/sql/sql_lex.cc:197
#2 0x0000000000f0ceb2 in Parser_state::init (this=0x7fffe422d5b0, thd=0x7ffecc000b70, buff=0x7ffecc005bf0 "select * from t1", length=16)
at /opt/percona-server-locks-detail-5.7.22/sql/sql_lex.h:3678
#3 0x000000000156671b in dispatch_command (thd=0x7ffecc000b70, com_data=0x7fffe422dd90, command=COM_QUERY) at /opt/percona-server-locks-detail-5.7.22/sql/sql_parse.cc:1487
#4 0x00000000015655c5 in do_command (thd=0x7ffecc000b70) at /opt/percona-server-locks-detail-5.7.22/sql/sql_parse.cc:1021
#5 0x00000000016a635c in handle_connection (arg=0x8b63710) at /opt/percona-server-locks-detail-5.7.22/sql/conn_handler/connection_handler_per_thread.cc:312
#6 0x00000000018ce0f6 in pfs_spawn_thread (arg=0x711dac0) at /opt/percona-server-locks-detail-5.7.22/storage/perfschema/pfs.cc:2190
#7 0x00007ffff7bc6ea5 in start_thread () from /lib64/libpthread.so.0
#8 0x00007ffff66748dd in clone () from /lib64/libc.so.6
网友评论