add index for sorting by saved_at

This commit is contained in:
Hongbo Wu 2024-01-10 17:09:15 +08:00
parent ff83147c09
commit d9d800cfc4
3 changed files with 21 additions and 2 deletions

View file

@ -93,6 +93,7 @@ export enum SortOrder {
export interface Sort {
by: string
order?: SortOrder
nulls?: 'NULLS FIRST' | 'NULLS LAST'
}
interface Select {
@ -332,8 +333,10 @@ export const buildQuery = (
const order =
sortOrder === 'asc' ? SortOrder.ASCENDING : SortOrder.DESCENDING
const nulls =
order === SortOrder.ASCENDING ? 'NULLS FIRST' : 'NULLS LAST'
orders.push({ by: `library_item.${column}`, order })
orders.push({ by: `library_item.${column}`, order, nulls })
return null
}
case 'has':
@ -613,12 +616,13 @@ export const searchLibraryItems = async (
orders.push({
by: 'library_item.saved_at',
order: SortOrder.DESCENDING,
nulls: 'NULLS LAST',
})
}
// add order by
orders.forEach((order) => {
queryBuilder.addOrderBy(order.by, order.order, 'NULLS LAST')
queryBuilder.addOrderBy(order.by, order.order, order.nulls)
})
const libraryItems = await queryBuilder.skip(from).take(size).getMany()

View file

@ -0,0 +1,6 @@
-- Type: DO
-- Name: library_item_user_id_saved_at_idx
-- Description: Add library_item_user_id_saved_at_idx index on library_item table for user_id and saved_at
-- create index for sorting concurrently to avoid locking
CREATE INDEX CONCURRENTLY IF NOT EXISTS library_item_user_id_saved_at_idx ON omnivore.library_item (user_id, saved_at DESC NULLS LAST);

View file

@ -0,0 +1,9 @@
-- Type: UNDO
-- Name: library_item_user_id_saved_at_idx
-- Description: Add library_item_user_id_saved_at_idx index on library_item table for user_id and saved_at
BEGIN;
DROP INDEX IF EXISTS omnivore.library_item_user_id_saved_at_idx;
COMMIT;