.. _sys_stat_sql(KES):

sys_stat_sql
~~~~~~~~~~~~~~~~~~~~

按数据库时间模型展示 TOP SQL，展示信息包括：SQL基本信息、DB Time、TOP3 等待事件、解析计划执行时间、IO 大小和时间等。


**使用约束**

sys_stat_sql视图随底层统计视图的数据实时刷新，按执行总时间（dbtime）排序。同时必须GUC参数开启统计开关，才能正常使用该功能。

- 底层统计视图：sys_stat_sqltime, sys_stat_sqlwait，sys_stat_sqlio 和 sys_stat_statements。

- GUC参数： 在kingbase.conf中添加以下设置。

  .. code:: 

    track_sql = on 
    sys_stat_statements.track = 'top'



.. table:: sys_stat_sql 列
   :class: longtable
   :widths: 2 2 3

   +------------------------+---------+---------------------------------------------+
   | 名称                   | 类型    | 描述                                        |
   +========================+=========+=============================================+
   | datid                  | oid     | 数据库OID                                   |
   +------------------------+---------+---------------------------------------------+
   | datname                | text    | 数据库名称                                  |
   +------------------------+---------+---------------------------------------------+
   | userid                 | oid     | 用户OID                                     |
   +------------------------+---------+---------------------------------------------+
   | username               | text    | 用户名称                                    |
   +------------------------+---------+---------------------------------------------+
   | queryid                | bigint  | SQL语句标识符                               |
   +------------------------+---------+---------------------------------------------+
   | query                  | text    | SQL文本                                     |
   +------------------------+---------+---------------------------------------------+
   | calls                  | bigint  | 执行次数（Execute或SimpleQuery）            |
   +------------------------+---------+---------------------------------------------+
   | rows                   | bigint  | 返回元组数                                  |
   +------------------------+---------+---------------------------------------------+
   | db_time                | bigint  | DB Time时间，微秒                           |
   +------------------------+---------+---------------------------------------------+
   | db_cpu                 | bigint  | DB CPU时间，微秒                            |
   +------------------------+---------+---------------------------------------------+
   | db_wait                | bigint  | FG Wait时间，微秒                           |
   +------------------------+---------+---------------------------------------------+
   | total_db_time_pct      | numeric | 当前SQL占总DB Time百分比                    |
   +------------------------+---------+---------------------------------------------+
   | cpu_time_pct           | numeric | DB CPU占本条SQLDB Time百分比                |
   +------------------------+---------+---------------------------------------------+
   | wait_time_pct          | numeric | 等待占本条SQLDB Time百分比                  |
   +------------------------+---------+---------------------------------------------+
   | wait_event_1           | text    | 等待时间最长的等待事件                      |
   +------------------------+---------+---------------------------------------------+
   | wait_calls_1           | bigint  | 最长等待事件等待次数                        |
   +------------------------+---------+---------------------------------------------+
   | wait_time_1            | bigint  | 最长等待事件等待时间，微秒                  |
   +------------------------+---------+---------------------------------------------+
   | wait_time_pct_1        | numeric | 最长等待事件等待时间占本条SQL DB Time百分比 |
   +------------------------+---------+---------------------------------------------+
   | wait_event_2           | text    | 等待时间次长的等待事件                      |
   +------------------------+---------+---------------------------------------------+
   | wait_calls_2           | bigint  | 次长等待事件等待次数                        |
   +------------------------+---------+---------------------------------------------+
   | wait_time_2            | bigint  | 次长等待事件等待时间，微秒                  |
   +------------------------+---------+---------------------------------------------+
   | wait_time_pct_2        | numeric | 次长等待事件等待时间占本条SQL DB Time百分比 |
   +------------------------+---------+---------------------------------------------+
   | parse_calls            | bigint  | 解析次数                                    |
   +------------------------+---------+---------------------------------------------+
   | parse_time             | bigint  | 解析时间，微秒                              |
   +------------------------+---------+---------------------------------------------+
   | parse_time_pct         | numeric | 解析时间占本条SQL DB Time百分比             |
   +------------------------+---------+---------------------------------------------+
   | plan_calls             | bigint  | 计划次数                                    |
   +------------------------+---------+---------------------------------------------+
   | plan_time              | bigint  | 计划时间，微秒                              |
   +------------------------+---------+---------------------------------------------+
   | plan_time_pct          | numeric | 计划时间占本条SQL DB Time百分比             |
   +------------------------+---------+---------------------------------------------+
   | exec_calls             | bigint  | 执行次数                                    |
   +------------------------+---------+---------------------------------------------+
   | exec_time              | bigint  | 执行时间，微秒                              |
   +------------------------+---------+---------------------------------------------+
   | exec_time_pct          | numeric | 执行时间占本条SQL DB Time百分比             |
   +------------------------+---------+---------------------------------------------+
   | wal_size               | bigint  | 写WAL日志大小，字节                         |
   +------------------------+---------+---------------------------------------------+
   | shared_blks_read_size  | bigint  | 共享块读大小，字节                          |
   +------------------------+---------+---------------------------------------------+
   | shared_blks_write_size | bigint  | 共享块写大小，字节                          |
   +------------------------+---------+---------------------------------------------+
   | local_blks_read_size   | bigint  | 本地块读大小，字节                          |
   +------------------------+---------+---------------------------------------------+
   | local_blks_write_size  | bigint  | 本地块写大小，字节                          |
   +------------------------+---------+---------------------------------------------+
   | temp_blks_read_size    | bigint  | 临时块读大小，字节                          |
   +------------------------+---------+---------------------------------------------+
   | temp_blks_write_size   | bigint  | 临时块写大小，字节                          |
   +------------------------+---------+---------------------------------------------+
   | shared_blks_hit        | numeric | 共享块缓存命中率                            |
   +------------------------+---------+---------------------------------------------+
   | local_blks_hit         | numeric | 本地块缓存命中率                            |
   +------------------------+---------+---------------------------------------------+
   | blks_read_time         | bigint  | 读共享、本地和临时块时间，微秒              |
   +------------------------+---------+---------------------------------------------+
   | blks_write_time        | bigint  | 写共享、本地和临时块时间，微秒              |
   +------------------------+---------+---------------------------------------------+
