mirror of
https://github.com/coder/coder.git
synced 2026-06-04 21:48:22 +00:00
chore: optimize workspace_latest_builds view query (#17789)
Avoids two sequential scans of massive tables (`workspace_builds`, `provisioner_jobs`) and uses index scans instead. This new view largely replicates our already optimized query `GetWorkspaces` to fetch the latest build. The original query and the new query were compared against the dogfood database to ensure they return the exact same data in the exact same order (minus the new `workspaces.deleted = false` filter to improve performance even more). The performance is massively improved even without the `workspaces.deleted = false` filter, but it was added to improve it even more. Note: these query times are probably inflated due to high database load on our dogfood environment that this intends to partially resolve. Before: 2,139ms ([explain](https://explain.dalibo.com/plan/997e4fch241b46e6)) After: 33ms ([explain](https://explain.dalibo.com/plan/c888dc223870f181)) Co-authored-by: Cian Johnston <cian@coder.com> --------- Signed-off-by: Danny Kopping <dannykopping@gmail.com> Co-authored-by: Mathias Fredriksson <mafredri@gmail.com> Co-authored-by: Danny Kopping <dannykopping@gmail.com>
This commit is contained in:
Generated
+45
-32
@@ -2024,18 +2024,52 @@ CREATE VIEW workspace_build_with_user AS
|
||||
|
||||
COMMENT ON VIEW workspace_build_with_user IS 'Joins in the username + avatar url of the initiated by user.';
|
||||
|
||||
CREATE TABLE workspaces (
|
||||
id uuid NOT NULL,
|
||||
created_at timestamp with time zone NOT NULL,
|
||||
updated_at timestamp with time zone NOT NULL,
|
||||
owner_id uuid NOT NULL,
|
||||
organization_id uuid NOT NULL,
|
||||
template_id uuid NOT NULL,
|
||||
deleted boolean DEFAULT false NOT NULL,
|
||||
name character varying(64) NOT NULL,
|
||||
autostart_schedule text,
|
||||
ttl bigint,
|
||||
last_used_at timestamp with time zone DEFAULT '0001-01-01 00:00:00+00'::timestamp with time zone NOT NULL,
|
||||
dormant_at timestamp with time zone,
|
||||
deleting_at timestamp with time zone,
|
||||
automatic_updates automatic_updates DEFAULT 'never'::automatic_updates NOT NULL,
|
||||
favorite boolean DEFAULT false NOT NULL,
|
||||
next_start_at timestamp with time zone
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN workspaces.favorite IS 'Favorite is true if the workspace owner has favorited the workspace.';
|
||||
|
||||
CREATE VIEW workspace_latest_builds AS
|
||||
SELECT DISTINCT ON (wb.workspace_id) wb.id,
|
||||
wb.workspace_id,
|
||||
wb.template_version_id,
|
||||
wb.job_id,
|
||||
wb.template_version_preset_id,
|
||||
wb.transition,
|
||||
wb.created_at,
|
||||
pj.job_status
|
||||
FROM (workspace_builds wb
|
||||
JOIN provisioner_jobs pj ON ((wb.job_id = pj.id)))
|
||||
ORDER BY wb.workspace_id, wb.build_number DESC;
|
||||
SELECT latest_build.id,
|
||||
latest_build.workspace_id,
|
||||
latest_build.template_version_id,
|
||||
latest_build.job_id,
|
||||
latest_build.template_version_preset_id,
|
||||
latest_build.transition,
|
||||
latest_build.created_at,
|
||||
latest_build.job_status
|
||||
FROM (workspaces
|
||||
LEFT JOIN LATERAL ( SELECT workspace_builds.id,
|
||||
workspace_builds.workspace_id,
|
||||
workspace_builds.template_version_id,
|
||||
workspace_builds.job_id,
|
||||
workspace_builds.template_version_preset_id,
|
||||
workspace_builds.transition,
|
||||
workspace_builds.created_at,
|
||||
provisioner_jobs.job_status
|
||||
FROM (workspace_builds
|
||||
JOIN provisioner_jobs ON ((provisioner_jobs.id = workspace_builds.job_id)))
|
||||
WHERE (workspace_builds.workspace_id = workspaces.id)
|
||||
ORDER BY workspace_builds.build_number DESC
|
||||
LIMIT 1) latest_build ON (true))
|
||||
WHERE (workspaces.deleted = false)
|
||||
ORDER BY workspaces.id;
|
||||
|
||||
CREATE TABLE workspace_modules (
|
||||
id uuid NOT NULL,
|
||||
@@ -2072,27 +2106,6 @@ CREATE TABLE workspace_resources (
|
||||
module_path text
|
||||
);
|
||||
|
||||
CREATE TABLE workspaces (
|
||||
id uuid NOT NULL,
|
||||
created_at timestamp with time zone NOT NULL,
|
||||
updated_at timestamp with time zone NOT NULL,
|
||||
owner_id uuid NOT NULL,
|
||||
organization_id uuid NOT NULL,
|
||||
template_id uuid NOT NULL,
|
||||
deleted boolean DEFAULT false NOT NULL,
|
||||
name character varying(64) NOT NULL,
|
||||
autostart_schedule text,
|
||||
ttl bigint,
|
||||
last_used_at timestamp with time zone DEFAULT '0001-01-01 00:00:00+00'::timestamp with time zone NOT NULL,
|
||||
dormant_at timestamp with time zone,
|
||||
deleting_at timestamp with time zone,
|
||||
automatic_updates automatic_updates DEFAULT 'never'::automatic_updates NOT NULL,
|
||||
favorite boolean DEFAULT false NOT NULL,
|
||||
next_start_at timestamp with time zone
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN workspaces.favorite IS 'Favorite is true if the workspace owner has favorited the workspace.';
|
||||
|
||||
CREATE VIEW workspace_prebuilds AS
|
||||
WITH all_prebuilds AS (
|
||||
SELECT w.id,
|
||||
|
||||
Reference in New Issue
Block a user