-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(appeal): create find active grant query * feat(store): add indexes for slow queries * fix(appeal): return grant err not found * fix(appeal): use order by * chore: rename var activeGrant * fix: test * chore: add index explaination
- Loading branch information
Showing
5 changed files
with
88 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
internal/store/postgres/migrations/000014_add_indexes_to_improve_slow_queries.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DROP INDEX IF EXISTS idx_appeals_created_by_status_deleted_at_updated_at; | ||
DROP INDEX IF EXISTS idx_appeals_id_deleted_at_null; | ||
DROP INDEX IF EXISTS idx_approvers_approval_id_deleted_at_null; | ||
DROP INDEX IF EXISTS idx_grants_appeal_id_deleted_at_null; | ||
DROP INDEX IF EXISTS idx_grants_status_deleted_at_null; | ||
DROP INDEX IF EXISTS idx_resources_id_deleted_at_null; |
43 changes: 43 additions & 0 deletions
43
internal/store/postgres/migrations/000014_add_indexes_to_improve_slow_queries.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
-- This index reducing query cost from 13997.45..13997 to 17.66..17.67 | ||
-- SELECT ... | ||
-- FROM "appeals" | ||
-- LEFT JOIN "resources" "Resource" ON "appeals"."resource_id" = "Resource"."id" | ||
-- AND "Resource"."deleted_at" IS NULL | ||
-- LEFT JOIN "grants" "Grant" ON "appeals"."id" = "Grant"."appeal_id" | ||
-- AND "Grant"."deleted_at" IS NULL | ||
-- WHERE "appeals"."created_by" = '<creator>' | ||
-- AND "appeals"."status" IN (...) | ||
-- AND "appeals"."deleted_at" IS NULL | ||
-- ORDER BY ARRAY_POSITION( | ||
-- ARRAY [...], | ||
-- "appeals"."status" | ||
-- ), | ||
-- "updated_at" desc; | ||
|
||
CREATE INDEX IF NOT EXISTS idx_appeals_created_by_status_deleted_at_updated_at | ||
ON appeals (created_by, status, deleted_at, updated_at DESC) | ||
WHERE deleted_at IS NULL; | ||
|
||
-- This index reducing query cost from 0.15..8.17 to 0.00..1.02 | ||
-- Since we have a lot of queries like this due to the gorm soft delete: | ||
-- SELECT * FROM <table> WHERE id = '<uuid>' AND "deleted_at" IS NULL ORDER BY "id" LIMIT 1 | ||
CREATE INDEX IF NOT EXISTS idx_appeals_id_deleted_at_null | ||
ON appeals (id, deleted_at) | ||
WHERE deleted_at IS NULL; | ||
|
||
CREATE INDEX IF NOT EXISTS idx_approvers_approval_id_deleted_at_null | ||
ON approvers (approval_id, deleted_at) | ||
WHERE deleted_at IS NULL; | ||
|
||
CREATE INDEX IF NOT EXISTS idx_grants_appeal_id_deleted_at_null | ||
ON grants (appeal_id, deleted_at) | ||
WHERE deleted_at IS NULL; | ||
|
||
CREATE INDEX IF NOT EXISTS idx_grants_status_deleted_at_null | ||
ON grants (status, deleted_at) | ||
WHERE deleted_at IS NULL and status = 'active'; | ||
|
||
CREATE INDEX IF NOT EXISTS idx_resources_id_deleted_at_null | ||
ON resources (id, deleted_at) | ||
WHERE deleted_at IS NULL; | ||
|