向当前用户显示其自己的查询日志记录。该表读取由 query_log.database 和 query_log.table 服务器设置指定的查询日志表 (默认是 system.query_log) ,仅返回发起用户等于 currentUser() 的行 (若设置了 initial_user,则以其作为发起用户;否则使用 user) 。
与查询日志表本身不同,读取 system.user_query_log 无需任何授权,因此用户无需获得访问其他用户查询的权限,即可查看自己的查询。
仅当查询日志存储在本地时才支持此功能。如果将 query_log.engine 配置为 Distributed,或配置为任何其他将读取操作委托给另一台服务器的 engine,system.user_query_log 将拒绝读取并抛出异常,因为无法跨越 ClickHouse 协议的服务器边界强制执行所需的访问检查。在这种情况下,请通过设置 query_log.enable_user_query_log = 0 禁用该表。
可通过 query_log.enable_user_query_log 服务器设置禁用该表。如果未配置查询日志,或其表尚未创建,system.user_query_log 将为空。
查询日志的分区列和键列上与常量比较的条件 (如 event_date、event_time、query_start_time、query_id、type 及其他类似的标量列) 会下推到底层查询日志表。因此,如下例所示的常规查找可保留分区裁剪,并避免扫描整个保留的日志。
如果在升级到提供此表的 ClickHouse 版本之前已创建名为 system.user_query_log 的表,服务器将无法启动,除非重命名或删除现有表,或者将 query_log.enable_user_query_log 设置为 0。
hostname (String) — 执行查询的服务器主机名。
type (Enum8(‘QueryStart’ = 1, ‘QueryFinish’ = 2, ‘ExceptionBeforeStart’ = 3, ‘ExceptionWhileProcessing’ = 4)) — 查询执行期间发生的事件类型。取值:QueryStart — 查询执行成功启动,QueryFinish — 查询执行成功结束,ExceptionBeforeStart — 查询执行开始前发生异常,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) — 查询所涉及的所有表和表函数读取的总行数。包括常规子查询,以及用于 IN 和 JOIN 的子查询。对于分布式查询,read_rows 包含从所有副本读取的总行数。每个副本都会发送其 read_rows 值,查询发起服务器会汇总所有接收到的值及本地值。缓存卷不会影响此值。
read_bytes (UInt64) — 查询所涉及的所有表和表函数读取的总字节数。包括常规子查询,以及用于 IN 和 JOIN 的子查询。对于分布式查询,read_bytes 包含从所有副本读取的总行数。每个副本都会发送其 read_bytes 值,查询发起服务器会汇总所有接收到的值及本地值。缓存卷不会影响此值。
written_rows (UInt64) — 查询写入的行数,包括由管道触发的下游 insert 所写入的行,例如已附加的 materialized view。对于同步 insert,这些下游行记录在 query_kind = Insert 条目中;对于异步 insert,则记录在 query_kind = AsyncInsertFlush 条目中,而面向客户端的 Insert 条目仅记录从客户端接收的行。对于不写入行的查询,该值为 0。
written_bytes (UInt64) — 查询写入的字节数 (未压缩) ,包括由管道触发的下游 insert 所写入的字节,例如已附加的 materialized view。对于同步 insert,这些下游字节记录在 query_kind = Insert 条目中;对于异步 insert,则记录在 query_kind = AsyncInsertFlush 条目中,而面向客户端的 Insert 条目仅记录从客户端接收的字节。对于不写入数据的查询,该值为 0。
result_rows (UInt64) — SELECT 查询结果中的行数,或 insert 写入的行数。对于同步 insert,这包括记录在 query_kind = Insert 条目中、由管道触发的下游 insert (例如已附加的 materialized view) 写入的行;对于异步 insert,这些下游行记录在 query_kind = AsyncInsertFlush 条目中,而面向客户端的 Insert 条目仅记录从客户端接收的行。
result_bytes (UInt64) — 存储查询结果所使用的 RAM 容量,单位为字节。
memory_usage (UInt64) — 查询的内存消耗。
current_database (String) — 当前数据库名称。
query (String) — 查询字符串。
formatted_query (String) — 格式化后的查询字符串。
normalized_query_hash (UInt64) — 数值哈希值;例如,仅字面量值不同的查询具有相同的哈希值。
query_kind (String) — 查询类型。
databases (Array(String)) — 查询中涉及的数据库名称。
tables (Array(String)) — 查询中涉及的表名称。
columns (Array(String)) — 查询中涉及的列名称。
partitions (Array(String)) — 查询中涉及的分区名称。
projections (Array(String)) — 查询执行期间使用的投影名称。
views (Array(String)) — 查询中涉及的 (物化或实时) 视图名称。
exception_code (Int32) — 异常代码。
exception (String) — 异常消息。
stack_trace (String) — 堆栈跟踪。查询成功完成时为空字符串。
is_initial_query (UInt8) — 查询类型。可能的值:1 — 由客户端发起的查询;0 — 作为分布式查询执行的一部分,由另一查询发起的查询。
connection_address (IPv6) — 发起连接的客户端 IP 地址。通过代理连接时,该地址为代理地址。
connection_port (UInt16) — 发起连接的客户端端口。通过代理连接时,该端口为代理端口。
user (String) — 发起当前查询的用户名。
query_id (String) — 查询 ID。
address (IPv6) — 用于发起查询的 IP 地址。通过代理连接且设置了 auth_use_forwarded_address 时,该地址为客户端地址而非代理地址。
port (UInt16) — 用于发起查询的客户端端口。通过代理连接且设置了 auth_use_forwarded_address 时,该端口为客户端端口而非代理端口。
initial_user (String) — 执行初始查询的用户名 (用于分布式查询执行) 。
initial_query_id (String) — 初始查询的 ID (用于分布式查询执行) 。
initial_address (IPv6) — 发起父查询的 IP 地址。
initial_port (UInt16) — 用于发起父查询的客户端端口。
initial_query_start_time (DateTime) — 初始查询的开始时间 (用于分布式查询执行) 。
initial_query_start_time_microseconds (DateTime64(6)) — 具有微秒精度的初始查询开始时间 (用于分布式查询执行) 。
authenticated_user (String) — 会话中通过身份验证的用户名。
interface (UInt8) — 发起查询所使用的接口。可能的值:1 — TCP;2 — HTTP。
is_secure (UInt8) — 指示查询是否通过安全接口执行的标志
os_user (String) — 运行 clickhouse-client 的操作系统用户名。
client_hostname (String) — 运行 clickhouse-client 或其他 TCP 客户端的客户端计算机主机名。
client_name (String) — clickhouse-client 或其他 TCP 客户端的名称。
client_agent (String) — 调用客户端的 AI 编程 agent (例如 claude-code、cursor) ,通过环境变量检测。未检测到 agent 时为空。
client_revision (UInt32) — clickhouse-client 或其他 TCP 客户端的修订版本。
client_version_major (UInt32) — clickhouse-client 或其他 TCP 客户端的主版本。
client_version_minor (UInt32) — clickhouse-client 或其他 TCP 客户端的次版本。
client_version_patch (UInt32) — clickhouse-client 或其他 TCP 客户端版本的补丁版本号。
script_query_number (UInt32) — clickhouse-client 多查询脚本中的查询编号。
script_line_number (UInt32) — clickhouse-client 多查询脚本中查询起始位置的行号。
http_method (UInt8) — 发起查询的 HTTP 方法。可能的值:0 — 查询通过 TCP 接口发起,1 — 使用 GET 方法,2 — 使用 POST 方法,4 — 使用 PUT 方法,5 — 使用 DELETE 方法,6 — 使用 HEAD 方法。
http_user_agent (String) — HTTP 查询中传递的 UserAgent HTTP 请求头。
http_referer (String) — HTTP 查询中传递的 Referer HTTP 请求头 (包含发起查询的页面的完整或部分地址) 。
forwarded_for (String) — HTTP 查询中传递的 X-Forwarded-For HTTP 请求头。
quota_key (String) — quotas 设置中指定的配额键 (请参阅 keyed) 。
distributed_depth (UInt64) — 查询在服务器之间被转发的次数。
revision (UInt32) — ClickHouse 修订版本。
http_handler_name (String) — 调用该查询的 SQL 定义 HTTP handler (CREATE HANDLER) 的名称。未通过此类 handler 调用查询时为空。
http_request_url (String) — 调用该查询的 HTTP 请求路径 (不含查询字符串) 。为避免持久化敏感请求参数,省略查询字符串。对于非 HTTP 查询为空。
log_comment (String) — 日志注释。可设置为长度不超过 max_query_size 的任意字符串。未定义时为空字符串。
thread_ids (Array(UInt64)) — 参与查询执行的线程 ID。这些线程不一定同时运行。
peak_threads_usage (UInt64) — 同时执行查询的最大线程数。
ProfileEvents (Map(String, UInt64)) — 用于衡量不同指标的 ProfileEvents。相关说明可在 system.events 表中找到。
Settings (Map(String, String)) — 客户端执行查询时更改的设置。要启用设置变更日志,请将 log_query_settings 参数设为 1。
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)) — 查询执行期间使用的表函数的标准名称。
used_executable_user_defined_functions (Array(String)) — 查询执行期间使用的可执行用户自定义函数的标准名称。
used_sql_user_defined_functions (Array(String)) — 查询执行期间使用的 SQL 用户自定义函数的标准名称。
used_row_policies (Array(String)) — 查询执行期间使用的行策略名称列表。
used_privileges (Array(String)) — 查询执行期间成功检查的特权。
missing_privileges (Array(String)) — 查询执行期间缺少的特权。
transaction_id (Tuple(UInt64, UInt64, UUID)) — 执行此查询的事务标识符。
query_cache_usage (Enum8(‘Unknown’ = 0, ‘None’ = 1, ‘Write’ = 2, ‘Read’ = 3)) — 查询执行期间对查询缓存的使用情况。值:‘Unknown’ = 状态未知,‘None’ = 查询结果既未写入查询结果缓存,也未从中读取,‘Write’ = 查询结果已写入查询结果缓存,‘Read’ = 查询结果已从查询结果缓存读取。
asynchronous_read_counters (Map(String, UInt64)) — 异步读取指标。
is_internal (UInt8) — 指示该查询是否为内部执行的辅助查询。
别名:
ProfileEvents.Names — mapKeys(ProfileEvents) 的别名。
ProfileEvents.Values — mapValues(ProfileEvents) 的别名。
Settings.Names — mapKeys(Settings) 的别名。
Settings.Values — mapValues(Settings) 的别名。