The provided query is valid MetricsQL query for VictoriaMetrics, but unfortunately it doesn't work in the original PromQL.

Prometheus provides topk and bottomk operators, which can be used for limiting the number of returned time series. Unfortunately, these operators limit the number of returned time series on a per-point basis (considering points on the graph). This means that the total number of returned time series may exceed the requested limit. MetricsQL solve this issue with a family of topk_* and bottomk_* functions. See MetricsQL docs for details.

Answer from valyala on Stack Overflow
🌐
Robust Perception
robustperception.io › limiting-promql-resource-usage
Limiting PromQL resource usage – Robust Perception | Prometheus Monitoring Experts
The more efficient PromQL implementation actually caused a few out of memory issues, as previously some large slow queries were hitting the timeout but were now quickly eating up memory! The most recent addition was in Prometheus 2.5.0, a limit on how many samples a PromQL query can have in memory at once.
🌐
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=…
🌐
Prometheus
discuss.prometheus.io › promql
Limit the number of results in a range vector? - PromQL - Prometheus Monitoring System
August 1, 2024 - Hello, I am trying to limit the results returned in range vector. I tried using topk to limit my return but it is returning more than n specified. I found this on the web: This is because it returns top N time series independently per each timestamp on the graph. docs Here is an example of ...
🌐
GitHub
github.com › prometheus › prometheus › issues › 4414
Limit number of data points returned in a query · Issue #4414 · prometheus/prometheus
July 25, 2018 - We currently limit the number of steps to 11k, but if you query a very high cardinality metric we seem to be able OOM even a very large Prometheus. I propose we limit the number of points we return (step * timeseries). Plumbing this thro...
Author: prometheus
🌐
GitHub
github.com › prometheus › prometheus › issues › 5656
limit the number of returning time-series · Issue #5656 · prometheus/prometheus
June 12, 2019 - Proposal Use case. Why is this important? I'm proposing to add an optional "max-series" parameter to /api/v1/query_range HTTP API. This parameter, if specified, will limit the number of returning time-series. Here is the rationale: In so...
Author: prometheus
🌐
Alibaba Cloud
alibabacloud.com › help › en › sls › user-guide › overview-of-query-and-analysis-on-metric-data
SLS metric data query syntax - PromQL SQL limits - Simple Log Service - Alibaba Cloud Documentation Center
April 10, 2025 - SLS metric data query syntax - PromQL SQL limits,Simple Log Service:Simple Log Service (SLS) supports PromQL, SQL, and combined SQL+PromQL syntaxes for querying and analyzing metric data, with specific naming conventions and query limits.
🌐
GitHub
github.com › prometheus › prometheus › issues › 7485
PromQL: add `limit` keyword to reduce too much datapoint return · Issue #7485 · prometheus/prometheus
June 30, 2020 - Proposal I'm curious why no limit keyword for PromQL. It's a general grammar for other SQL-like language, i.e. select * from users limit 5. Keyword limit is a very useful feature for graph or preview data, an alternative is topk or botto...
Author: prometheus
🌐
Medium
medium.com › @yogeshkolhatkar › prometheus-querying-best-practices-a-complete-guide-a2f0b2899a93
Prometheus Querying Best Practices: A Complete Guide | by Yogesh Kolhatkar | Medium
March 22, 2025 - High-cardinality metrics increase storage and processing time. Limit the number of unique label values. Subqueries allow breaking down large queries into manageable chunks. ... Prometheus and PromQL are powerful tools for querying time-series data. Following best practices such as optimizing ...
Find elsewhere
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › functions
Query functions | Prometheus
This function has to be enabled via the feature flag --enable-feature=promql-experimental-functions.
🌐
pint
cloudflare.github.io › pint › checks › promql › range_query.html
promql/range_query | pint
This check inspects range query selectors on all queries. It will warn if a query tries to request a time range that is bigger than Prometheus retention limits.
🌐
Last9
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
September 12, 2024 - This cheat sheet collects practical PromQL snippets organized by use case — from quick incident queries to advanced aggregation, label manipulation, and capacity planning. When everything’s on fire and you need to know what’s going on ASAP: sum(rate(http_requests_total[5m])) by (status_code) This gives you a quick snapshot of HTTP requests broken down by status code. ... This limits the result to the top 5 status codes, reducing noise in busy environments.
🌐
New Relic
docs.newrelic.com › docs › infrastructure › prometheus-integrations › view-query-data › supported-promql-features
Supported PromQL features | New Relic Documentation
In all cases, we enforce a limit of 366 steps in range queries. We also return only 100 time series from queries by default. If you want to see more (or fewer), you need to explicitly add a topk() to your query. (Note that the topk() implementation in our PromQL-style query is different from ...
🌐
GitHub
github.com › thanos-io › promql-engine › issues › 515
Implement limitk and limit_ratio aggregations · Issue #515 · thanos-io/promql-engine
February 4, 2025 - Quoting PromQL docs here: limitk (sample n elements) limit_ratio (sample elements with approximately 𝑟 ratio if 𝑟 > 0, and the complement of such samples if 𝑟 = -(1.0 - 𝑟)) ... limitk and limit_...
Author: thanos-io
🌐
GitHub
github.com › prometheus › prometheus › issues › 4383
Proposal: Add ability to (approximately) limit memory use by queries · Issue #4383 · prometheus/prometheus
July 13, 2018 - Queries that hit the limit I've found tend to do so quite quickly, at which point their resources are released anyway so even with a theoretical upper-bound of 100GB total query memory configured (20x max connections, 5GB max query size) we ...
Author: prometheus
🌐
Sysdig Docs
docs.sysdig.com › en › limit-prometheus-metric-collection.html
Collect Prometheus Metrics | Sysdig Docs
May 16, 2020 - The time series from all the endpoints are sent to the backend in order to prevent data integrity issues. Therefore, if total number of time series from a particular endpoint is greater than the maximum allowed limit by any agent, the time series from that endpoint will be dropped irrespective of whether dynamic sampling is turned on or not.
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › operators
Operators | Prometheus
limit_ratio(r, v) (sample a pseudo-random ratio r of elements, experimental, must be enabled with --enable-feature=promql-experimental-functions)
🌐
Medium
valyala.medium.com › how-to-optimize-promql-and-metricsql-queries-85a1b75bf986
How to optimize PromQL in Prometheus | Medium
October 29, 2021 - Usually the number of processed raw samples matches the number of selected raw samples. But sometimes the number of processed raw samples may significantly exceed the number of selected raw samples. PromQL and MetricsQL can process the same raw sample multiple times if the following conditions are met:
🌐
Google Groups
groups.google.com › g › prometheus-users › c › Evum5I8ARDs
Calculate Max over time on Sum function
July 13, 2017 - PromQL currently only supports constructing a range vector from a time series, but not from the result of an expression. Recording a new time series is a workaround that for now. Here is the longstanding issue for that limitation: https://github.com/prometheus/prometheus/issues/1227