Skip to content

Commit

Permalink
Replace the filter and aggregate query in stats call with subqueries …
Browse files Browse the repository at this point in the history
…to increase performance on larger datasets.

reformat sql to match existing style
  • Loading branch information
shingler committed Jun 2, 2024
1 parent 4892fdf commit 1a45f28
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/Minion/Backend/Pg.pm
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,17 @@ sub stats {
my $self = shift;

my $stats = $self->pg->db->query(qq{
select
(select count(*) from minion_jobs mj where state = 'inactive' and (expires IS NULL OR expires > NOW())) as inactive_jobs,
(select count(*) from minion_jobs mj where state = 'active') as active_jobs,
(select count(*) from minion_jobs mj where state = 'failed') as failed_jobs,
(select count(*) from minion_jobs mj where state = 'finished') as finished_jobs,
(select count(*) from minion_jobs mj where state = 'inactive' and delayed > NOW()) as delayed_jobs,
(select COUNT(*) from minion_locks where expires > NOW()) as active_locks,
(select count(DISTINCT worker) from minion_jobs mj where state = 'active') as active_workers,
(select CASE WHEN is_called THEN last_value ELSE 0 END FROM minion_jobs_id_seq) as enqueued_jobs,
(select COUNT(*) from minion_workers) as workers,
EXTRACT(EPOCH FROM NOW() - PG_POSTMASTER_START_TIME()) as uptime
SELECT
(SELECT COUNT(*) FROM minion_jobs WHERE state = 'inactive' AND (expires IS NULL OR expires > NOW())) AS inactive_jobs,
(SELECT COUNT(*) FROM minion_jobs WHERE state = 'active') AS active_jobs,
(SELECT COUNT(*) FROM minion_jobs WHERE state = 'failed') AS failed_jobs,
(SELECT COUNT(*) FROM minion_jobs WHERE state = 'finished') AS finished_jobs,
(SELECT COUNT(*) FROM minion_jobs WHERE state = 'inactive' AND delayed > NOW()) AS delayed_jobs,
(SELECT COUNT(*) FROM minion_locks WHERE expires > NOW()) AS active_locks,
(SELECT COUNT(DISTINCT worker) FROM minion_jobs mj WHERE state = 'active') AS active_workers,
(SELECT CASE WHEN is_called THEN last_value ELSE 0 END FROM minion_jobs_id_seq) AS enqueued_jobs,
(SELECT COUNT(*) FROM minion_workers) AS workers,
EXTRACT(EPOCH FROM NOW() - PG_POSTMASTER_START_TIME()) AS uptime
})->hash;
$stats->{inactive_workers} = $stats->{workers} - $stats->{active_workers};

Expand Down

0 comments on commit 1a45f28

Please sign in to comment.