I was wondering how I might speed this query up with indices or something else if it's at all possible. I don't know if it's possible with my current query.
The query looks like this:
SELECT
id,
metric,
out_metric,
run_every,
last_downsampled_window,
query,
(last_updated + run_every)::bigint - $1 AS time_until_update
FROM downsamplers
WHERE worker_id = $2
ORDER BY time_until_update ASC
LIMIT 1;
Where $1
is the current unix timestamp in nanos. And $2
is just an id.
And the table schema looks like this:
CREATE TABLE downsamplers (
id serial,
metric text,
out_metric text,
run_every bigint,
last_downsampled_window bigint,
last_updated bigint NOT NULL,
worker_id int,
query jsonb
)
Basically I'm using this query to get the item from downsamplers with the earliest time_until_update.
time_until_update
aspect though.