Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query performance #217

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading