diff --git a/api/v1_users_comments.go b/api/v1_users_comments.go index 8faa8109..c81c68fa 100644 --- a/api/v1_users_comments.go +++ b/api/v1_users_comments.go @@ -8,13 +8,12 @@ import ( func (app *ApiServer) v1UsersComments(c *fiber.Ctx) error { sql := ` - SELECT comment_id as id - FROM comments - LEFT JOIN comment_threads USING (comment_id) - WHERE user_id = @user_id - AND entity_type = 'Track' - AND comments.is_delete = false - ` + SELECT comment_id as id + FROM comments + WHERE user_id = @user_id + AND entity_type = 'Track' + AND comments.is_delete = false + ` args := pgx.NamedArgs{ "user_id": app.getUserId(c), diff --git a/ddl/migrations/0224_comments_user_track_created_at_idx.sql b/ddl/migrations/0224_comments_user_track_created_at_idx.sql new file mode 100644 index 00000000..d6b6124c --- /dev/null +++ b/ddl/migrations/0224_comments_user_track_created_at_idx.sql @@ -0,0 +1,14 @@ +-- Supports /v1/users/:id/comments pagination: +-- +-- WHERE user_id = ? +-- AND entity_type = 'Track' +-- AND is_delete = false +-- ORDER BY created_at DESC +-- +-- Without this index, the endpoint scans and sorts the full comments table for +-- every user-comments page request. +CREATE INDEX CONCURRENTLY IF NOT EXISTS comments_user_track_created_at_idx + ON public.comments USING btree (user_id, created_at DESC) + INCLUDE (comment_id) + WHERE entity_type = 'Track' + AND is_delete = false;