MySQL慢查詢記錄原理和內(nèi)容解析


我的學(xué)習(xí)記錄,可能有誤請諒解主之,也提供了一些源碼接口供有興趣的朋友調(diào)試拂檩。
源碼版本:percona 5.7.14


本文并不準(zhǔn)備說明如何開啟記錄慢查詢芬迄,只是將一些重要的部分進(jìn)行解析便贵。如何記錄慢查詢可以自行參考官方文檔:

  • 5.4.5 The Slow Query Log

本文使用了Percona 版本開啟來了參數(shù)log_slow_verbosity菠镇,得到了更詳細(xì)的慢查詢信息。通常情況下信息沒有這么多承璃,但是一定是包含關(guān)系利耍,本文也會使用Percona的參數(shù)解釋說明一下這個參數(shù)的含義。

一绸硕、慢查詢中的時(shí)間

實(shí)際上慢查詢中的時(shí)間就是時(shí)鐘時(shí)間堂竟,是通過操作系統(tǒng)的命令獲得的時(shí)間,如下是Linux中獲取時(shí)間的方式

  while (gettimeofday(&t, NULL) != 0)
  {}
  newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
  return newtime;

實(shí)際上就是通過OS的API gettimeofday函數(shù)獲得的時(shí)間玻佩。

二出嘹、慢查詢記錄的依據(jù)

  1. long_query_time:如果執(zhí)行時(shí)間超過本參數(shù)設(shè)置記錄慢查詢。
  2. log_queries_not_using_indexes:如果語句未使用索引記錄慢查詢咬崔。
  3. log_slow_admin_statements:是否記錄管理語句税稼。(如ALTER TABLE,ANALYZE TABLE, CHECK TABLE, CREATE INDEX, DROP INDEX, OPTIMIZE TABLE, and REPAIR TABLE.)

本文主要討論long_query_time參數(shù)的含義。

三垮斯、long_query_time參數(shù)的具體含義

如果我們將語句的執(zhí)行時(shí)間定義為如下:

實(shí)際消耗時(shí)間 = 實(shí)際執(zhí)行時(shí)間+鎖等待消耗時(shí)間

那么long_query_time實(shí)際上界定的是實(shí)際執(zhí)行時(shí)間郎仆,所以有些情況下雖然語句實(shí)際消耗的時(shí)間很長但是是因?yàn)殒i等待時(shí)間較長而引起的,那么實(shí)際上這種語句也不會記錄到慢查詢兜蠕。

我們看一下log_slow_applicable函數(shù)的代碼片段:

res= cur_utime - thd->utime_after_lock;

  if (res > thd->variables.long_query_time)
    thd->server_status|= SERVER_QUERY_WAS_SLOW;
  else
    thd->server_status&= ~SERVER_QUERY_WAS_SLOW;

這里實(shí)際上清楚的說明了上面的觀點(diǎn)扰肌,是不是慢查詢就是通過這個函數(shù)進(jìn)行的判斷的,非常重要熊杨。我可以清晰的看到如下公式:

  • res (實(shí)際執(zhí)行時(shí)間 ) = cur_utime(實(shí)際消耗時(shí)間) - thd->utime_after_lock( 鎖等待消耗時(shí)間)

實(shí)際上在慢查詢中記錄的正是

  • Query_time:實(shí)際消耗時(shí)間
  • Lock_time:鎖等待消耗時(shí)間

但是是否是慢查詢其評判標(biāo)準(zhǔn)卻是實(shí)際執(zhí)行時(shí)間及Query_time - Lock_time

其中鎖等待消耗時(shí)間( Lock_time)我現(xiàn)在已經(jīng)知道的包括:

  1. MySQL層 MDL LOCK等待消耗的時(shí)間曙旭。(Waiting for table metadata lock)
  2. MySQL層 MyISAM表鎖消耗的時(shí)間。 (Waiting for table level lock)
  3. InnoDB層 行鎖消耗的時(shí)間晶府。

四桂躏、MySQL是如何記錄鎖時(shí)間

