# 1. Prerequisites:
The metric topsql_cpu_time_ms is of Gauge type,
Query example:
promQL: topsql_cpu_time_ms{sql_digest="9D5A0F1A8C1C0E1FD5F0955DA80B305EAE920A2AB897319C6D2D8DE5D890BEDC"}
api: /api/v1/query
curl 'https://www.ds.us-east-1.aws.observability.tidbcloud.com/internal/metrics/ed869d81-91c5-4a87-a28d-20795c738d3f/api/v1/query?query=topsql_cpu_time_ms%7Bsql_digest%3D%2219D5A0F1A8C1C0E1FD5F0955DA80B305EAE920A2AB897319C6D2D8DE5D890BEDC%22%7D&time=1753431615.819&step=3s'

{"status":"success","isPartial":false,"data":{"resultType":"vector","result":[{"metric":{"__name__":"topsql_cpu_time_ms","cluster_id":"1379661944646416027","instance":"db-tikv-181.db-tikv-peer.tidb1379661944646416027.svc:20160","instance_type":"tikv","k8s_cluster_name":"prod-1372813089454544954-eks-us-east-1-17f21136","k8s_namespace":"tidb1379661944646416027","plan_digest":"03FA0F18132C15290435CCD6930C03FE7257C7017164897B1E5ACDD3C92E1828","project_id":"1372813089454544954","provider":"aws","region":"us-east-1","sql_digest":"1A96197A635AB3D1C0FC027CE178D8AA751E4BD033C39DB6163D3BA75B49FE3F","tag_label":"row","tenant_id":"1372813089209061633"},"value":[1753431615.819,"20"]},{"metric":{"__name__":"topsql_cpu_time_ms","cluster_id":"1379661944646416027","instance":"db-tikv-24.db-tikv-peer.tidb1379661944646416027.svc:20160","instance_type":"tikv","k8s_cluster_name":"prod-1372813089454544954-eks-us-east-1-17f21136","k8s_namespace":"tidb1379661944646416027","plan_digest":"03FA0F18132C15290435CCD6930C03FE7257C7017164897B1E5ACDD3C92E1828","project_id":"1372813089454544954","provider":"aws","region":"us-east-1","sql_digest":"1A96197A635AB3D1C0FC027CE178D8AA751E4BD033C39DB6163D3BA75B49FE3F","tag_label":"row","tenant_id":"1372813089209061633"},"value":[1753431615.819,"20"]},{"metric":{"__name__":"topsql_cpu_time_ms","cluster_id":"1379661944646416027","instance":"db-tikv-36.db-tikv-peer.tidb1379661944646416027.svc:20160","instance_type":"tikv","k8s_cluster_name":"prod-1372813089454544954-eks-us-east-1-17f21136","k8s_namespace":"tidb1379661944646416027","plan_digest":"03FA0F18132C15290435CCD6930C03FE7257C7017164897B1E5ACDD3C92E1828","project_id":"1372813089454544954","provider":"aws","region":"us-east-1","sql_digest":"1A96197A635AB3D1C0FC027CE178D8AA751E4BD033C39DB6163D3BA75B49FE3F","tag_label":"row","tenant_id":"1372813089209061633"},"value":[1753431615.819,"39"]},{"metric":{"__name__":"topsql_cpu_time_ms","cluster_id":"1379661944646416027","instance":"db-tikv-37.db-tikv-peer.tidb1379661944646416027.svc:20160","instance_type":"tikv","k8s_cluster_name":"prod-1372813089454544954-eks-us-east-1-17f21136","k8s_namespace":"tidb1379661944646416027","plan_digest":"03FA0F18132C15290435CCD6930C03FE7257C7017164897B1E5ACDD3C92E1828","project_id":"1372813089454544954","provider":"aws","region":"us-east-1","sql_digest":"1A96197A635AB3D1C0FC027CE178D8AA751E4BD033C39DB6163D3BA75B49FE3F","tag_label":"row","tenant_id":"1372813089209061633"},"value":[1753431615.819,"30"]},{"metric":{"__name__":"topsql_cpu_time_ms","cluster_id":"1379661944646416027","instance":"db-tikv-41.db-tikv-peer.tidb1379661944646416027.svc:20160","instance_type":"tikv","k8s_cluster_name":"prod-1372813089454544954-eks-us-east-1-17f21136","k8s_namespace":"tidb1379661944646416027","plan_digest":"03FA0F18132C15290435CCD6930C03FE7257C7017164897B1E5ACDD3C92E1828","project_id":"1372813089454544954","provider":"aws","region":"us-east-1","sql_digest":"1A96197A635AB3D1C0FC027CE178D8AA751E4BD033C39DB6163D3BA75B49FE3F","tag_label":"row","tenant_id":"1372813089209061633"},"value":[1753431615.819,"20"]},{"metric":{"__name__":"topsql_cpu_time_ms","cluster_id":"1379661944646416027","instance":"db-tikv-62.db-tikv-peer.tidb1379661944646416027.svc:20160","instance_type":"tikv","k8s_cluster_name":"prod-1372813089454544954-eks-us-east-1-17f21136","k8s_namespace":"tidb1379661944646416027","plan_digest":"03FA0F18132C15290435CCD6930C03FE7257C7017164897B1E5ACDD3C92E1828","project_id":"1372813089454544954","provider":"aws","region":"us-east-1","sql_digest":"1A96197A635AB3D1C0FC027CE178D8AA751E4BD033C39DB6163D3BA75B49FE3F","tag_label":"row","tenant_id":"1372813089209061633"},"value":[1753431615.819,"40"]},{"metric":{"__name__":"topsql_cpu_time_ms","cluster_id":"1379661944646416027","instance":"db-tikv-67.db-tikv-peer.tidb1379661944646416027.svc:20160","instance_type":"tikv","k8s_cluster_name":"prod-1372813089454544954-eks-us-east-1-17f21136","k8s_namespace":"tidb1379661944646416027","plan_digest":"03FA0F18132C15290435CCD6930C03FE7257C7017164897B1E5ACDD3C92E1828","project_id":"1372813089454544954","provider":"aws","region":"us-east-1","sql_digest":"1A96197A635AB3D1C0FC027CE178D8AA751E4BD033C39DB6163D3BA75B49FE3F","tag_label":"row","tenant_id":"1372813089209061633"},"value":[1753431615.819,"19"]},{"metric":{"__name__":"topsql_cpu_time_ms","cluster_id":"1379661944646416027","instance":"db-tikv-74.db-tikv-peer.tidb1379661944646416027.svc:20160","instance_type":"tikv","k8s_cluster_name":"prod-1372813089454544954-eks-us-east-1-17f21136","k8s_namespace":"tidb1379661944646416027","plan_digest":"03FA0F18132C15290435CCD6930C03FE7257C7017164897B1E5ACDD3C92E1828","project_id":"1372813089454544954","provider":"aws","region":"us-east-1","sql_digest":"1A96197A635AB3D1C0FC027CE178D8AA751E4BD033C39DB6163D3BA75B49FE3F","tag_label":"row","tenant_id":"1372813089209061633"},"value":[1753431615.819,"20"]}]},"stats":{"seriesFetched": "34","executionTimeMsec":61}} 

