#只在当前会话中有效 SET log_queries = 0; ## -- We do not recommend to turn off logging because information in this table is important for solving issues. # 官方不建议关闭
<query_log> <!-- What table to insert data. If table is not exist, it will be created. When query log structure is changed after system update, then old table will be renamed and new table will be created automatically. --> <database>system</database> <table>query_log</table> <!-- PARTITION BY expr: https://clickhouse.com/docs/en/table_engines/mergetree-family/custom_partitioning_key/ Example: event_date toMonday(event_date) toYYYYMM(event_date) toStartOfHour(event_time) --> <partition_by>toYYYYMM(event_date)</partition_by> <!-- Table TTL specification: https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree/#mergetree-table-ttl Example: event_date + INTERVAL 1 WEEK event_date + INTERVAL 7 DAY DELETE event_date + INTERVAL 2 WEEK TO DISK 'bbb' <ttl>event_date + INTERVAL 30 DAY DELETE</ttl> -->
<!-- ORDER BY expr: https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#order_by Example: event_date, event_time event_date, type, query_id event_date, event_time, initial_query_id <order_by>event_date, event_time, initial_query_id</order_by> -->
<!-- Instead of partition_by, you can provide full engine expression (starting with ENGINE = ) with parameters, Example: <engine>ENGINE = MergeTree PARTITION BY toYYYYMM(event_date) ORDER BY (event_date, event_time) SETTINGS index_granularity = 1024</engine> -->
<!-- Interval of flushing data. --> <flush_interval_milliseconds>7500</flush_interval_milliseconds> <!-- Maximal size in lines for the logs. When non-flushed logs amount reaches max_size, logs dumped to the disk. --> <max_size_rows>1048576</max_size_rows> <!-- Pre-allocated size in lines for the logs. --> <reserved_size_rows>8192</reserved_size_rows> <!-- Lines amount threshold, reaching it launches flushing logs to the disk in background. --> <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold> <!-- Indication whether logs should be dumped to the disk in case of a crash --> <flush_on_crash>false</flush_on_crash>
<!-- example of using a different storage policy for a system table --> <!-- storage_policy>local_ssd</storage_policy --> </query_log>
SELECT user, query_id, query_start_time, query_duration_ms, initial_query_id, --query, tables, read_rows, memory_usage FROM clusterAllReplicas(xxx, system.query_log) WHERE hasAny(databases, ['xxx', 'xxx']) AND (event_time >= (now() - toIntervalMinute(3))) AND user = 'ck_sink_user' ORDER BY query_duration_ms DESC limit 10 ;