First of all, you need to enable instant query in Grafana. In this case Grafana will return only the last data point on the selected time range per each resulting time series instead of returning many data points per time series on the selected time range.

Other considerations:

  • if my_app_login is a counter, then the following query should returns top users with the most login actions during the last 24 hours:
topk(10, sum(increase(my_app_login[24h])) by (User_Name))

Note that Prometheus may return fractional results from this query even if my_app_login contains only integer values. This is because of data model quirks for increase() function in Prometheus - see this comment and this article for details. If you need exact integer results, then take a look at MetricsQL from VictoriaMetrics.

  • if my_app_login is a gauge, which contains the number of app logins since the previous sample, then the following query should work:
topk(10, sum(sum_over_time(my_app_login[24h])) by (User_Name))

See sum_over_time() docs for more details.

Note that topk(N, ...) function may return more than N time series when this function is used for building a graph over time (aka range query). This is because it returns top N time series independently per each timestamp on the graph. If you need no more than top N time series on the graph, then take a look at topk_max, topk_last and other topk_* functions from MetricsQL.

Answer from valyala on Stack Overflow
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › operators
Operators | Prometheus
topk(k, v) and bottomk(k, v) are different from other aggregators in that a subset of k values from the input samples, including the original labels, are returned in the result vector.
Discussions