# 2. Requirements:
a) Total CPU time for a specified SQL Digest within the past day
b) Within the past day, with 5-minute cycles, the instance that used the most CPU for the specified Digest across all cycles, i.e., the global maximum value

# 3. Implementation:
a) Total CPU time for a specified SQL Digest within the past day
 api :/prometheus/api/v1/query
 query:sum_over_time(topsql_cpu_time_ms{sql_digest="xxxxxx"}[1d])
```

b) Within the past day, with 5-minute cycles, the instance that used the most CPU for the specified Digest across all cycles, i.e., the global maximum value
Solution 1:
api :/prometheus/api/v1/query_range
query=max_over_time(topsql_cpu_time_ms{sql_digest="..."})[5m]
start=2025-07-27T00:00:00Z
end=2025-07-27T23:59:59Z
step=300

After extracting the array results above, iterate through the query to find the global maximum value and instance.

Solution 2:
api :/prometheus/api/v1/query_range
query=topk(1, max_over_time(topsql_cpu_time_ms{sql_digest="YOUR_SQL_DIGEST"}[5m]))
start=2025-07-27T00:00:00Z
end=2025-07-27T23:59:59Z
step=300

After extracting the array results above, iterate through the query to find the global maximum value and instance.

Solution 2:
api :/prometheus/api/v1/query
topk(1, max_over_time(sum_over_time(topsql_cpu_time_ms{sql_digest="..."}[5m])[1d:1m]))

