Skip to content

Conversation

Jamesbarford
Copy link
Contributor

@Jamesbarford Jamesbarford commented Sep 15, 2025

Context

We wanted to see the EXPLAIN ANALYSE for' completed, in_progress and dequeue queries;

I've tested this with the following in the database;

  • 91,303 requests
  • 70,001 artifacts
  • 40,047 errors
  • 70,122 jobs

For small numbers of items it is always faster to use a sequence scan as it avoids loading an index. Postgres seems to be smart enough to know when to switch from a sequence scan to an index scan depending on how many entries there are.

The below indexes are created off the basis of wanting to avoid any sequence scan where possible.

Completed

This needs an index on the column completed_at otherwise we get a full sequence scan on the completed_at column c799d67. Similarly the error table needs an index on aid to prevent a full sequence scan of the error table when we do a join on the artifact.id 6a6dadb.

BEFORE image
AFTER image

In Progress

This query needs no new indexes. We do a full sequence scan of the job queue table. I'm not sure yet if this is avoidable or acceptable. I'm also not exactly sure what we are wanting from the query. Below is the current EXPLAIN ANALYSE output

BEFORE - EXPLAIN ANALYSE output image

If the query is to get the jobs of currently in_progress requests I have updated the query in this commit; fe91155

Which produces a more optimised;

AFTER - EXPLAIN ANALYSE output image

Dequeue

This query needs no new indexes.

EXPLAIN ANALYSE output image

@Jamesbarford Jamesbarford force-pushed the fix/database-indexes branch 2 times, most recently from abbeaba to 131541d Compare September 16, 2025 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant