mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Merge pull request #3566 from omnivore-app/perf/optimize-slow-query
perf/optimize slow query
This commit is contained in:
commit
61e9ab3a17
9 changed files with 48 additions and 7 deletions
|
|
@ -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'
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
5
packages/db/migrations/0162.do.library_item_user_id_state_index.sql
Executable file
5
packages/db/migrations/0162.do.library_item_user_id_state_index.sql
Executable 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);
|
||||
9
packages/db/migrations/0162.undo.library_item_user_id_state_index.sql
Executable file
9
packages/db/migrations/0162.undo.library_item_user_id_state_index.sql
Executable 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;
|
||||
5
packages/db/migrations/0163.do.api_key_index.sql
Executable file
5
packages/db/migrations/0163.do.api_key_index.sql
Executable 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);
|
||||
9
packages/db/migrations/0163.undo.api_key_index.sql
Executable file
9
packages/db/migrations/0163.undo.api_key_index.sql
Executable 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;
|
||||
5
packages/db/migrations/0164.do.library_item_file_id_index.sql
Executable file
5
packages/db/migrations/0164.do.library_item_file_id_index.sql
Executable 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);
|
||||
9
packages/db/migrations/0164.undo.library_item_file_id_index.sql
Executable file
9
packages/db/migrations/0164.undo.library_item_file_id_index.sql
Executable 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;
|
||||
Loading…
Reference in a new issue