我們可以看到在公式中utime_after_lock( 鎖等待消耗時(shí)間Lock_time)的記錄也就成了整個公式的關(guān)鍵,那么我們試著進(jìn)行debug川陆。

1剂习、MySQL層utime_after_lock的記錄方式

不管是 MDL LOCK等待消耗的時(shí)間還是 MyISAM表鎖消耗的時(shí)間都是在MySQL層記錄的,實(shí)際上它只是記錄在函數(shù)mysql_lock_tables的末尾會調(diào)用的THD::set_time_after_lock進(jìn)行的記錄時(shí)間而已如下:

void set_time_after_lock()
  {
    utime_after_lock= my_micro_time();
    MYSQL_SET_STATEMENT_LOCK_TIME(m_statement_psi, (utime_after_lock - start_utime));
  }

那么這里可以解析為代碼運(yùn)行到mysql_lock_tables函數(shù)的末尾之前的所有的時(shí)間都記錄到utime_after_lock時(shí)間中较沪,實(shí)際上并不精確鳞绕。但是實(shí)際上MDL LOCK的獲取和MyISAM表鎖的獲取都包含在里面。所以即便是select語句也會看到Lock_time并不為0尸曼。下面是棧幀:

#0  THD::set_time_after_lock (this=0x7fff28012820) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_class.h:3414
#1  0x0000000001760d6d in mysql_lock_tables (thd=0x7fff28012820, tables=0x7fff28c16b58, count=1, flags=0) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/lock.cc:366
#2  0x000000000151dc1a in lock_tables (thd=0x7fff28012820, tables=0x7fff28c165b0, count=1, flags=0) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_base.cc:6700
#3  0x00000000017c4234 in Sql_cmd_delete::mysql_delete (this=0x7fff28c16b50, thd=0x7fff28012820, limit=18446744073709551615)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_delete.cc:136
#4  0x00000000017c84ba in Sql_cmd_delete::execute (this=0x7fff28c16b50, thd=0x7fff28012820) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_delete.cc:1389
#5  0x00000000015a7814 in mysql_execute_command (thd=0x7fff28012820, first_level=true) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:3729
#6  0x00000000015adcd6 in mysql_parse (thd=0x7fff28012820, parser_state=0x7ffff035b600) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836
#7  0x00000000015a1b95 in dispatch_command (thd=0x7fff28012820, com_data=0x7ffff035bd70, command=COM_QUERY)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447
#8  0x00000000015a09c6 in do_command (thd=0x7fff28012820) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010
2猾昆、InnoDB層的行鎖的utime_after_lock記錄方式

InnoDB引擎層調(diào)用通過thd_set_lock_wait_time調(diào)用thd_storage_lock_wait函數(shù)完成的棧幀如下:

#0  thd_storage_lock_wait (thd=0x7fff2c000bc0, value=9503561) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_class.cc:798
#1  0x00000000019a4b2a in thd_set_lock_wait_time (thd=0x7fff2c000bc0, value=9503561)
    at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:1784
#2  0x0000000001a4b50f in lock_wait_suspend_thread (thr=0x7fff2c088200) at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/lock/lock0wait.cc:363
#3  0x0000000001b0ec9b in row_mysql_handle_errors (new_err=0x7ffff0317d54, trx=0x7ffff2f2e5d0, thr=0x7fff2c088200, savept=0x0)
    at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/row/row0mysql.cc:772
#4  0x0000000001b4fe61 in row_search_mvcc (buf=0x7fff2c087640 "\377", mode=PAGE_CUR_G, prebuilt=0x7fff2c087ac0, match_mode=0, direction=0)
    at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/row/row0sel.cc:5940
#5  0x00000000019b3051 in ha_innobase::index_read (this=0x7fff2c087100, buf=0x7fff2c087640 "\377", key_ptr=0x0, key_len=0, find_flag=HA_READ_AFTER_KEY)
    at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:9104
#6  0x00000000019b4374 in ha_innobase::index_first (this=0x7fff2c087100, buf=0x7fff2c087640 "\377")
    at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:9551
#7  0x00000000019b462c in ha_innobase::rnd_next (this=0x7fff2c087100, buf=0x7fff2c087640 "\377")
    at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:9656
#8  0x0000000000f66f1b in handler::ha_rnd_next (this=0x7fff2c087100, buf=0x7fff2c087640 "\377") at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:3099
#9  0x00000000014c61b6 in rr_sequential (info=0x7ffff03189e0) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/records.cc:520
#10 0x00000000017c56c3 in Sql_cmd_delete::mysql_delete (this=0x7fff2c006ae8, thd=0x7fff2c000bc0, limit=1)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_delete.cc:454
#11 0x00000000017c84ba in Sql_cmd_delete::execute (this=0x7fff2c006ae8, thd=0x7fff2c000bc0) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_delete.cc:1389

函數(shù)本身還是很簡單自己看看就知道了就是相加而已如下:

void thd_storage_lock_wait(THD *thd, long long value)
{
  thd->utime_after_lock+= value;
}

五、Percona中的log_slow_verbosity參數(shù)

這是Percona的解釋:

Specifies how much information to include in your slow log. The value is a comma-delimited string, and can contain any combination of the following values:

  • microtime: Log queries with microsecond precision (mandatory).
  • query_plan: Log information about the query’s execution plan (optional).
  • innodb: Log InnoDB statistics (optional).
  • minimal: Equivalent to enabling just microtime.
  • standard: Equivalent to enabling microtime,innodb.
  • full: Equivalent to all other values OR’ed together.

總之在Percona中可以修改這個參數(shù)獲得更加詳細(xì)的信息大概的格式如下:

# Time: 2018-05-30T09:30:12.039775Z
# User@Host: root[root] @ localhost []  Id:    10
# Schema: test  Last_errno: 1317  Killed: 0
# Query_time: 19.254508  Lock_time: 0.001043  Rows_sent: 0  Rows_examined: 0  Rows_affected: 0
# Bytes_sent: 44  Tmp_tables: 0  Tmp_disk_tables: 0  Tmp_table_sizes: 0
# InnoDB_trx_id: 0
# QC_Hit: No  Full_scan: No  Full_join: No  Tmp_table: No  Tmp_table_on_disk: No
# Filesort: No  Filesort_on_disk: No  Merge_passes: 0
#   InnoDB_IO_r_ops: 0  InnoDB_IO_r_bytes: 0  InnoDB_IO_r_wait: 0.000000
#   InnoDB_rec_lock_wait: 0.000000  InnoDB_queue_wait: 0.000000
#   InnoDB_pages_distinct: 0
SET timestamp=1527672612;
select count(*) from z1 limit 1;

六骡苞、輸出的詳細(xì)解釋

本節(jié)將會進(jìn)行詳細(xì)的解釋垂蜗,全部的慢查詢的輸出都來自于函數(shù)File_query_log::write_slow 楷扬,有興趣的同學(xué)可以自己看看,我這里也會給出輸出的位置和含義贴见,其中含義部分可能給出的是源碼中的注釋烘苹。

1、第一部分時(shí)間
# Time: 2018-05-30T09:30:12.039775Z

對應(yīng)的代碼:

my_snprintf(buff, sizeof buff,"# Time: %s\n", my_timestamp);

其中my_timestamp取值來自于

thd->current_utime();

實(shí)際上就是:

 while (gettimeofday(&t, NULL) != 0)
  {}
  newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
  return newtime;

可以看到實(shí)際就是調(diào)用gettimeofday系統(tǒng)調(diào)用得到的系統(tǒng)當(dāng)前時(shí)間片部。

注意:
對于5.6來講還有一句判斷

if (current_time != last_time)

如果兩次打印的時(shí)間秒鐘一致則不會輸出時(shí)間镣衡,只有通過后面介紹的

SET timestamp=1527753496;

來判斷時(shí)間,5.7.14沒有看到這樣的代碼档悠。

2廊鸥、第二部分用戶信息
# User@Host: root[root] @ localhost []  Id:    10

對應(yīng)的代碼:

  buff_len= my_snprintf(buff, 32, "%5u", thd->thread_id());
    if (my_b_printf(&log_file, "# User@Host: %s  Id: %s\n", user_host, buff)
        == (uint) -1)
      goto err;
  }

