Skip to content

Commit

Permalink
Merge pull request #217 from digirati-co-uk/feature/further-index
Browse files Browse the repository at this point in the history
Query performance
  • Loading branch information
stephenwf authored Nov 13, 2023
2 parents 8f62c36 + 295cce6 commit b766fa0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<!--
### Added
Expand All @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security
-->

## [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
Expand Down
6 changes: 6 additions & 0 deletions migrations/2023-11-13T11-03.further-index.sql
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 3 additions & 0 deletions migrations/down/2023-11-13T11-03.further-index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--further-index (down)
drop index if exists tasks_created_at_index;
drop index if exists tasks_context_index;
13 changes: 9 additions & 4 deletions src/routes/get-all-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down

0 comments on commit b766fa0

Please sign in to comment.