From fcc67acdefddd490c5f29a94692dea1bdd529bb7 Mon Sep 17 00:00:00 2001 From: Stephen Fraser Date: Mon, 13 Nov 2023 11:07:17 +0000 Subject: [PATCH 1/2] Performance improvements for task queries --- CHANGELOG.md | 8 +++++++- migrations/2023-11-13T11-03.further-index.sql | 6 ++++++ migrations/down/2023-11-13T11-03.further-index.sql | 1 + src/routes/get-all-tasks.ts | 13 +++++++++---- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 migrations/2023-11-13T11-03.further-index.sql create mode 100644 migrations/down/2023-11-13T11-03.further-index.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index 937806a..d379382 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.3...main) +## [Unreleased](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.5...main) +## [v1.1.5](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.4...v1.1.5) + +### Fixed +- Performance improvements for task queries +- Removed join when not required + ## [v1.1.4](https://github.com/digirati-co-uk/tasks-api/compare/v1.1.3...v1.1.4) ### Fixed diff --git a/migrations/2023-11-13T11-03.further-index.sql b/migrations/2023-11-13T11-03.further-index.sql new file mode 100644 index 0000000..61d8831 --- /dev/null +++ b/migrations/2023-11-13T11-03.further-index.sql @@ -0,0 +1,6 @@ +--further-index (up) +create index tasks_created_at_index + on tasks (created_at); + +create index tasks_context_index + on tasks (context); diff --git a/migrations/down/2023-11-13T11-03.further-index.sql b/migrations/down/2023-11-13T11-03.further-index.sql new file mode 100644 index 0000000..1320c97 --- /dev/null +++ b/migrations/down/2023-11-13T11-03.further-index.sql @@ -0,0 +1 @@ +--further-index (down) diff --git a/src/routes/get-all-tasks.ts b/src/routes/get-all-tasks.ts index bb283cd..5e5454e 100644 --- a/src/routes/get-all-tasks.ts +++ b/src/routes/get-all-tasks.ts @@ -81,6 +81,9 @@ export const getAllTasks: RouteMiddleware = async (context) => { ? sql`` : sql`and t.parent_task is null`; const assigneeId = context.query.assignee; + + const joinRequired = !(isAdmin || canCreate) && !assigneeId; + const userExclusion = isAdmin || canCreate ? assigneeId @@ -131,10 +134,12 @@ export const getAllTasks: RouteMiddleware = async (context) => { ? sql`and t.created_at > (now() - ${context.query.created_date_interval}::interval)` : sql``; + const dtJoin = joinRequired ? sql`left join tasks dt on t.delegated_task = dt.id` : sql``; + try { const countQuery = sql<{ total_items: number }>` - select COUNT(*) as total_items from tasks t - left join tasks dt on t.delegated_task = dt.id + select COUNT(*) as total_items from tasks t + ${dtJoin} where t.context ?& ${sql.array(context.state.jwt.context, 'text')} ${subtaskExclusion} ${userExclusion} @@ -153,8 +158,8 @@ export const getAllTasks: RouteMiddleware = async (context) => { `; const query = sql` SELECT t.id, t.name, t.status, t.status_text, t.metadata, t.type ${detailedFields} - FROM tasks t - LEFT JOIN tasks dt on t.delegated_task = dt.id + FROM tasks t + ${dtJoin} WHERE t.context ?& ${sql.array(context.state.jwt.context, 'text')} ${subtaskExclusion} ${userExclusion} From 295cce699da05e028c58d8f0a86dce8dba467ec0 Mon Sep 17 00:00:00 2001 From: Stephen Fraser Date: Mon, 13 Nov 2023 11:07:50 +0000 Subject: [PATCH 2/2] Down migrations --- migrations/down/2023-11-13T11-03.further-index.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/migrations/down/2023-11-13T11-03.further-index.sql b/migrations/down/2023-11-13T11-03.further-index.sql index 1320c97..e9aa0a6 100644 --- a/migrations/down/2023-11-13T11-03.further-index.sql +++ b/migrations/down/2023-11-13T11-03.further-index.sql @@ -1 +1,3 @@ --further-index (down) +drop index if exists tasks_created_at_index; +drop index if exists tasks_context_index;