跳到主要内容
跳到主要内容

query_log

在 ClickHouse Cloud 中查询

此系统表中的数据本地保存在 ClickHouse Cloud 的每个节点上。因此,要获得所有数据的完整视图,需要使用 clusterAllReplicas 函数。有关更多详细信息,请参阅此处

包含有关已执行查询的信息,例如,开始时间、处理时长、错误消息。

注意

此表不包含 INSERT 查询的导入数据。

您可以在服务器配置的 query_log 部分中更改查询日志记录的设置。

您可以通过设置 log_queries = 0 来禁用查询日志记录。我们不建议关闭日志记录,因为此表中的信息对于解决问题非常重要。

数据的刷新周期在 query_log 服务器设置部分的 flush_interval_milliseconds 参数中设置。要强制刷新,请使用 SYSTEM FLUSH LOGS 查询。

ClickHouse 不会自动从此表中删除数据。有关更多详细信息,请参阅简介

system.query_log 表注册两种类型的查询

  1. 由客户端直接运行的初始查询。
  2. 由其他查询启动的子查询(用于分布式查询执行)。对于这些类型的查询,有关父查询的信息显示在 initial_* 列中。

每个查询在 query_log 表中创建一个或两个行,具体取决于查询的状态(请参阅 type 列)

  1. 如果查询执行成功,则创建具有 QueryStartQueryFinish 类型的两行。
  2. 如果在查询处理期间发生错误,则创建具有 QueryStartExceptionWhileProcessing 类型的两个事件。
  3. 如果在启动查询之前发生错误,则创建一个具有 ExceptionBeforeStart 类型的单个事件。

您可以使用 log_queries_probability 设置来减少在 query_log 表中注册的查询数量。

您可以使用 log_formatted_queries 设置将格式化查询记录到 formatted_query 列。

  • hostname (LowCardinality(String)) — 执行查询的服务器的主机名。
  • type (Enum8) — 执行查询时发生的事件类型。值:
    • 'QueryStart' = 1 — 查询执行成功启动。
    • 'QueryFinish' = 2 — 查询执行成功结束。
    • 'ExceptionBeforeStart' = 3 — 查询执行开始之前的异常。
    • 'ExceptionWhileProcessing' = 4 — 查询执行期间的异常。
  • event_date (Date) — 查询开始日期。
  • event_time (DateTime) — 查询开始时间。
  • event_time_microseconds (DateTime64) — 查询开始时间,精确到微秒。
  • query_start_time (DateTime) — 查询执行的开始时间。
  • query_start_time_microseconds (DateTime64) — 查询执行的开始时间,精确到微秒。
  • query_duration_ms (UInt64) — 查询执行时长,以毫秒为单位。
  • read_rows (UInt64) — 从参与查询的所有表和表函数中读取的总行数。包括常见的子查询、INJOIN 的子查询。对于分布式查询,read_rows 包括在所有副本上读取的总行数。每个副本都发送其 read_rows 值,并且查询的发起服务器会汇总所有接收到的值和本地值。缓存卷不影响此值。
  • read_bytes (UInt64) — 从参与查询的所有表和表函数中读取的总字节数。包括常见的子查询、INJOIN 的子查询。对于分布式查询,read_bytes 包括在所有副本上读取的总字节数。每个副本都发送其 read_bytes 值,并且查询的发起服务器会汇总所有接收到的值和本地值。缓存卷不影响此值。
  • written_rows (UInt64) — 对于 INSERT 查询,为写入的行数。对于其他查询,此列值为 0。
  • written_bytes (UInt64) — 对于 INSERT 查询,为写入的字节数(未压缩)。对于其他查询,此列值为 0。
  • result_rows (UInt64) — SELECT 查询结果中的行数,或 INSERT 查询中的行数。
  • result_bytes (UInt64) — 用于存储查询结果的 RAM 容量,以字节为单位。
  • memory_usage (UInt64) — 查询的内存消耗。
  • current_database (String) — 当前数据库的名称。
  • query (String) — 查询字符串。
  • formatted_query (String) — 格式化查询字符串。
  • normalized_query_hash (UInt64) — 相似查询的规范化哈希值,不包含字面量的值。
  • query_kind (LowCardinality(String)) — 查询类型。
  • databases (Array(LowCardinality(String))) — 查询中存在的数据库名称。
  • tables (Array(LowCardinality(String))) — 查询中存在的表名称。
  • columns (Array(LowCardinality(String))) — 查询中存在的列名称。
  • partitions (Array(LowCardinality(String))) — 查询中存在的分区名称。
  • projections (String) — 查询执行期间使用的投影名称。
  • views (Array(LowCardinality(String))) — 查询中存在的(物化或实时)视图的名称。
  • exception_code (Int32) — 异常代码。
  • exception (String) — 异常消息。
  • stack_trace (String) — 堆栈追踪。如果查询成功完成,则为空字符串。
  • is_initial_query (UInt8) — 查询类型。可能的值:
    • 1 — 查询由客户端发起。
    • 0 — 查询由另一个查询作为分布式查询执行的一部分发起。
  • user (String) — 发起当前查询的用户的名称。
  • query_id (String) — 查询的 ID。
  • address (IPv6) — 用于发起查询的 IP 地址。
  • port (UInt16) — 用于发起查询的客户端端口。
  • 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) — 初始查询开始时间,精确到微秒(用于分布式查询执行)。
  • interface (UInt8) — 发起查询的接口。可能的值:
    • 1 — TCP。
    • 2 — HTTP。
  • os_user (String) — 运行 clickhouse-client 的操作系统用户名。
  • client_hostname (String) — 运行 clickhouse-client 或其他 TCP 客户端的客户端机器的主机名。
  • client_name (String) — clickhouse-client 或其他 TCP 客户端的名称。
  • 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 方法。
  • http_user_agent (String) — HTTP 查询中传递的 HTTP 标头 UserAgent
  • http_referer (String) — HTTP 查询中传递的 HTTP 标头 Referer(包含发起查询的页面的绝对或部分地址)。
  • forwarded_for (String) — HTTP 查询中传递的 HTTP 标头 X-Forwarded-For
  • quota_key (String) — 在 配额设置中指定的 quota key(请参阅 keyed)。
  • revision (UInt32) — ClickHouse 版本修订号。
  • ProfileEvents (Map(String, UInt64)) — 衡量不同指标的 ProfileEvents。它们的描述可以在表 system.events 中找到
  • Settings (Map(String, String)) — 客户端运行查询时更改的设置。要启用设置更改的日志记录,请将 log_query_settings 参数设置为 1。
  • log_comment (String) — 日志注释。可以设置为不超过 max_query_size 的任意字符串。如果未定义,则为空字符串。
  • thread_ids (Array(UInt64)) — 参与查询执行的线程 ID。这些线程可能并非同时运行。
  • peak_threads_usage (UInt64)) — 执行查询的同时线程的最大计数。
  • used_aggregate_functions (Array(String)) — 查询执行期间使用的 aggregate functions 的规范名称。
  • used_aggregate_function_combinators (Array(String)) — 查询执行期间使用的 aggregate functions combinators 的规范名称。
  • used_database_engines (Array(String)) — 查询执行期间使用的 database engines 的规范名称。
  • used_data_type_families (Array(String)) — 查询执行期间使用的 data type families 的规范名称。
  • used_dictionaries (Array(String)) — 查询执行期间使用的 dictionaries 的规范名称。对于使用 XML 文件配置的字典,这是字典的名称,对于通过 SQL 语句创建的字典,规范名称是完全限定的对象名称。
  • used_formats (Array(String)) — 查询执行期间使用的 formats 的规范名称。
  • used_functions (Array(String)) — 查询执行期间使用的 functions 的规范名称。
  • used_storages (Array(String)) — 查询执行期间使用的 storages 的规范名称。
  • used_table_functions (Array(String)) — 查询执行期间使用的 table functions 的规范名称。
  • used_privileges (Array(String)) - 查询执行期间成功检查的权限。
  • missing_privileges (Array(String)) - 查询执行期间缺少的权限。
  • query_cache_usage (Enum8) — 查询执行期间 query cache 的使用情况。值:
    • 'Unknown' = 状态未知。
    • 'None' = 查询结果既未写入也未从查询缓存中读取。
    • 'Write' = 查询结果已写入查询缓存。
    • 'Read' = 查询结果已从查询缓存中读取。

