speed up scanning

This commit is contained in:
aecsocket
2026-08-03 17:53:04 +01:00
parent c2705a0975
commit 5bb8b7368c
4 changed files with 99 additions and 88 deletions
@@ -0,0 +1,60 @@
{
"db_name": "PostgreSQL",
"query": "\n WITH project_ids AS (\n SELECT unnest($1::bigint[]) AS project_id\n ),\n detail_states AS (\n SELECT\n p.project_id,\n COALESCE(\n BOOL_OR(\n didws.status = 'pending'\n AND dri.issue_type != $3\n ),\n FALSE\n ) AS has_pending_detail,\n COALESCE(\n BOOL_OR(\n didws.status = 'unsafe'\n AND dri.issue_type != $3\n ),\n FALSE\n ) AS has_unsafe_detail,\n COALESCE(\n BOOL_OR(\n didws.status = 'pending'\n AND dri.issue_type = $3\n ),\n FALSE\n ) AS has_dummy\n FROM project_ids p\n LEFT JOIN delphi_issue_details_with_statuses didws\n ON didws.project_id = p.project_id\n LEFT JOIN delphi_report_issues dri ON dri.id = didws.issue_id\n GROUP BY p.project_id\n )\n SELECT\n p.project_id AS \"project_id!: DBProjectId\",\n p.has_pending_detail AS \"has_pending_detail!\",\n p.has_unsafe_detail AS \"has_unsafe_detail!\",\n p.has_dummy AS \"has_dummy!\",\n (\n SELECT t.id\n FROM threads t\n WHERE t.mod_id = p.project_id\n ORDER BY t.id\n LIMIT 1\n ) AS \"thread_id: DBThreadId\",\n (\n SELECT tm.body->>'type'\n FROM threads t\n INNER JOIN threads_messages tm ON tm.thread_id = t.id\n WHERE\n t.mod_id = p.project_id\n AND tm.body->>'type' = ANY($2::text[])\n ORDER BY tm.created DESC, tm.id DESC\n LIMIT 1\n ) AS \"last_tech_review_message_type\",\n (\n SELECT dr.id\n FROM versions v\n INNER JOIN files f ON f.version_id = v.id\n INNER JOIN delphi_reports dr ON dr.file_id = f.id\n WHERE v.mod_id = p.project_id\n ORDER BY dr.created DESC, dr.id DESC\n LIMIT 1\n ) AS \"report_id: DelphiReportId\"\n FROM detail_states p\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "project_id!: DBProjectId",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "has_pending_detail!",
"type_info": "Bool"
},
{
"ordinal": 2,
"name": "has_unsafe_detail!",
"type_info": "Bool"
},
{
"ordinal": 3,
"name": "has_dummy!",
"type_info": "Bool"
},
{
"ordinal": 4,
"name": "thread_id: DBThreadId",
"type_info": "Int8"
},
{
"ordinal": 5,
"name": "last_tech_review_message_type",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "report_id: DelphiReportId",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Int8Array",
"TextArray",
"Text"
]
},
"nullable": [
null,
null,
null,
null,
null,
null,
null
]
},
"hash": "6241bc72a80de01fd8e5506442da48057c24dece008dbf2c00c6ec135d8cef41"
}
@@ -1,60 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n WITH project_ids AS (\n SELECT unnest($1::bigint[]) AS project_id\n )\n SELECT\n p.project_id AS \"project_id!: DBProjectId\",\n EXISTS(\n SELECT 1\n FROM delphi_issue_details_with_statuses didws\n INNER JOIN delphi_report_issues dri ON dri.id = didws.issue_id\n WHERE\n didws.project_id = p.project_id\n AND didws.status = 'pending'\n AND dri.issue_type != $3\n ) AS \"has_pending_detail!\",\n EXISTS(\n SELECT 1\n FROM delphi_issue_details_with_statuses didws\n INNER JOIN delphi_report_issues dri ON dri.id = didws.issue_id\n WHERE\n didws.project_id = p.project_id\n AND didws.status = 'unsafe'\n AND dri.issue_type != $3\n ) AS \"has_unsafe_detail!\",\n EXISTS(\n SELECT 1\n FROM delphi_issue_details_with_statuses didws\n INNER JOIN delphi_report_issues dri ON dri.id = didws.issue_id\n WHERE\n didws.project_id = p.project_id\n AND didws.status = 'pending'\n AND dri.issue_type = $3\n ) AS \"has_dummy!\",\n (\n SELECT t.id\n FROM threads t\n WHERE t.mod_id = p.project_id\n ORDER BY t.id\n LIMIT 1\n ) AS \"thread_id: DBThreadId\",\n (\n SELECT tm.body->>'type'\n FROM threads t\n INNER JOIN threads_messages tm ON tm.thread_id = t.id\n WHERE\n t.mod_id = p.project_id\n AND tm.body->>'type' = ANY($2::text[])\n ORDER BY tm.created DESC, tm.id DESC\n LIMIT 1\n ) AS \"last_tech_review_message_type\",\n (\n SELECT dr.id\n FROM versions v\n INNER JOIN files f ON f.version_id = v.id\n INNER JOIN delphi_reports dr ON dr.file_id = f.id\n WHERE v.mod_id = p.project_id\n ORDER BY dr.created DESC, dr.id DESC\n LIMIT 1\n ) AS \"report_id: DelphiReportId\"\n FROM project_ids p\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "project_id!: DBProjectId",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "has_pending_detail!",
"type_info": "Bool"
},
{
"ordinal": 2,
"name": "has_unsafe_detail!",
"type_info": "Bool"
},
{
"ordinal": 3,
"name": "has_dummy!",
"type_info": "Bool"
},
{
"ordinal": 4,
"name": "thread_id: DBThreadId",
"type_info": "Int8"
},
{
"ordinal": 5,
"name": "last_tech_review_message_type",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "report_id: DelphiReportId",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Int8Array",
"TextArray",
"Text"
]
},
"nullable": [
null,
null,
null,
null,
null,
null,
null
]
},
"hash": "bb94018c84f3809b9ad89f35ff08501addc8f203a0a152346aaebd32db52be30"
}
@@ -0,0 +1,5 @@
CREATE INDEX delphi_report_issue_details_issue_id
ON delphi_report_issue_details (issue_id);
CREATE INDEX threads_messages_thread_id_created_id
ON threads_messages (thread_id, created DESC, id DESC);
@@ -88,36 +88,42 @@ pub async fn sync_project_tech_review_state(
r#"
WITH project_ids AS (
SELECT unnest($1::bigint[]) AS project_id
),
detail_states AS (
SELECT
p.project_id,
COALESCE(
BOOL_OR(
didws.status = 'pending'
AND dri.issue_type != $3
),
FALSE
) AS has_pending_detail,
COALESCE(
BOOL_OR(
didws.status = 'unsafe'
AND dri.issue_type != $3
),
FALSE
) AS has_unsafe_detail,
COALESCE(
BOOL_OR(
didws.status = 'pending'
AND dri.issue_type = $3
),
FALSE
) AS has_dummy
FROM project_ids p
LEFT JOIN delphi_issue_details_with_statuses didws
ON didws.project_id = p.project_id
LEFT JOIN delphi_report_issues dri ON dri.id = didws.issue_id
GROUP BY p.project_id
)
SELECT
p.project_id AS "project_id!: DBProjectId",
EXISTS(
SELECT 1
FROM delphi_issue_details_with_statuses didws
INNER JOIN delphi_report_issues dri ON dri.id = didws.issue_id
WHERE
didws.project_id = p.project_id
AND didws.status = 'pending'
AND dri.issue_type != $3
) AS "has_pending_detail!",
EXISTS(
SELECT 1
FROM delphi_issue_details_with_statuses didws
INNER JOIN delphi_report_issues dri ON dri.id = didws.issue_id
WHERE
didws.project_id = p.project_id
AND didws.status = 'unsafe'
AND dri.issue_type != $3
) AS "has_unsafe_detail!",
EXISTS(
SELECT 1
FROM delphi_issue_details_with_statuses didws
INNER JOIN delphi_report_issues dri ON dri.id = didws.issue_id
WHERE
didws.project_id = p.project_id
AND didws.status = 'pending'
AND dri.issue_type = $3
) AS "has_dummy!",
p.has_pending_detail AS "has_pending_detail!",
p.has_unsafe_detail AS "has_unsafe_detail!",
p.has_dummy AS "has_dummy!",
(
SELECT t.id
FROM threads t
@@ -144,7 +150,7 @@ pub async fn sync_project_tech_review_state(
ORDER BY dr.created DESC, dr.id DESC
LIMIT 1
) AS "report_id: DelphiReportId"
FROM project_ids p
FROM detail_states p
"#,
&project_ids_raw,
&tech_review_message_types,