topk/bottomk X over time, X = [avg, sum, min, max, etc...]
Proposal Use case. Why is this important? Quoting this comment here Another thing we cannot do natively in PromQL is graphing top N time series. All the details are in this blog post. TL;DR is, if ... More on github.com
🌐 github.com
8
September 7, 2020
How to use PromQL operators to limit/filter query values?
I have HTTP request logs with client IP addresses and I have a panel with the following query from Loki’s Prometheus data source (http://localhost:3100/loki): topk(5, sum by (client_ip)(rate({host="webserver.local",job="nginx_access",client_ip=~".+"}[1m]))) This query shows a time series ... More on community.grafana.com
🌐 community.grafana.com
6
0
April 5, 2020
topk/bottomk performance and correctness
Describe the bug I have had a few issues with topk, bottomk functions. Follow your docs, topk and bottomk return up to k points with the smallest values or biggest values across all the time series returned by q. But sometimes, bottomk f... More on github.com
🌐 github.com
9
August 16, 2023
People also ask

When should I use recording rules in Prometheus?
Use recording rules for expensive queries that run frequently — particularly aggregations over large label sets or queries used in multiple dashboards. Recording rules precompute results on a schedule and store them as new metrics, reducing query latency at read time.
🌐
last9.io
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
How do I find the top N series by a metric in PromQL?
Use topk(N, expr). For example, topk(5, sum(rate(http_requests_total[5m])) by (service)) returns the 5 services with the highest request rate. Use bottomk() for the lowest values.
🌐
last9.io
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
What is PromQL?
PromQL (Prometheus Query Language) is the query language built into Prometheus for selecting, filtering, and aggregating time series data. You use it to write expressions that power dashboards, alerts, and ad-hoc metric analysis.
🌐
last9.io
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
🌐
Promlabs
promlabs.com › promql-cheat-sheet
PromLabs | PromQL Cheat Sheet
Available aggregation operators: sum(), min(), max(), avg(), stddev(), stdvar(), count(), count_values(), group(), bottomk(), topk(), quantile()
🌐
Google Groups
groups.google.com › g › prometheus-developers › c › 5T1BDDGynUg
topk() sorting
January 15, 2021 - Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... Prometheus itself returns topk() results sorted from highest to lowest value (as can be evidenced if you evaluate your expression in Prometheus's expression ...
🌐
GitHub
github.com › prometheus › prometheus › issues › 7903
topk/bottomk X over time, X = [avg, sum, min, max, etc...] · Issue #7903 · prometheus/prometheus
September 7, 2020 - The aim of this is to be able to do topk X over time where X can be average, sum, min, max, median, etc. Update: Developers mailing list: https://groups.google.com/g/prometheus-developers/c/K8HUmH7t6hk Design doc: https://docs.google.com/document/d/1uSbD3T2beM-iX4-Hp7V074bzBRiRNlqUdcWP6JTDQSs/edit?usp=sharing ·
Author: prometheus
🌐
Grafana
grafana.com › blog › inside-promql-a-closer-look-at-the-mechanics-of-a-prometheus-query
Inside PromQL: A closer look at the mechanics of a Prometheus query | Grafana Labs
October 9, 2024 - topk, bottomk, limitk, or limit_ratio: Output has the same labels as the input, but just k (which is a number, and the first parameter to the aggregation) of them per group.
Find elsewhere
🌐
Robust Perception
robustperception.io › which-are-my-biggest-metrics
Which are my biggest metrics? – Robust Perception | Prometheus Monitoring Experts
December 16, 2015 - It can be used not only to analyse the metrics from your individual services, but also to analyse across all the metrics inside your Prometheus server. To know which metrics are using the most resources it'd be good to count how many time series each has, and then display the top 10. This can be done with an expression selecting all metrics, aggregating a count based on the metric name and returning the top 10 in the expression browser (make sure to use the Console view): topk(10, count by (__name__)({__name__=~".+"})) On the live demo this shows that Conway's Life has the biggest time series:
🌐
Last9
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
September 12, 2024 - Use topk(N, expr). For example, topk(5, sum(rate(http_requests_total[5m])) by (service)) returns the 5 services with the highest request rate. Use bottomk() for the lowest values. histogram_quantile(phi, metric) calculates a quantile from a ...
🌐
VictoriaMetrics
docs.victoriametrics.com › victoriametrics › metricsql
VictoriaMetrics: MetricsQL
topk_min · zscore · Subqueries · Implicit query conversions · Back to Page · VictoriaMetrics implements MetricsQL - query language inspired by PromQL . MetricsQL is backwards-compatible with PromQL, so Grafana dashboards backed by Prometheus datasource should work the same after switching from Prometheus to VictoriaMetrics.
🌐
Grafana
community.grafana.com › prometheus
How to use PromQL operators to limit/filter query values? - Prometheus - Grafana Labs Community Forums
April 5, 2020 - I have HTTP request logs with client IP addresses and I have a panel with the following query from Loki’s Prometheus data source (http://localhost:3100/loki): topk(5, sum by (client_ip)(rate({host="webserver.local",job="nginx_access",client_ip=~".+"}[1m]))) This query shows a time series graph with the top 5 Client IP addresses with the most requests/minute.
🌐
OKD Documentation
docs.okd.io › 4.18 › virt › monitoring › virt-prometheus-queries.html
Prometheus queries for virtual resources - Monitoring | Virtualization | OKD 4.18
topk(3, sum by (name, namespace) (rate(kubevirt_vmi_network_receive_bytes_total[6m])) + sum by (name, namespace) (rate(kubevirt_vmi_network_transmit_bytes_total[6m]))) > 0 (1) You can monitor virtual machine storage traffic and identify high-traffic VMs by using Prometheus queries.
🌐
Gitbook
yunlzheng.gitbook.io › prometheus-book › parti-prometheus-ji-chu › promql › prometheus-aggr-ops
PromQL聚合操作 | prometheus-book
Prometheus还提供了下列内置的聚合操作符,这些操作符作用域瞬时向量。可以将瞬时表达式返回的样本数据进行聚合,形成一个新的时间序列。 · sum (求和) min (最小值) max (最大值) avg (平均值) stddev (标准差) stdvar (标准方差) count (计数) count_values (对value进行计数) bottomk (后n条时序) topk (前n条时序) quantile (分位数) 使用聚合操作的语法如下: ·
🌐
Percona
percona.com › sites › default › files › presentations › Prometheus-MySQL-2101.pdf pdf
Monitoring MySQL with Prometheus Prometheus
Prometheus · In Production · ●1500+ Tables · ●160+ Databases · ●30+ Clusters · ●250+ Servers · ●6000+ Events Digests · Prometheus · Real-World Load · ●1.5 million timeseries · ●80k samples per second · ●400GB (28 days, old encoding) ●10 cores (2.0ghz Ivy-Bridge) Prometheus ·
🌐
Guides
docs.observeinc.com › docs › verb-topk
topk
May 4, 2026 - topk k: int64, [score: expression]?, [groupby: col storable]? Retains rows that belong to the highest-ranked groups in the current query window and adds an int64 _c_rank column giving each kept row its group’s rank. k must be a compile-time non-negative int64 .
🌐
Google Translate
translate.google.com › translate
High Cardinality in Prometheus: How to Find and Fix It | Last9
June 11, 2026 - Look for metrics with a significantly higher count than others. - topk(10, count by (__name__, job)({__name__=~".+"})): It returns the top 10 highest series counts by metric name and job.
🌐
Google Translate
translate.google.com › home › prometheus monitoring › prometheus metrics
Prometheus Metrics: A Practical Guide | Tigera – Creator of Calico
July 30, 2021 - topk(3, metric_per_second) An arithmetic binary operator (+, -, *, /, %, ^), where “%” stands for a modulo operation and “^” stands for arithmetic power operation, can work with a combination of scalars and instant vectors, which can ...
🌐
GitHub
github.com › VictoriaMetrics › VictoriaMetrics › issues › 4844
topk/bottomk performance and correctness · Issue #4844 · VictoriaMetrics/VictoriaMetrics
August 16, 2023 - Describe the bug I have had a few issues with topk, bottomk functions. Follow your docs, topk and bottomk return up to k points with the smallest values or biggest values across all the time series returned by q. But sometimes, bottomk f...
Author: VictoriaMetrics
🌐
Google Groups
groups.google.com › g › prometheus-users › c › LeAiyKcT2kk
topk(10, count by (__name__, job)({__name__=~".+"})) Unexpected Token
That query itself should be ok, but if you get a JSON parsing error for the response coming back from Prometheus, that can indeed mean that your Prometheus died as a result of an OOM and whatever is in front of it returns invalid JSON in that case... probably no way to make it work without giving your Prometheus more memory, if that's the problem. Btw. you could switch '=~".+"' with "!=""' to get rid of regex matching, but that shouldn't make a difference. ... Running into more OOM problems, and trying to use topk(10, count by (__name__, job)({__name__=~".+"})) .