Percona提供了一個參數query_response_time_stats用於在伺服器端觀察資料庫的回應時間root@test 02:29:35>show variables like'query_response_time_stats';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| query_response_time_stats | OFF |
+---------------------------+-------+
1 row in set (0.00 sec)
將這個全域變數設定為‘ON’即可觀察回應時間
root@test 02:29:47>set globalquery_response_time_stats = 'ON';
Query OK, 0 rows affected (0.00 sec)
回應時間資訊被記錄在一個內建的I_S 外掛程式中:QUERY_RESPONSE_TIME,其結構如下,包含3個欄位:
root@information_schema 02:56:37>desc QUERY_RESPONSE_TIME;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| time | varchar(14) | NO | | | |
| count | int(11) unsigned | NO | | 0 | |
| total | varchar(14) | NO | | | |
+-------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
root@information_schema 04:28:01>select * from QUERY_RESPONSE_TIME;
+----------------+-------+----------------+
| time |count | total |
+----------------+-------+----------------+
| 0.000001| 0 | 0.000000 |
| 0.000010| 0 | 0.000000 |
| 0.000100| 16 | 0.000697 |
| 0.001000| 11 | 0.002144 |
| 0.010000| 0 | 0.000000 |
| 0.100000| 0 | 0.000000 |
| 1.000000| 0 | 0.000000 |
| 10.000000| 0 | 0.000000 |
| 100.000000| 0 | 0.000000 |
| 1000.000000| 0 | 0.000000 |
| 10000.000000| 0 | 0.000000 |
| 100000.000000| 0 | 0.000000 |
| 1000000.000000 | 0 | 0.000000 |
| TOO LONG | 0 | TOO LONG |
+----------------+-------+----------------+
其中time代表RT區間(可以通過參數query_response_time_range_base)來設定,count表示該區間裡收集的SQL數,total表示這些SQL的執行總時間
在代碼sql/query_response_time.cc裡實現了該i_s表。在系統啟動時(init_server_components函數)進行初始化(空函數),在sql_show.cc裡聲明內建i_s表
欄位定義:
ST_FIELD_INFO query_response_time_fields_info[]
在i_s表描述數組(ST_SCHEMA_TABLEschema_tables[])中的定義如下:
#ifdef HAVE_RESPONSE_TIME_DISTRIBUTION
{"QUERY_RESPONSE_TIME",query_response_time_fields_info, create_schema_table,
query_response_time_fill, make_old_format,0, -1, -1, 0, 0},
#else
{"QUERY_RESPONSE_TIME",query_response_time_fields_info, create_schema_table,
0, make_old_format, 0, -1, -1, 0, 0},
#endif //HAVE_RESPONSE_TIME_DISTRIBUTION