user_host是一串字符串,參考代碼:

size_t user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
                                  sctx->priv_user().str
                                  ? sctx->priv_user().str : "",
                                  "[", sctx_user.length ? sctx_user.str :
                                  (thd->slave_thread ? "SQL_SLAVE" : ""),
                                  "] @ ",
                                  sctx_host.length ? sctx_host.str : "", " [",
                                  sctx_ip.length ? sctx_ip.str : "", "]",
                                  NullS) - user_host_buff);

解釋如下:

  • root: m_priv_user - The user privilege we are using. May be "" for anonymous user辖所。
  • [root]: m_user - user of the client, set to NULL until the user has been read from the connection惰说。
  • localhost: m_host - host of the client。
  • []:client IP m_ip - client IP缘回。
  • Id: 10 thd->thread_id()實(shí)際上就是show processlist出來的id吆视。
3、第三部分schema等信息
# Schema: test  Last_errno: 1317  Killed: 0

對應(yīng)的代碼:

 "# Schema: %s  Last_errno: %u  Killed: %u\n"
 (thd->db().str ? thd->db().str : ""),
  thd->last_errno, (uint) thd->killed,
  • Schema:
    m_db Name of the current (default) database.If there is the current (default) database, "db" contains its name. If there is no current (default) database, "db" is NULL and "db_length" is 0. In other words, "db", "db_length" must either be NULL, or contain a valid database name.

  • Last_errno:
    Variable last_errno contains the last error/warning acquired during query execution.

  • Killed: 這里代表的是終止的錯誤碼酥宴。源碼中如下:
    enum killed_state
    {
    NOT_KILLED=0,
    KILL_BAD_DATA=1,
    KILL_CONNECTION=ER_SERVER_SHUTDOWN,
    KILL_QUERY=ER_QUERY_INTERRUPTED,
    KILL_TIMEOUT=ER_QUERY_TIMEOUT,
    KILLED_NO_VALUE /* means neither of the states */
    };
    在錯誤碼中代表如下:
    { "ER_SERVER_SHUTDOWN", 1053, "Server shutdown in progress" },
    { "ER_QUERY_INTERRUPTED", 1317, "Query execution was interrupted" },
    { "ER_QUERY_TIMEOUT", 1886, "Query execution was interrupted, max_statement_time exceeded" },

4啦吧、第四部分執(zhí)行信息

這部分可能是大家最關(guān)心的部分,很多信息也是默認(rèn)輸出都會輸出的拙寡。

# Query_time: 19.254508  Lock_time: 0.001043  Rows_sent: 0  Rows_examined: 0  Rows_affected: 0
# Bytes_sent: 44  Tmp_tables: 0  Tmp_disk_tables: 0  Tmp_table_sizes: 0
# InnoDB_trx_id: 0

對應(yīng)代碼:

