Skip to content

Commit f7aaadd

Browse files
authored
Merge pull request #170 from capgar/ca_query_log
Updated the useful-queries query_log page
2 parents 69e328d + 557b739 commit f7aaadd

2 files changed

Lines changed: 67 additions & 55 deletions

File tree

content/en/altinity-kb-useful-queries/altinity-kb-database-size-table-column-size.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Database Size - Table - Column size"
33
linkTitle: "Database Size - Table - Column size"
44
description: >
5-
Database Size - Table - Column size
5+
Queries to analyze size and compression rates across tables and columns
66
keywords:
77
- clickhouse database size
88
- clickhouse table size
@@ -52,7 +52,6 @@ ORDER BY size DESC;
5252
```
5353

5454

55-
5655
### Column size
5756

5857
> Returns size, compression rate, row counts, and average row size for each column (by db and table)
@@ -223,7 +222,7 @@ ORDER BY
223222
```
224223

225224

226-
## Understanding the partitioning
225+
## Understanding partitioning
227226

228227
> Partition distribution analysis, aggregating system.parts metrics by partition. The quantiles results can indicate whether there is skewed distribution of data between partitions.
229228
@@ -262,7 +261,7 @@ FORMAT Vertical
262261

263262
## Subcolumns sizes
264263

265-
Returns column-level storage metricsk, including subcolumns (JSON, tuples, maps, etc - if present)
264+
> Returns column-level storage metrics, including subcolumns (JSON, tuples, maps, etc - if present)
266265
267266
```sql
268267
WITH

content/en/altinity-kb-useful-queries/query_log.md

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,16 @@ title: "Handy queries for system.query_log"
33
linkTitle: "Handy queries for system.query_log"
44
weight: 100
55
description: >-
6-
Handy queries for a system.query_log.
6+
Useful queries for analyzing query performance, resource usage, and overall query statistics
7+
keywords:
8+
- clickhouse query performance
79
---
810

9-
## The most cpu / write / read-intensive queries from query_log
10-
11-
```sql
12-
SELECT
13-
normalized_query_hash,
14-
any(query),
15-
count(),
16-
sum(query_duration_ms) / 1000 AS QueriesDuration,
17-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'RealTimeMicroseconds')]) / 1000000 AS RealTime,
18-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'UserTimeMicroseconds')]) / 1000000 AS UserTime,
19-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'SystemTimeMicroseconds')]) / 1000000 AS SystemTime,
20-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'DiskReadElapsedMicroseconds')]) / 1000000 AS DiskReadTime,
21-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'DiskWriteElapsedMicroseconds')]) / 1000000 AS DiskWriteTime,
22-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'NetworkSendElapsedMicroseconds')]) / 1000000 AS NetworkSendTime,
23-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'NetworkReceiveElapsedMicroseconds')]) / 1000000 AS NetworkReceiveTime,
24-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'ZooKeeperWaitMicroseconds')]) / 1000000 AS ZooKeeperWaitTime,
25-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'OSIOWaitMicroseconds')]) / 1000000 AS OSIOWaitTime,
26-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'OSCPUWaitMicroseconds')]) / 1000000 AS OSCPUWaitTime,
27-
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'OSCPUVirtualTimeMicroseconds')]) / 1000000 AS OSCPUVirtualTime,
28-
sum(read_rows) AS ReadRows,
29-
formatReadableSize(sum(read_bytes)) AS ReadBytes,
30-
sum(written_rows) AS WrittenTows,
31-
formatReadableSize(sum(written_bytes)) AS WrittenBytes,
32-
sum(result_rows) AS ResultRows,
33-
formatReadableSize(sum(result_bytes)) AS ResultBytes
34-
FROM system.query_log
35-
WHERE (event_date >= today()) AND (event_time > (now() - 3600)) AND type in (2,4) -- QueryFinish, ExceptionWhileProcessing
36-
GROUP BY normalized_query_hash
37-
WITH TOTALS
38-
ORDER BY UserTime DESC
39-
LIMIT 30
40-
FORMAT Vertical
41-
```
42-
43-
-- modern ClickHouse®
44-
11+
## Most resource-intensive queries
12+
> For each query (cluster-wide, grouped by query hash and ordered by time), reports:
13+
- Latency-related metrics: CPU time categories, disk read and write time, network send and receive time, Zookeeper wait time
14+
- Data size-related metrics: counts of bytes and rows read/written, parts/ranges/marks read, files opened, and memory used
15+
- Cache hit performance
4516
```sql
4617
SELECT
4718
hostName() as host,
@@ -74,7 +45,7 @@ SELECT
7445
sum(ProfileEvents['ZooKeeperTransactions']) as ZooKeeperTransactions,
7546
formatReadableSize(sum(ProfileEvents['OSReadBytes'] ) as os_read_bytes ) as OSReadBytesExcludePageCache,
7647
formatReadableSize(sum(ProfileEvents['OSWriteBytes'] ) as os_write_bytes ) as OSWriteBytesExcludePageCache,
77-
formatReadableSize(sum(ProfileEvents['OSReadChars'] ) as os_read_chars ) as OSReadBytesIncludePageCache,
48+
formatReadableSize(sum(ProfileEvents['OSReadChars'] ) as os_read_chars ) as OSReadCharsIncludePageCache,
7849
formatReadableSize(sum(ProfileEvents['OSWriteChars'] ) as os_write_chars ) as OSWriteCharsIncludePageCache,
7950
formatReadableSize(quantile(0.97)(memory_usage) as memory_usage_q97) as MemoryUsageQ97 ,
8051
sum(read_rows) AS ReadRows,
@@ -95,7 +66,42 @@ LIMIT 30
9566
FORMAT Vertical;
9667
```
9768

69+
> Similar to above, for older ClickHouse versions (pre-22.4). Returns the slowest queries from a single host along with elements of latency.
70+
```sql
71+
SELECT
72+
normalized_query_hash,
73+
any(query),
74+
count(),
75+
sum(query_duration_ms) / 1000 AS QueriesDuration,
76+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'RealTimeMicroseconds')]) / 1000000 AS RealTime,
77+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'UserTimeMicroseconds')]) / 1000000 AS UserTime,
78+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'SystemTimeMicroseconds')]) / 1000000 AS SystemTime,
79+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'DiskReadElapsedMicroseconds')]) / 1000000 AS DiskReadTime,
80+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'DiskWriteElapsedMicroseconds')]) / 1000000 AS DiskWriteTime,
81+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'NetworkSendElapsedMicroseconds')]) / 1000000 AS NetworkSendTime,
82+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'NetworkReceiveElapsedMicroseconds')]) / 1000000 AS NetworkReceiveTime,
83+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'ZooKeeperWaitMicroseconds')]) / 1000000 AS ZooKeeperWaitTime,
84+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'OSIOWaitMicroseconds')]) / 1000000 AS OSIOWaitTime,
85+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'OSCPUWaitMicroseconds')]) / 1000000 AS OSCPUWaitTime,
86+
sum(ProfileEvents.Values[indexOf(ProfileEvents.Names, 'OSCPUVirtualTimeMicroseconds')]) / 1000000 AS OSCPUVirtualTime,
87+
sum(read_rows) AS ReadRows,
88+
formatReadableSize(sum(read_bytes)) AS ReadBytes,
89+
sum(written_rows) AS WrittenTows,
90+
formatReadableSize(sum(written_bytes)) AS WrittenBytes,
91+
sum(result_rows) AS ResultRows,
92+
formatReadableSize(sum(result_bytes)) AS ResultBytes
93+
FROM system.query_log
94+
WHERE (event_date >= today()) AND (event_time > (now() - 3600)) AND type in (2,4) -- QueryFinish, ExceptionWhileProcessing
95+
GROUP BY normalized_query_hash
96+
WITH TOTALS
97+
ORDER BY UserTime DESC
98+
LIMIT 30
99+
FORMAT Vertical
100+
```
101+
98102
## A/B tests of the same query
103+
104+
> Runs cluster-wide, returns a side-by-side comparison of performance metrics, ordered by relative difference
99105
```
100106
WITH
101107
query_id='8c050082-428e-4523-847a-caf29511d6ba' AS first,
@@ -118,11 +124,11 @@ ORDER BY
118124
metric ASC
119125
```
120126

121-
Another variant
127+
> Compares two queries run on the same host in the past day, returning the metrics highlighting the most significant performance differences between the faster and slower query
122128
```
123129
WITH
124-
toUUID('d18fb820-4075-49bf-8fa3-cd7e53b9d523') AS fast_query_id,
125-
toUUID('22ffbcc0-c62a-4895-8105-ee9d7447a643') AS slow_query_id,
130+
'd18fb820-4075-49bf-8fa3-cd7e53b9d523' AS fast_query_id,
131+
'22ffbcc0-c62a-4895-8105-ee9d7447a643' AS slow_query_id,
126132
faster AS
127133
(
128134
SELECT pe.1 AS event_name, pe.2 AS event_value
@@ -157,28 +163,33 @@ ORDER BY event_name ASC
157163
SETTINGS join_use_nulls = 1
158164
```
159165

160-
## Find queries that were started but not finished at some moment in time
161166

167+
## Queries which did not complete within specified timeframe
168+
> For a given time range, returns queries which either did not complete, or did not complete within a configurable timeframe (100 seconds)
162169
```sql
163170
SELECT
164171
query_id,
165172
min(event_time) t,
166173
any(query)
167174
FROM system.query_log
168-
where event_date = today() and event_time > '2021-11-25 02:29:12'
175+
WHERE event_date = today() AND event_time > '2021-11-25 02:29:12'
169176
GROUP BY query_id
170177
HAVING countIf(type='QueryFinish') = 0 OR sum(query_duration_ms) > 100000
171-
order by t;
178+
ORDER BY t;
179+
```
172180

173-
select
181+
> Returns queries which started within a specified timeframe but did not complete successfully (still running, crashed, threw exception)
182+
``` sql
183+
SELECT
174184
query_id,
175185
any(query)
176-
from system.query_log
177-
where event_time between '2021-09-24 07:00:00' and '2021-09-24 09:00:00'
178-
group by query_id HAVING countIf(type=1) <> countIf(type!=1)
186+
FROM system.query_log
187+
WHERE event_time BETWEEN '2021-09-24 07:00:00' AND '2021-09-24 09:00:00'
188+
GROUP BY query_id HAVING countIf(type=1) <> countIf(type!=1)
179189
```
180190

181191
## Columns used in WHERE clauses
192+
> Returns a list of columns which are used as filters against a table. Replace %target_table% with the actual table name (or pattern) you want to inspect.
182193
```
183194
WITH
184195
any(query) AS q,
@@ -197,9 +208,9 @@ WHERE (event_time >= (now() - toIntervalDay(1)))
197208
GROUP BY c2
198209
ORDER BY count() ASC;
199210
```
200-
Replace %target_table% with the actual table name (or pattern) you want to inspect.
201211

202212
## Most‑selected columns
213+
> Over the past week, which columns have been accessed the most frequently in SELECT queries
203214
204215
```
205216
SELECT
@@ -217,6 +228,7 @@ LIMIT 50;
217228
```
218229

219230
## Most‑used functions
231+
> Over the past week, which functions have been used the most
220232
221233
```
222234
SELECT
@@ -232,9 +244,10 @@ ORDER BY hits DESC
232244
LIMIT 50;
233245
```
234246

235-
## query ranks
236-
```
247+
## "Worst offender" query ranks
248+
> Over a specified time range, returns the query shapes which appear to be the worst performing based on a range of ranked criteria
237249
250+
```
238251
SELECT *
239252
FROM
240253
(

0 commit comments

Comments
 (0)