Merge pull request #3566 from omnivore-app/perf/optimize-slow-query

perf/optimize slow query
This commit is contained in:
Hongbo Wu 2024-02-23 12:53:05 +08:00 committed by GitHub
commit 61e9ab3a17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 48 additions and 7 deletions

View file

@ -16,7 +16,7 @@ import {
libraryItemToArticleSavingRequest,
} from '../utils/helpers'
import { logger } from '../utils/logger'
import { countByCreatedAt, createOrUpdateLibraryItem } from './library_item'
import { countBySavedAt, createOrUpdateLibraryItem } from './library_item'
interface PageSaveRequest {
user: User
@ -38,12 +38,12 @@ const SAVING_CONTENT = 'Your link is being saved...'
const isPrivateIP = privateIpLib.default
// 5 articles added in the last minute: use low queue
// 5 items saved in the last minute: use low queue
// default: use normal queue
const getPriorityByRateLimit = async (
userId: string
): Promise<'low' | 'high'> => {
const count = await countByCreatedAt(userId, new Date(Date.now() - 60 * 1000))
const count = await countBySavedAt(userId, new Date(Date.now() - 60 * 1000))
return count >= 5 ? 'low' : 'high'
}

View file

@ -1004,7 +1004,7 @@ export const findLibraryItemsByPrefix = async (
)
}
export const countByCreatedAt = async (
export const countBySavedAt = async (
userId: string,
startDate = new Date(0),
endDate = new Date()
@ -1014,7 +1014,7 @@ export const countByCreatedAt = async (
tx
.createQueryBuilder(LibraryItem, 'library_item')
.where('library_item.user_id = :userId', { userId })
.andWhere('library_item.created_at between :startDate and :endDate', {
.andWhere('library_item.saved_at between :startDate and :endDate', {
startDate,
endDate,
})

View file

@ -40,8 +40,7 @@ export const updateContentForFileItem = async (msg: UpdateContentMessage) => {
.withRepository(libraryItemRepository)
.createQueryBuilder('item')
.innerJoinAndSelect('item.uploadFile', 'file')
.where('item.user = :userId', { userId: uploadFile.user.id })
.andWhere('file.id = :fileId', { fileId })
.where('file.id = :fileId', { fileId })
.getOne(),
undefined,
uploadFile.user.id

View file

@ -0,0 +1,5 @@
-- Type: DO
-- Name: library_item_user_id_state_index
-- Description: Create an index on omnivore.library_item table for querying by user_id and state
CREATE INDEX CONCURRENTLY IF NOT EXISTS library_item_user_id_state_idx ON omnivore.library_item (user_id, state);

View file

@ -0,0 +1,9 @@
-- Type: UNDO
-- Name: library_item_user_id_state_index
-- Description: Create an index on omnivore.library_item table for querying by user_id and state
BEGIN;
DROP INDEX IF EXISTS omnivore.library_item_user_id_state_idx;
COMMIT;

View file

@ -0,0 +1,5 @@
-- Type: DO
-- Name: api_key_index
-- Description: Create an index for checking key in api_key table
CREATE INDEX CONCURRENTLY IF NOT EXISTS api_key_key_idx ON omnivore.api_key (key);

View file

@ -0,0 +1,9 @@
-- Type: UNDO
-- Name: api_key_index
-- Description: Create an index for checking key in api_key table
BEGIN;
DROP INDEX IF EXISTS omnivore.api_key_key_idx;
COMMIT;

View file

@ -0,0 +1,5 @@
-- Type: DO
-- Name: library_item_file_id_index
-- Description: create an index for upload_file_id column on library_item table
CREATE INDEX CONCURRENTLY IF NOT EXISTS library_item_upload_file_id_idx ON omnivore.library_item (upload_file_id);

View file

@ -0,0 +1,9 @@
-- Type: UNDO
-- Name: library_item_file_id_index
-- Description: create an index for upload_file_id column on library_item table
BEGIN;
DROP INDEX IF EXISTS omnivore.library_item_upload_file_id_idx;
COMMIT;