You need to enable JavaScript to run this app.
导航
SQL 审计
最近更新时间:2025.03.11 15:43:37首次发布时间:2024.08.29 12:45:15
我的收藏
有用
有用
无用
无用

ByteHouse 云数仓版为您提供SQL审计能力,为您提供界面化查看SQL执行日志的功能,同时为您提供详细SQL日志表,打开详细日志开关后即可查看对应详细SQL日志表数据进一步查看更细致的日志数据。本文为您介绍ByteHouse的SQL审计能力。

SQL历史

通过 “ByteHouse 控制台-审计日志-SQL 审计” 页面,您可以查询到执行 SQL 的历史日志。

  1. 访问SQL 历史页签,可以看到历史查询列表,并可以通过筛选条件筛选历史记录。
    Image
    1. SQL 历史列表中包含如下信息:
      • 查询 ID :SQL 查询的唯一 ID 号。
      • 状态:事件完成状态,如“成功”或“失败”。失败的事件,可以在后侧按钮查询具体情况。
      • 有效角色:执行此事件的角色类型。
      • 计算组:执行查询的计算组VW名称。
      • 开始时间:事件发生的具体时间。
      • 结束时间:事件结束的具体时间。
      • 时长:事件执行的时间长度。
      • SQL 类型:执行 SQL 语句的类型,如 CREATE、DROP、INSERT、DELETE 等等。
      • SQL 语句:执行的 SQL 语句内容,以及其方言类型。
    2. 切换到“错误查询”页签可以快速定位失败事件,并根据需求查询相关状态。
      Image
  2. 切换到“运行中的查询”页签,可以看到正在执行的查询列表,相关信息与上文相同。

详细日志(query_log)

注意

  • 详细日志 非公开功能,需要加白使用

当我们为您的账号加白后,您可以在SQL审计页面的右上角打开开启详细日志开关,打开后,ByteHouse会为您在系统中创建一个query_log的表,用于写入详细的SQL执行日志数据。

  1. 打开开启详细日志开关。
    Image
  2. 打开详细日志按钮后,我们将获得您的授权,在您的【租户-环境】下创建一个bh_system的数据库,和一张query_log表,您对这个库和表拥有全部的权限;

警告

由于ByteHouse将您执行的SQL日志信息持续写入到bh_system.query_log表中;对于该表的增删改操作将会影响日志写入,请与ByteHouse同学确认后再执行此类操作;

  1. 查看query_log表。以使用SQL工作表为例为,您可以在SQL工作表的左侧数据对象中看到query_log表。
    Image

query_log表

列名

数据类型

字段描述

type

Enum8,枚举值包括:

  • 'QueryStart' = 1
  • 'QueryFinish' = 2
  • 'ExceptionBeforeStart' = 3

查询事件类型:

  1. QueryStart (查询开始)
  2. QueryFinish (查询成功结束)
  3. ExceptionBeforeStart (执行前异常)
  4. ExceptionWhileProcessing (执行中异常)

event_date

Date

事件发生的日期

event_time

DateTime

事件发生的精确时间戳

event_time_microseconds

DateTime64(6)

事件时间戳(微秒精度)

query_start_time

DateTime

查询开始时间

query_start_time_microseconds

DateTime64(6)

查询开始时间(微秒精度)

query_duration_ms

UInt64

查询总耗时(毫秒)

read_rows

UInt64

查询的所有表和表函数读取的总行数

read_bytes

UInt64

查询的所有表和表函数读取的总字节数

written_rows

UInt64

写入的行数(仅INSERT操作)

written_bytes

UInt64

写入的字节数(仅INSERT操作)

result_rows

UInt64

返回结果的行数

result_bytes

UInt64

返回结果的字节数

memory_usage

UInt64

查询在每个worker,每个segment上使用的最高内存用量

current_database

String

当前数据库

query

String

完整的查询语句

normalized_query_hash

UInt64

规范化查询哈希值

query_kind

String

查询类型(如 SELECT, INSERT 等)

databases

Array(String)

查询涉及到的数据库

tables

Array(String)

查询涉及到的数据表

columns

Array(String)

查询涉及到的数据表字段

partition_ids

Array(String)

分区

projections

Array(String)

查询涉及的投影(Projection)名称列表

materialized_views

Array(String)

查询涉及的物化视图名称列表

exception_code

Int32

异常代码(仅当type为异常事件时有值)

exception

String

异常信息(错误消息)

stack_trace

String

