在金融、电信、政务等核心业务场景中,GBase 8c 分布式数据库承载着高并发、低延迟的事务处理与分析任务。随着业务规模扩大与数据量激增,SQL 语句的执行效率直接决定了系统的整体性能与稳定性。传统“黑盒式”的性能监控手段(如仅观察 CPU、内存等资源指标)已难以快速定位响应延迟的根因——低效 SQL 往往隐藏在复杂的业务逻辑中,成为系统瓶颈的隐形杀手。为此,GBase 8c提供了丰富的工具或系统对象,辅助用户快速完成SQL性能分析。

本文简单介绍dbe_perf.statement 与 dbe_perf.statement_history视图。这两个视图如同数据库内部的“X光机”,为运维与开发人员提供了 SQL 执行全生命周期的透视能力:

  • dbe_perf.statement:动态捕获当前活跃 SQL 的执行状态(如运行时长、资源消耗、锁等待)。
  • dbe_perf.statement_history:持久化记录历史 SQL 的执行统计(如调用次数、总耗时、I/O 与 CPU 开销)。

1、分析SQL执行时间

dbe_perf.statement 这个视图实际执行的是 get_instr_unique_sql() 内置函数,它能够显示CN节点上执行过的sql的一些时间信息,比如 sql执行的总耗时,在执行器中的耗时等信息,在优化器中执行的耗时等。它只能在CN节点上执行,不能在DN上执行。

内置函数get_instr_unique_sql()的数据来自于 g_instance.stat_cxt.UniqueSQLHashtbl 哈希表。哈希表内存有限,所以不能保存所有的sql信息,通过配置 instr_unique_sql_count GUC参数(默认值是 100)可以调整 dbe_perf.statement 能够记录的sql的数据。节点重启后,g_instance.stat_cxt.UniqueSQLHashtbl 哈希表内容会被清空。

示例

下面的示例展示了benchmarksql测试中 UPDATE bmsql_warehouse SET w_ytd = w_ytd + $1 WHERE w_id = $2 语句的统计信息。

test=# select * from dbe_perf.statement where query ~ 'UPDATE bmsql_warehouse';

例如返回如下信息:

以下信息表示在测试的这段时间,这条sql执行了 9339次。每次执行的时间是 db_time / n_calls = 313us 实际消耗CPU的时间 CPU_time / n_calls = 97us。这条sql走lightproxy,所以 plan_time rewrite_time execution_time 都是0。

-[ RECORD 1 ]--------+----------------------------------------------------------------------
node_name            | cn1
node_id              | -1178713634
user_name            | test1
user_id              | 16974
unique_sql_id        | 739002000
query                | UPDATE bmsql_warehouse     SET w_ytd = w_ytd + $1     WHERE w_id = $2
n_calls              | 9339
min_elapse_time      | 150
max_elapse_time      | 26529
total_elapse_time    | 2512361
n_returned_rows      | 0
n_tuples_fetched     | 0
n_tuples_returned    | 0
n_tuples_inserted    | 0
n_tuples_updated     | 0
n_tuples_deleted     | 0
n_blocks_fetched     | 1
n_blocks_hit         | 1
n_soft_parse         | 0
n_hard_parse         | 0
db_time              | 2929210
CPU_time             | 906947
execution_time       | 0
parse_time           | 24
plan_time            | 0
rewrite_time         | 9
pl_execution_time    | 0
pl_compilation_time  | 0
data_io_time         | 0
net_send_info        | {"time":215081, "n_calls":28017, "size":1285855}
net_recv_info        | {"time":1385396, "n_calls":26612, "size":884261}
net_stream_send_info | {"time":0, "n_calls":0, "size":0}
net_stream_recv_info | {"time":0, "n_calls":0, "size":0}
last_updated         | 2022-12-12 13:51:59.767953+08
sort_count           | 0
sort_time            | 0
sort_mem_used        | 0
sort_spill_count     | 0
sort_spill_size      | 0
hash_count           | 0
hash_time            | 0
hash_mem_used        | 0
hash_spill_count     | 0
hash_spill_size      | 0