Skip to main content

system_history.query_history

Records detailed logs of all SQL query executions in Databend. For each query, two entries are generated: one when the query starts and another when it finishes. This table is valuable for monitoring query activity, auditing user actions, and analyzing performance metrics.

Fields

FieldTypeDescription
log_typeTINYINTThe query status.
log_type_nameVARCHARThe name of query status.
handler_typeVARCHARThe protocol or handler used for the query (e.g., HTTPQuery, MySQL).
tenant_idVARCHARThe tenant identifier.
cluster_idVARCHARThe cluster identifier.
node_idVARCHARThe node identifier.
sql_userVARCHARThe user who executed the query.
sql_user_quotaVARCHARThe quota information of the user.
sql_user_privilegesVARCHARThe privileges of the user.
query_idVARCHARThe unique identifier for the query.
query_kindVARCHARThe kind of query (e.g., Query, Insert, CopyIntoTable, etc.).
query_textVARCHARThe SQL text of the query.
query_hashVARCHARThe hash value of the query text.
query_parameterized_hashVARCHARThe hash value of the query regardless of the specific values.
event_dateDATEThe date when the event occurred.
event_timeTIMESTAMPThe timestamp when the event occurred.
query_start_timeTIMESTAMPThe timestamp when the query started.
query_duration_msBIGINTThe duration of the query in milliseconds.
query_queued_duration_msBIGINTThe time the query spent in the queue in milliseconds.
current_databaseVARCHARThe database in use when the query was executed.
written_rowsBIGINT UNSIGNEDThe number of rows written by the query.
written_bytesBIGINT UNSIGNEDThe number of bytes written by the query.
join_spilled_rowsBIGINT UNSIGNEDThe number of rows spilled during join operations.
join_spilled_bytesBIGINT UNSIGNEDThe number of bytes spilled during join operations.
agg_spilled_rowsBIGINT UNSIGNEDThe number of rows spilled during aggregation operations.
agg_spilled_bytesBIGINT UNSIGNEDThe number of bytes spilled during aggregation operations.
group_by_spilled_rowsBIGINT UNSIGNEDThe number of rows spilled during group by operations.
group_by_spilled_bytesBIGINT UNSIGNEDThe number of bytes spilled during group by operations.
written_io_bytesBIGINT UNSIGNEDThe number of bytes written to IO.
written_io_bytes_cost_msBIGINT UNSIGNEDThe IO cost in milliseconds for writing.
scan_rowsBIGINT UNSIGNEDThe number of rows scanned by the query.
scan_bytesBIGINT UNSIGNEDThe number of bytes scanned by the query.
scan_io_bytesBIGINT UNSIGNEDThe number of IO bytes read during scanning.
scan_io_bytes_cost_msBIGINT UNSIGNEDThe IO cost in milliseconds for scanning.
scan_partitionsBIGINT UNSIGNEDThe number of partitions scanned.
total_partitionsBIGINT UNSIGNEDThe total number of partitions involved.
result_rowsBIGINT UNSIGNEDThe number of rows in the query result.
result_bytesBIGINT UNSIGNEDThe number of bytes in the query result.
bytes_from_remote_diskBIGINT UNSIGNEDThe number of bytes read from remote disk.
bytes_from_local_diskBIGINT UNSIGNEDThe number of bytes read from local disk.
bytes_from_memoryBIGINT UNSIGNEDThe number of bytes read from memory.
client_addressVARCHARThe address of the client that issued the query.
user_agentVARCHARThe user agent string of the client.
exception_codeINTThe exception code if the query failed.
exception_textVARCHARThe exception message if the query failed.
server_versionVARCHARThe version of the server that processed the query.
query_tagVARCHARThe tag associated with the query.
has_profileBOOLEANWhether the query has an associated execution profile.
peek_memory_usageVARIANTThe peak memory usage during query execution (as a JSON object).
session_idVARCHARThe session identifier associated with the query.

Examples

Query the history for a specific query using its query_id

SELECT * FROM system_history.query_history WHERE query_id = '4e1f50a9-bce2-45cc-86e4-c7a36b9b8d43';

*************************** 1. row ***************************
log_type: 2
log_type_name: Finish
handler_type: HTTPQuery
tenant_id: test_tenant
cluster_id: test_cluster
node_id: jxSgvulZFAq1sDckR1bu85
sql_user: root
sql_user_quota: NULL
sql_user_privileges: NULL
query_id: 4e1f50a9-bce2-45cc-86e4-c7a36b9b8d43
query_kind: Query
query_text: SELECT * FROM t
query_hash: cd36a2072e7f9deaa746db7480200944
query_parameterized_hash: cd36a2072e7f9deaa746db7480200944
event_date: 2025-06-12
event_time: 2025-06-12 03:31:35.135987
query_start_time: 2025-06-12 03:31:35.041725
query_duration_ms: 94
query_queued_duration_ms: 0
current_database: default
written_rows: 0
written_bytes: 0
join_spilled_rows: 0
join_spilled_bytes: 0
agg_spilled_rows: 0
agg_spilled_bytes: 0
group_by_spilled_rows: 0
group_by_spilled_bytes: 0
written_io_bytes: 0
written_io_bytes_cost_ms: 0
scan_rows: 1
scan_bytes: 20
scan_io_bytes: 605
scan_io_bytes_cost_ms: 0
scan_partitions: 1
total_partitions: 1
result_rows: 1
result_bytes: 20
bytes_from_remote_disk: 74
bytes_from_local_disk: 0
bytes_from_memory: 0
client_address: 127.0.0.1
user_agent: bendsql/0.26.2-unknown
exception_code: 0
exception_text:
server_version: v1.2.753-nightly-c3d5fabb79(rust-1.88.0-nightly-2025-06-12T01:48:36.733925000Z)
query_tag:
has_profile: NULL
peek_memory_usage: {"jxSgvulZFAq1sDckR1bu85":223840}
session_id: e3c54c32-f3c0-4ea9-bdd2-65701aa3f2a6
Explore Databend Cloud for FREE
Low-cost
Fast Analytics
Easy Data Ingestion
Elastic Scaling
Try it today