示例

SELECT * FROM system.query_log WHERE type = 'QueryFinish' ORDER BY query_start_time DESC LIMIT 1 FORMAT Vertical;
Row 1:
──────
hostname: clickhouse.eu-central1.internal
type: QueryFinish
event_date: 2021-11-03
event_time: 2021-11-03 16:13:54
event_time_microseconds: 2021-11-03 16:13:54.953024
query_start_time: 2021-11-03 16:13:54
query_start_time_microseconds: 2021-11-03 16:13:54.952325
query_duration_ms: 0
read_rows: 69
read_bytes: 6187
written_rows: 0
written_bytes: 0
result_rows: 69
result_bytes: 48256
memory_usage: 0
current_database: default
query: DESCRIBE TABLE system.query_log
formatted_query:
normalized_query_hash: 8274064835331539124
query_kind:
databases: []
tables: []
columns: []
projections: []
views: []
exception_code: 0
exception:
stack_trace:
is_initial_query: 1
user: default
query_id: 7c28bbbb-753b-4eba-98b1-efcbe2b9bdf6
address: ::ffff:127.0.0.1
port: 40452
initial_user: default
initial_query_id: 7c28bbbb-753b-4eba-98b1-efcbe2b9bdf6
initial_address: ::ffff:127.0.0.1
initial_port: 40452
initial_query_start_time: 2021-11-03 16:13:54
initial_query_start_time_microseconds: 2021-11-03 16:13:54.952325
interface: 1
os_user: sevirov
client_hostname: clickhouse.eu-central1.internal
client_name: ClickHouse
client_revision: 54449
client_version_major: 21
client_version_minor: 10
client_version_patch: 1
http_method: 0
http_user_agent:
http_referer:
forwarded_for:
quota_key:
revision: 54456
log_comment:
thread_ids: [30776,31174]
ProfileEvents: {'Query':1,'NetworkSendElapsedMicroseconds':59,'NetworkSendBytes':2643,'SelectedRows':69,'SelectedBytes':6187,'ContextLock':9,'RWLockAcquiredReadLocks':1,'RealTimeMicroseconds':817,'UserTimeMicroseconds':427,'SystemTimeMicroseconds':212,'OSCPUVirtualTimeMicroseconds':639,'OSReadChars':894,'OSWriteChars':319}
Settings: {'load_balancing':'random','max_memory_usage':'10000000000'}
used_aggregate_functions: []
used_aggregate_function_combinators: []
used_database_engines: []
used_data_type_families: []
used_dictionaries: []
used_formats: []
used_functions: []
used_storages: []
used_table_functions: []
used_privileges: []
missing_privileges: []
query_cache_usage: None

另请参阅