> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 包含执行查询的线程相关信息的系统表， 例如线程名称、线程开始时间以及查询处理耗时。

# system.query_thread_log

<Info>
  **在 ClickHouse Cloud 中查询**

  此系统表中的数据分别保存在 ClickHouse Cloud 各节点的本地。因此，如需查看所有数据的完整情况，需要使用 `clusterAllReplicas` 函数。更多详情请参见[此处](/zh/reference/system-tables/overview#system-tables-in-clickhouse-cloud)。
</Info>

<div id="description">
  ## 描述
</div>

包含执行查询的线程相关信息，例如线程名称、线程开始时间以及查询处理耗时。

要开始记录日志：

1. 在 [query\_thread\_log](/zh/reference/settings/server-settings/settings#query_thread_log) 部分中配置参数。
2. 将 [log\_query\_threads](/zh/reference/settings/session-settings#log_query_threads) 设置为 1。

数据刷新周期通过 [query\_thread\_log](/zh/reference/settings/server-settings/settings#query_thread_log) 服务器设置部分中的 `flush_interval_milliseconds` 参数进行设置。要强制刷新，请使用 [SYSTEM FLUSH LOGS](/zh/reference/statements/system#flush-logs) 查询。

ClickHouse 不会自动删除表中的数据。更多详情，请参见 [Introduction](/zh/reference/system-tables/overview#system-tables-introduction)。

你可以使用 [log\_queries\_probability](/zh/reference/settings/session-settings#log_queries_probability)) 设置来减少记录到 `query_thread_log` 表中的查询数量。

<div id="columns">
  ## 列
</div>

* `hostname` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — 执行该查询的服务器主机名。
* `event_date` ([Date](/zh/reference/data-types/date)) — 线程执行完查询时的日期。
* `event_time` ([DateTime](/zh/reference/data-types/datetime)) — 线程执行完查询时的日期和时间。
* `event_time_microseconds` ([DateTime64(6)](/zh/reference/data-types/datetime64)) — 线程完成查询执行时的日期和时间，精度为微秒。
* `query_start_time` ([DateTime](/zh/reference/data-types/datetime)) — 查询开始执行的时间。
* `query_start_time_microseconds` ([DateTime64(6)](/zh/reference/data-types/datetime64)) — 查询开始执行的时间，精确到微秒。
* `query_duration_ms` ([UInt64](/zh/reference/data-types/int-uint)) — 查询执行耗时。
* `read_rows` ([UInt64](/zh/reference/data-types/int-uint)) — 读取的行数。
* `read_bytes` ([UInt64](/zh/reference/data-types/int-uint)) — 已读取的字节数。
* `written_rows` ([UInt64](/zh/reference/data-types/int-uint)) — 对于 INSERT 查询，表示已写入的行数。对于其他查询，该列的值为 0。
* `written_bytes` ([UInt64](/zh/reference/data-types/int-uint)) — 对于 INSERT 查询，表示已写入的字节数。对于其他查询，此列的值为 0。
* `memory_usage` ([Int64](/zh/reference/data-types/int-uint)) — 在该线程的上下文中，已分配内存量与已释放内存量之间的差值。
* `peak_memory_usage` ([Int64](/zh/reference/data-types/int-uint)) — 该线程上下文中已分配内存与已释放内存之间的最大差值。
* `thread_name` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — 线程名称。
* `thread_id` ([UInt64](/zh/reference/data-types/int-uint)) — 内部线程 ID。
* `master_thread_id` ([UInt64](/zh/reference/data-types/int-uint)) — 初始线程的操作系统初始 ID。
* `current_database` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — 当前 database 的名称。
* `query` ([String](/zh/reference/data-types/string)) — 查询字符串。
* `normalized_query_hash` ([UInt64](/zh/reference/data-types/int-uint)) — 归一化查询的哈希值——其中的常量等已被去除。
* `is_initial_query` ([UInt8](/zh/reference/data-types/int-uint)) — 查询类型。可能的值：1 — 查询由客户端发起，0 — 查询由另一个查询为进行分布式查询执行而发起。
* `connection_address` ([IPv6](/zh/reference/data-types/ipv6)) — 建立 connection 的客户端 IP 地址。若通过 proxy 连接，则此地址为 proxy 的地址。
* `connection_port` ([UInt16](/zh/reference/data-types/int-uint)) — 建立 connection 时使用的客户端端口。若通过 proxy 连接，此处显示的将是 proxy 的端口。
* `user` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — 发起当前查询的用户名。
* `query_id` ([String](/zh/reference/data-types/string)) — 查询 ID。
* `address` ([IPv6](/zh/reference/data-types/ipv6)) — 用于发起查询的 IP 地址。若通过代理连接且设置了 `auth_use_forwarded_address`，此处显示的是客户端地址，而非代理地址。
* `port` ([UInt16](/zh/reference/data-types/int-uint)) — 用于发起查询的客户端端口。若通过代理连接，且设置了 `auth_use_forwarded_address`，这里显示的将是客户端端口，而不是代理端口。
* `initial_user` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — 执行初始查询的用户名 (用于分布式查询执行) 。
* `initial_query_id` ([String](/zh/reference/data-types/string)) — 初始查询 ID (用于分布式查询执行) 。
* `initial_address` ([IPv6](/zh/reference/data-types/ipv6)) — 发起父查询的 IP 地址。
* `initial_port` ([UInt16](/zh/reference/data-types/int-uint)) — 用于发起父级查询的客户端端口。
* `initial_query_start_time` ([DateTime](/zh/reference/data-types/datetime)) — 初始查询的执行开始时间。
* `initial_query_start_time_microseconds` ([DateTime64(6)](/zh/reference/data-types/datetime64)) — 初始查询开始执行的时间
* `authenticated_user` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — 会话中已通过身份验证的用户名。
* `interface` ([UInt8](/zh/reference/data-types/int-uint)) — 发起查询所使用的接口。可能的值：1 — TCP，2 — HTTP。
* `is_secure` ([UInt8](/zh/reference/data-types/int-uint)) — 用于指示连接是否安全的标志。
* `os_user` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — 运行 clickhouse-client 的操作系统用户名称。
* `client_hostname` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — 运行 clickhouse-client 或其他 TCP 客户端的客户机主机名。
* `client_name` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — `clickhouse-client` 或其他 TCP 客户端名称。
* `client_revision` ([UInt32](/zh/reference/data-types/int-uint)) — `clickhouse-client` 或其他 TCP 客户端的修订号。
* `client_version_major` ([UInt32](/zh/reference/data-types/int-uint)) — clickhouse-client 或其他 TCP 客户端的主版本。
* `client_version_minor` ([UInt32](/zh/reference/data-types/int-uint)) — clickhouse-client 或其他 TCP 客户端的次要版本号。
* `client_version_patch` ([UInt32](/zh/reference/data-types/int-uint)) — `clickhouse-client` 或其他 TCP 客户端版本号的补丁部分。
* `script_query_number` ([UInt32](/zh/reference/data-types/int-uint)) — 多查询脚本中的顺序查询编号。
* `script_line_number` ([UInt32](/zh/reference/data-types/int-uint)) — 多查询脚本中当前查询起始所在的行号。
* `http_method` ([UInt8](/zh/reference/data-types/int-uint)) — 发起该查询时使用的 HTTP 方法。可能的值：0 — 查询通过 TCP 接口发起，1 — 使用 GET 方法，2 — 使用 POST 方法。
* `http_user_agent` ([LowCardinality(String)](/zh/reference/data-types/lowcardinality)) — HTTP 请求中的 UserAgent 请求头。
* `http_referer` ([String](/zh/reference/data-types/string)) — 在 HTTP 查询中传递的 `Referer` HTTP 请求头 (包含发起该查询的页面的完整地址或部分地址) 。
* `forwarded_for` ([String](/zh/reference/data-types/string)) — 在 HTTP 查询中传递的 `X-Forwarded-For` HTTP 请求头。
* `quota_key` ([String](/zh/reference/data-types/string)) — quotas 设置中指定的“配额键”。
* `distributed_depth` ([UInt64](/zh/reference/data-types/int-uint)) — 查询在服务器之间被转发的次数。
* `revision` ([UInt32](/zh/reference/data-types/int-uint)) — ClickHouse 修订号。
* `ProfileEvents` ([Map(LowCardinality(String), UInt64)](/zh/reference/data-types/map)) — 用于统计该线程各项指标的 ProfileEvents。其说明可在 system.events 表中找到。

**别名：**

* `ProfileEvents.Names` — `mapKeys(ProfileEvents)` 的别名。
* `ProfileEvents.Values` — `mapValues(ProfileEvents)` 的别名。

<div id="example">
  ## 示例
</div>

```sql theme={null}
 SELECT * FROM system.query_thread_log LIMIT 1 \G
```

```text theme={null}
Row 1:
──────
hostname:                      clickhouse.eu-central1.internal
event_date:                    2020-09-11
event_time:                    2020-09-11 10:08:17
event_time_microseconds:       2020-09-11 10:08:17.134042
query_start_time:              2020-09-11 10:08:17
query_start_time_microseconds: 2020-09-11 10:08:17.063150
query_duration_ms:             70
read_rows:                     0
read_bytes:                    0
written_rows:                  1
written_bytes:                 12
memory_usage:                  4300844
peak_memory_usage:             4300844
thread_name:                   TCPHandler
thread_id:                     638133
master_thread_id:              638133
query:                         INSERT INTO test1 VALUES
is_initial_query:              1
user:                          default
query_id:                      50a320fd-85a8-49b8-8761-98a86bcbacef
address:                       ::ffff:127.0.0.1
port:                          33452
initial_user:                  default
initial_query_id:              50a320fd-85a8-49b8-8761-98a86bcbacef
initial_address:               ::ffff:127.0.0.1
initial_port:                  33452
interface:                     1
os_user:                       bharatnc
client_hostname:               tower
client_name:                   ClickHouse
client_revision:               54437
client_version_major:          20
client_version_minor:          7
client_version_patch:          2
http_method:                   0
http_user_agent:
quota_key:
revision:                      54440
ProfileEvents:        {'Query':1,'SelectQuery':1,'ReadCompressedBytes':36,'CompressedReadBufferBlocks':1,'CompressedReadBufferBytes':10,'IOBufferAllocs':1,'IOBufferAllocBytes':89,'ContextLock':15,'RWLockAcquiredReadLocks':1}
```

<div id="see-also">
  ## 另请参阅
</div>

* [system.query\_log](/zh/reference/system-tables/query_log) — `query_log` 系统表的说明，包含有关查询执行的常见信息。
* [system.query\_views\_log](/zh/reference/system-tables/query_views_log) — 此表包含查询执行期间每个视图的信息。