异常堆栈跟踪信息(需配置 log_query_threads=1

is_initial_query

UInt8

是否为初始查询(0:子查询;1:用户直接发起的查询)

user

String

用户名

query_id

String

查询ID

interface

UInt8

发起查询的接口. 可能的值:

  • 1 — TCP.
  • 2 — HTTP.

os_user

String

运行客户端的操作系统用户

client_hostname

String

客户端主机名

client_name

String

客户端名称

client_revision

UInt32

客户端版本

client_version_major

UInt32

客户端主版本

client_version_minor

UInt32

客户端次版本号

client_version_patch

UInt32

客户端补丁版本

http_method

UInt8

发起查询的HTTP方法. 可能值:
0 — TCP接口的查询.
1 — GET
2 — POST

http_user_agent

String

客户端的User-Agent信息

http_referer

String

HTTP 来源信息

forwarded_for

String

HTTP代理链中的客户端IP

quota_key

String

配额键

revision

UInt32

ProfileEvents

Map(String, UInt64)

PrunedPartitions:

  • 实际读取到的partition数量

TotalPartitions:

  • 总共partition数量

TotalCPUTimeMs:

  • 查询在每个worker,每个segment上总和的cpu耗时(毫秒)

Settings

Map(String, String)

查询生效的配置参数(覆盖默认值的部分)

log_comment

String

日志注释

thread_ids

Array(UInt64)

参与查询的线程ID列表

used_aggregate_functions

Array(String)

使用的聚合函数

used_aggregate_function_combinators

Array(String)

使用的聚合函数

used_database_engines

Array(String)

使用的表引擎

used_data_type_families

Array(String)

使用的数据类型

used_dictionaries

Array(String)

使用的字典表

used_formats

Array(String)

使用数据格式参数(如:FORMAT CSV)

used_functions

Array(String)

使用的函数

used_storages

Array(String)

使用的存储

used_table_functions

Array(String)

使用的表函数

virtual_warehouse

String

查询使用的计算组

query_plan

String

查询的执行计划

字段名

数据类型

type

Enum8

event_date

Date

event_time

DateTime

event_time_microseconds

DateTime64(6)

query_start_time

DateTime

query_start_time_microseconds

DateTime64(6)

query_duration_ms

UInt64

read_rows

UInt64

read_bytes

UInt64

written_rows

UInt64

written_bytes

UInt64

result_rows

UInt64

result_bytes

UInt64

memory_usage

UInt64

current_database

String

query

String

normalized_query_hash

UInt64

query_kind

String

databases

Array(String)

tables

Array(String)

columns

Array(String)

partition_ids

Array(String)

projections

Array(String)

materialized_views

Array(String)

exception_code

Int32

exception

String

stack_trace

String

is_initial_query

UInt8

user

String

query_id

String

interface

UInt8

os_user

String

client_hostname

String

client_name

String

client_revision

UInt32

client_version_major

UInt32

client_version_minor

UInt32

client_version_patch

UInt32

http_method

UInt8

http_user_agent

String

http_referer

String

forwarded_for

String

quota_key

String

revision

UInt32

ProfileEvents

Map(String, UInt64)

Settings

Map(String, String)

log_comment

String

thread_ids

Array(UInt64)

used_aggregate_functions

Array(String)

used_aggregate_function_combinators

Array(String)

used_database_engines

Array(String)

used_data_type_families

Array(String)

used_dictionaries

Array(String)

used_formats

Array(String)

used_functions

Array(String)

used_storages

Array(String)

used_table_functions

Array(String)

virtual_warehouse

String

query_plan

String

字段名

数据类型

type

Enum8

event_date

Date

event_time

DateTime

event_time_microseconds

DateTime64(6)

query_start_time

DateTime

query_start_time_microseconds

DateTime64(6)

query_duration_ms

UInt64

read_rows

UInt64

read_bytes

UInt64

written_rows

UInt64

written_bytes

UInt64

result_rows

UInt64

result_bytes

UInt64

memory_usage

UInt64

current_database

String

query

String

normalized_query_hash

UInt64

query_kind

String

databases

Array(String)

tables

Array(String)

columns

Array(String)

partition_ids

Array(String)

projections

Array(String)

materialized_views

Array(String)

exception_code

Int32

exception

String

stack_trace

String

is_initial_query

UInt8

user

String

query_id

String

interface

UInt8

os_user

String

client_hostname

String

client_name

String

client_revision

UInt32

client_version_major

UInt32

client_version_minor

UInt32

client_version_patch

UInt32

http_method

UInt8

http_user_agent

String

http_referer

String

forwarded_for

String

quota_key

String

revision

UInt32

ProfileEvents

Map(String, UInt64)

Settings

Map(String, String)

log_comment

String

thread_ids

Array(UInt64)

used_aggregate_functions

Array(String)

used_aggregate_function_combinators

Array(String)

used_database_engines

Array(String)

used_data_type_families

Array(String)

used_dictionaries

Array(String)

used_formats

Array(String)

used_functions

Array(String)

used_storages

Array(String)

used_table_functions

Array(String)

virtual_warehouse

String

query_plan

String