How metric queries work in Loki

The question is about how Loki creates metrics from logs

Let’s consider an example query.

Count_over_time({job=“mysql”}[5m])

My logs:

2025-07-09 12:00:01 content
2025-07-09 12:00:02 content
2025-07-09 12:00:03 content
2025-07-09 12:00:04 content
..
2025-07-09 12:00:99 content
2025-07-09 12:01:00 content

2025-07-09 12:01:99 content

How count_over_time calculates metrics? Is it for each log (combination of labels) counts lasting 5m? If so, does it mean that the calculation of every second in my example? What if we had billions of logs? Does it mean that such a calculation is done for every line also?

Is it possible to have metrics in deterministic intervals like:
​12:01 value
12:02 value
​…
12:59 values

If so, it would be less CPU, ram consuming for Loki.

According to the documentation:

  • count_over_time(log-range): counts the entries for each log stream within the given range.

So in your example:

count_over_time({job=“mysql”}[5m])

This means that it’ll count the number of logs that fit your filter, for every 5-minute interval, until the duration of the query.

Thank you for your replay. I think [5m] means that loki checks last 5min from current attempt, isn’t it? Are you sure this entry means that for example hour is splitted for 5min interval, so I will 12 dots?