my_b_printf(&log_file,
                  "# Schema: %s  Last_errno: %u  Killed: %u\n"
                  "# Query_time: %s  Lock_time: %s  Rows_sent: %llu"
                  "  Rows_examined: %llu  Rows_affected: %llu\n"
                  "# Bytes_sent: %lu",
                  (thd->db().str ? thd->db().str : ""),
                  thd->last_errno, (uint) thd->killed,
                  query_time_buff, lock_time_buff,
                  (ulonglong) thd->get_sent_row_count(),
                  (ulonglong) thd->get_examined_row_count(),
                  (thd->get_row_count_func() > 0)
                  ? (ulonglong) thd->get_row_count_func() : 0,
                  (ulong) (thd->status_var.bytes_sent - thd->bytes_sent_old)
my_b_printf(&log_file,
                    "  Tmp_tables: %lu  Tmp_disk_tables: %lu  "
                    "Tmp_table_sizes: %llu",
                    thd->tmp_tables_used, thd->tmp_tables_disk_used,
                    thd->tmp_tables_size)
snprintf(buf, 20, "%llX", thd->innodb_trx_id);及thd->innodb_trx_id

  • Query_time:語句執(zhí)行的時(shí)間及實(shí)際消耗時(shí)間 授滓。
  • Lock_time:包含MDL lock和InnoDB row lock和MyISAM表鎖消耗時(shí)間的總和及鎖等待消耗時(shí)間。前面已經(jīng)進(jìn)行了描述(實(shí)際上也并不全是鎖等待的時(shí)間只是鎖等待包含在其中)肆糕。
我們來看看Query_time和Lock_time的源碼來源般堆,它們來自于Query_logger::slow_log_write函數(shù)如下:

    query_utime= (current_utime > thd->start_utime) ?
      (current_utime - thd->start_utime) : 0;
    lock_utime=  (thd->utime_after_lock > thd->start_utime) ?
      (thd->utime_after_lock - thd->start_utime) : 0;

下面是數(shù)據(jù)current_utime 的來源,

current_utime= thd->current_utime();
實(shí)際上就是:
while (gettimeofday(&t, NULL) != 0)
  {}
  newtime= (ulonglong)t.tv_sec * 1000000 + t.tv_usec;
  return newtime;
獲取當(dāng)前時(shí)間而已

對于thd->utime_after_lock的獲取我已經(jīng)在前文進(jìn)行了描述擎宝,不再解釋郁妈。
  • Rows_sent:發(fā)送給mysql客戶端的行數(shù)浑玛,下面是源碼中的解釋
    Number of rows we actually sent to the client

  • Rows_examined:InnoDB引擎層掃描的行數(shù),下面是源碼中的解釋绍申。(備注棧幀1)
    Number of rows read and/or evaluated for a statement. Used for slow log reporting.
    An examined row is defined as a row that is read and/or evaluated
    according to a statement condition, including increate_sort_index(). Rows may be counted more than once, e.g., a statement including ORDER BY could possibly evaluate the row in filesort() before reading it for e.g. update.

  • Rows_affected:涉及到修改的話(比如DML語句)這是受影響的行數(shù)。
    for DML statements: to the number of affected rows;
    for DDL statements: to 0.

  • Bytes_sent:發(fā)送給客戶端的實(shí)際數(shù)據(jù)的字節(jié)數(shù)顾彰,它來自于
    (ulong) (thd->status_var.bytes_sent - thd->bytes_sent_old)

  • Tmp_tables:臨時(shí)表的個數(shù)极阅。

  • Tmp_disk_tables:磁盤臨時(shí)表的個數(shù)。

  • Tmp_table_sizes:臨時(shí)表的大小涨享。

以上三個指標(biāo)來自于:

thd->tmp_tables_used
thd->tmp_tables_disk_used
thd->tmp_tables_size

這三個指標(biāo)增加的位置對應(yīng)在free_tmp_table函數(shù)中如下:

  thd->tmp_tables_used++;
  if (entry->file)
  {
      thd->tmp_tables_size += entry->file->stats.data_file_length;
      if (entry->file->ht->db_type != DB_TYPE_HEAP)
          thd->tmp_tables_disk_used++;
  }
  • InnoDB_trx_id:事物ID筋搏,也就是trx->id,/*!< transaction id */
5厕隧、第五部分優(yōu)化器相關(guān)信息
# QC_Hit: No  Full_scan: No  Full_join: No  Tmp_table: No  Tmp_table_on_disk: No
# Filesort: No  Filesort_on_disk: No  Merge_passes: 0

這一行來自于如下代碼:

 my_b_printf(&log_file,
                  "# QC_Hit: %s  Full_scan: %s  Full_join: %s  Tmp_table: %s  "
                  "Tmp_table_on_disk: %s\n"                             \
                  "# Filesort: %s  Filesort_on_disk: %s  Merge_passes: %lu\n",
                  ((thd->query_plan_flags & QPLAN_QC) ? "Yes" : "No"),
                  ((thd->query_plan_flags & QPLAN_FULL_SCAN) ? "Yes" : "No"),
                  ((thd->query_plan_flags & QPLAN_FULL_JOIN) ? "Yes" : "No"),
                  ((thd->query_plan_flags & QPLAN_TMP_TABLE) ? "Yes" : "No"),
                  ((thd->query_plan_flags & QPLAN_TMP_DISK) ? "Yes" : "No"),
                  ((thd->query_plan_flags & QPLAN_FILESORT) ? "Yes" : "No"),
                  ((thd->query_plan_flags & QPLAN_FILESORT_DISK) ? "Yes" : "No"),

這里注意一個處理的技巧奔脐,這里query_plan_flags中每一位都代表一個含義俄周,這樣存儲既能存儲足夠多的信息同時(shí)存儲空間也很小,是C/C++中常用的方式髓迎。

  • QC_Hit: No:是否query cache命中峦朗。
  • Full_scan: 此處相當(dāng)于Select_scan 的含義,是否進(jìn)行了全掃描包括using index排龄。
  • Full_join: 此處相當(dāng)于Select_full_join 的含義波势,是否被驅(qū)動表使用到了索引,如果沒有使用到索引則為YES橄维。

考慮如下的執(zhí)行計(jì)劃

mysql> desc select *,sleep(1) from testuin a,testuin1 b where a.id1=b.id1;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                                              |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
|  1 | SIMPLE      | a     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    5 |   100.00 | NULL                                               |
|  1 | SIMPLE      | b     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    5 |    20.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

如此輸出如下:

# QC_Hit: No  Full_scan: Yes  Full_join: Yes
  • Tmp_table:是否使用了臨時(shí)表尺铣,在函數(shù)create_tmp_table中設(shè)置。
  • Tmp_table_on_disk:是否使用了磁盤臨時(shí)表争舞,如果時(shí)候innodb引擎則在create_innodb_tmp_table函數(shù)中設(shè)置凛忿。
  • Filesort:是否進(jìn)行了排序,在函數(shù)filesort中設(shè)置兑障。
  • Filesort_on_disk:是否使用了磁盤排序侄非,同樣在函數(shù)filesort中設(shè)置,但是設(shè)置之前會進(jìn)行是否需要磁盤排序文件的判斷流译。
  • Merge_passes: 進(jìn)行多路歸并排序逞怨,歸并的次數(shù)。
    Variable query_plan_fsort_passes collects information about file sort passes
    acquired during query execution.
6福澡、第六部分InnoDB相關(guān)信息
#   InnoDB_IO_r_ops: 0  InnoDB_IO_r_bytes: 0  InnoDB_IO_r_wait: 0.000000
#   InnoDB_rec_lock_wait: 0.000000  InnoDB_queue_wait: 0.000000
#   InnoDB_pages_distinct: 0

這一行來自于如下代碼:

 char buf[3][20];
    snprintf(buf[0], 20, "%.6f", thd->innodb_io_reads_wait_timer / 1000000.0);
    snprintf(buf[1], 20, "%.6f", thd->innodb_lock_que_wait_timer / 1000000.0);
    snprintf(buf[2], 20, "%.6f", thd->innodb_innodb_que_wait_timer / 1000000.0);
    if (my_b_printf(&log_file,
                    "#   InnoDB_IO_r_ops: %lu  InnoDB_IO_r_bytes: %llu  "
                    "InnoDB_IO_r_wait: %s\n"
                    "#   InnoDB_rec_lock_wait: %s  InnoDB_queue_wait: %s\n"
                    "#   InnoDB_pages_distinct: %lu\n",
                    thd->innodb_io_reads, thd->innodb_io_read,
                    buf[0], buf[1], buf[2], thd->innodb_page_access)
        == (uint) -1)
  • InnoDB_IO_r_ops:物理IO讀取次數(shù)叠赦。
  • InnoDB_IO_r_bytes:物理IO讀取的總字節(jié)數(shù)。
  • InnoDB_IO_r_wait:物理IO讀取等待的時(shí)間革砸。innodb 使用 BUF_IO_READ標(biāo)記為物理io讀取繁忙除秀,參考函數(shù)buf_wait_for_read。
  • InnoDB_rec_lock_wait:等待行鎖消耗的時(shí)間算利。在函數(shù)que_thr_end_lock_wait中設(shè)置册踩。
  • InnoDB_queue_wait: 等待進(jìn)入innodb引擎消耗的時(shí)間,在函數(shù)srv_conc_enter_innodb_with_atomics中設(shè)置效拭。(參考http://blog.itpub.net/7728585/viewspace-2140446/)
  • InnoDB_pages_distinct: innodb訪問的頁數(shù)暂吉,包含物理和邏輯IO,在函數(shù)buf_page_get_gen的末尾通過_increment_page_get_statistics函數(shù)設(shè)置缎患。

7慕的、第七部分set timestamp

SET timestamp=1527753496;

這一句來自源碼,注意源碼注釋解釋就是獲取的服務(wù)器的當(dāng)前的時(shí)間(current_utime)挤渔。

/*
    This info used to show up randomly, depending on whether the query
    checked the query start time or not. now we always write current
    timestamp to the slow log
  */
  end= my_stpcpy(end, ",timestamp=");
  end= int10_to_str((long) (current_utime / 1000000), end, 10);

  if (end != buff)
  {
    *end++=';';
    *end='\n';
    if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
        my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
      goto err;
  }      

七肮街、總結(jié)
本文通過查詢源碼解釋了一些關(guān)于MySQL慢查詢的相關(guān)的知識,主要解釋了慢查詢是基于什么標(biāo)準(zhǔn)進(jìn)行記錄的判导,同時(shí)輸出中各個指標(biāo)的含義嫉父,當(dāng)然這僅僅是我自己得出的結(jié)果沛硅,如果有不同意見可以一起討論。

備注棧幀1:
本棧幀主要跟蹤Rows_examined的變化及 join->examined_rows++;的變化

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000ebd5f3 in main(int, char**) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25
        breakpoint already hit 1 time
4       breakpoint     keep y   0x000000000155b94f in do_select(JOIN*) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_executor.cc:872
        breakpoint already hit 5 times
5       breakpoint     keep y   0x000000000155ca39 in evaluate_join_record(JOIN*, QEP_TAB*) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_executor.cc:1473
        breakpoint already hit 20 times
6       breakpoint     keep y   0x00000000019b4313 in ha_innobase::index_first(uchar*)
                                               at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:9547
        breakpoint already hit 4 times
7       breakpoint     keep y   0x00000000019b45cd in ha_innobase::rnd_next(uchar*)
                                               at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:9651
8       breakpoint     keep y   0x00000000019b2ba6 in ha_innobase::index_read(uchar*, uchar const*, uint, ha_rkey_function)
                                               at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:9004
        breakpoint already hit 3 times
9       breakpoint     keep y   0x00000000019b4233 in ha_innobase::index_next(uchar*)
                                               at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:9501
        breakpoint already hit 5 times

#0  ha_innobase::index_next (this=0x7fff2cbc6b40, buf=0x7fff2cbc7080 "\375\n") at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:9501
#1  0x0000000000f680d8 in handler::ha_index_next (this=0x7fff2cbc6b40, buf=0x7fff2cbc7080 "\375\n") at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:3269
#2  0x000000000155fa02 in join_read_next (info=0x7fff2c007750) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_executor.cc:2660
#3  0x000000000155c397 in sub_select (join=0x7fff2c007020, qep_tab=0x7fff2c007700, end_of_records=false)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_executor.cc:1274
#4  0x000000000155bd06 in do_select (join=0x7fff2c007020) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_executor.cc:944
#5  0x0000000001559bdc in JOIN::exec (this=0x7fff2c007020) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_executor.cc:199
#6  0x00000000015f9ea6 in handle_query (thd=0x7fff2c000b70, lex=0x7fff2c003150, result=0x7fff2c006cd0, added_options=0, removed_options=0)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_select.cc:184
#7  0x00000000015acd05 in execute_sqlcom_select (thd=0x7fff2c000b70, all_tables=0x7fff2c006688) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5391
#8  0x00000000015a5320 in mysql_execute_command (thd=0x7fff2c000b70, first_level=true) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:2889
#9  0x00000000015adcd6 in mysql_parse (thd=0x7fff2c000b70, parser_state=0x7ffff035b600) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836
#10 0x00000000015a1b95 in dispatch_command (thd=0x7fff2c000b70, com_data=0x7ffff035bd70, command=COM_QUERY)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447
#11 0x00000000015a09c6 in do_command (thd=0x7fff2c000b70) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010
#12 0x00000000016e29d0 in handle_connection (arg=0x3859ae0) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:312
#13 0x0000000001d7bfdc in pfs_spawn_thread (arg=0x38607b0) at /root/mysql5.7.14/percona-server-5.7.14-7/storage/perfschema/pfs.cc:2188
#14 0x0000003f74807aa1 in start_thread () from /lib64/libpthread.so.0
#15 0x0000003f740e8bcd in clone () from /lib64/libc.so.6

作者微信:


微信.jpg
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末绕辖,一起剝皮案震驚了整個濱河市稽鞭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌引镊,老刑警劉巖朦蕴,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異弟头,居然都是意外死亡吩抓,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進(jìn)店門赴恨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疹娶,“玉大人,你說我怎么就攤上這事伦连∮杲龋” “怎么了?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵惑淳,是天一觀的道長额港。 經(jīng)常有香客問我,道長歧焦,這世上最難降的妖魔是什么移斩? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮绢馍,結(jié)果婚禮上向瓷,老公的妹妹穿的比我還像新娘。我一直安慰自己舰涌,他們只是感情好猖任,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著瓷耙,像睡著了一般朱躺。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上哺徊,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天室琢,我揣著相機(jī)與錄音乾闰,去河邊找鬼落追。 笑死,一個胖子當(dāng)著我的面吹牛涯肩,可吹牛的內(nèi)容都是我干的轿钠。 我是一名探鬼主播巢钓,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼疗垛!你這毒婦竟也來了症汹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤贷腕,失蹤者是張志新(化名)和其女友劉穎背镇,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體泽裳,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡瞒斩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了涮总。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片胸囱。...
    茶點(diǎn)故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖瀑梗,靈堂內(nèi)的尸體忽然破棺而出烹笔,到底是詐尸還是另有隱情,我是刑警寧澤抛丽,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布谤职,位于F島的核電站,受9級特大地震影響亿鲜,放射性物質(zhì)發(fā)生泄漏柬帕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一狡门、第九天 我趴在偏房一處隱蔽的房頂上張望陷寝。 院中可真熱鬧,春花似錦其馏、人聲如沸凤跑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽仔引。三九已至,卻和暖如春褐奥,著一層夾襖步出監(jiān)牢的瞬間咖耘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工撬码, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留儿倒,地道東北人。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像夫否,于是被迫代替她去往敵國和親彻犁。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 通貨膨脹,一般定義為:在信用貨幣制度下微谓,流通中的貨幣數(shù)量超過經(jīng)濟(jì)實(shí)際需要而引起的貨幣貶值和物價(jià)水平全面而持續(xù)的上漲...
    依米的碎碎念閱讀 190評論 0 0
  • 圖文/馨雅百合 秋天 是一個五彩斑斕的季節(jié) 金色的陽光溫馨恬靜 湛藍(lán)的天空 彩云在絢麗著自身的潔白 秋天的光與色 ...
    馨晴百合閱讀 1,222評論 14 44
  • 輾轉(zhuǎn)反側(cè)不寐森篷,邪火攻心入肺。無奈披衣起豺型,蒲地藍(lán)中求慰疾宏。一支,兩支触创,解毒消火快睡坎藐。
    西嶺布衣閱讀 412評論 2 5
  • 放棄你岩馍,但我不放棄愛你,痛苦的不是過去抖韩,而是記憶...回首過往的點(diǎn)滴蛀恩,這段感情就像一個沙漏,哪怕自己已投入的再多茂浮,...
    海派壽司閱讀 419評論 0 0
  • 如果有時(shí)候你在中心双谆,你就能獲得發(fā)生在那些處在中心里的人所有的瞥見∠浚——奧修 如果你與你所謂的悲傷共處顽馋,沒有絲毫的思...
    健康幸福教練隨心閱讀 138評論 0 0