-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_sql_statistics_sample.sql
More file actions
61 lines (61 loc) · 3.18 KB
/
query_sql_statistics_sample.sql
File metadata and controls
61 lines (61 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
WITH stmt_hr_calc AS (
SELECT
aggregated_ts,
app_name,
fingerprint_id,
metadata->>'query' as queryTxt,
(select string_agg(z,E'\n') from crdb_internal.decode_plan_gist(statistics->'statistics'->'planGists'->>0) as z) as sampled_plan,
-- 'SELECT ZZZ' as sampled_plan,
IF (metadata->'implicitTxn' = 'false', 1, 0) as explicitTxn,
IF (metadata->'fullScan' = 'true', 1, 0) as fullScan,
IF ((select string_agg(z,E'\n') from crdb_internal.decode_plan_gist(statistics->'statistics'->'planGists'->>0) as z) like '%index join%', 1, 0) as ijoinStmt,
CAST(statistics->'statistics'->'numRows'->>'mean' as FLOAT)::INT as numRows,
CAST(statistics->'statistics'->'rowsRead'->>'mean' as FLOAT)::INT as rowsRead,
CASE
WHEN CAST(statistics->'statistics'->'numRows'->>'mean' as FLOAT)::INT > CAST(statistics->'statistics'->'rowsRead'->>'mean' as FLOAT)::INT
THEN CAST(statistics->'statistics'->'numRows'->>'mean' as FLOAT)::INT
ELSE CAST(statistics->'statistics'->'rowsRead'->>'mean' as FLOAT)::INT
END as rowsMean,
CAST(statistics->'statistics'->'cnt' as INT) as execCnt
FROM crdb_internal.statement_statistics
WHERE 1=1
AND aggregated_ts > now() - INTERVAL '1hr'
AND app_name not like '$ internal-%'
AND metadata->>'query' not like '%stmt_hr_calc%'
AND metadata->>'query' not like '%FROM system.%'
), sql_distinct_cnt as (
SELECT DISTINCT aggregated_ts,
-- app_name,
-- fingerprint_id,
substring(queryTxt for 30) as queryTxt,
-- sampled_plan,
sum(fullScan) OVER (PARTITION BY aggregated_ts, fingerprint_id) as fullCnt,
sum(iJoinStmt) OVER (PARTITION BY aggregated_ts, fingerprint_id) as iJoinCnt,
sum(explicitTxn) OVER (PARTITION BY aggregated_ts, fingerprint_id) as explicitCnt,
sum(IF((fullScan = 0) and (iJoinStmt = 0) and (explicitTxn = 0), 1, 0))
OVER (PARTITION BY aggregated_ts, fingerprint_id) as healthyCnt,
sum(execCnt) OVER (PARTITION BY aggregated_ts) as execTotal,
sum(rowsMean * execCnt) OVER (PARTITION BY aggregated_ts) as lioTotal,
sum(rowsMean * execCnt) OVER (PARTITION BY aggregated_ts, fingerprint_id) as lioPerStmt
FROM stmt_hr_calc
ORDER BY lioPerStmt
), lio_normalization as (
SELECT aggregated_ts,
lioTotal,
sum(lioPerStmt * (IF(fullCnt > 0, 1, 0))) as fullLio,
sum(lioPerStmt * (IF(iJoinCnt > 0, 1, 0))) as iJoinLio,
sum(lioPerStmt * (IF(explicitCnt > 0, 1, 0))) as explicitLio,
sum(lioPerStmt * (IF(healthyCnt > 0, 1, 0))) as healtyLio
FROM sql_distinct_cnt
GROUP BY 1, 2
)
SELECT
-- experimental_strftime(aggregated_ts,'%Y-%m-%d %H:%M:%S%z') as aggregated_ts,
extract(epoch from aggregated_ts) as aggEpochSecs,
LioTotal,
fullLio,
iJoinLio,
explicitLio,
healtyLio
FROM lio_normalization
;