diff --git a/fastapi_book_server/app/alembic/versions/08193b547a80_.py b/fastapi_book_server/app/alembic/versions/08193b547a80_.py index 29762c5..962287a 100644 --- a/fastapi_book_server/app/alembic/versions/08193b547a80_.py +++ b/fastapi_book_server/app/alembic/versions/08193b547a80_.py @@ -25,6 +25,8 @@ def upgrade(): op.create_index(op.f('tgrm_authors_lfm'), 'authors', [sa.text("(last_name || ' ' || first_name || ' ' || middle_name)")] ,postgresql_using='gin', postgresql_ops={'description': 'gin_trgm_ops'}) op.create_index(op.f('tgrm_authors_lf'), 'authors', [sa.text("(last_name || ' ' || first_name)")] ,postgresql_using='gin', postgresql_ops={'description': 'gin_trgm_ops'}) op.create_index(op.f('tgrm_authors_l'), 'authors', ['last_name'] ,postgresql_using='gin', postgresql_ops={'description': 'gin_trgm_ops'}) + op.create_index(op.f('book_authors_book'), 'book_authors', ['book'], unique=False, postgresql_using='btree') + op.create_index(op.f('book_authors_author'), 'book_authors', ['author'], unique=False, postgresql_using='btree') # ### end Alembic commands ### @@ -37,4 +39,6 @@ def downgrade(): op.drop_index(op.f('tgrm_authors_lfm'), table_name='books') op.drop_index(op.f('tgrm_authors_lf'), table_name='books') op.drop_index(op.f('tgrm_authors_l'), table_name='books') + op.drop_index(op.f('book_authors_book'), table_name='book_authors') + op.drop_index(op.f('book_authors_author'), table_name='book_authors') # ### end Alembic commands ### diff --git a/fastapi_book_server/app/services/author.py b/fastapi_book_server/app/services/author.py index 70a1e00..308eb30 100644 --- a/fastapi_book_server/app/services/author.py +++ b/fastapi_book_server/app/services/author.py @@ -15,8 +15,8 @@ SELECT ARRAY( ) as sml, ( SELECT count(*) FROM book_authors - LEFT JOIN books ON books.id = book - WHERE author = authors.id AND books.is_deleted = 'f' + LEFT JOIN books ON (books.id = book AND books.is_deleted = 'f') + WHERE author = authors.id ) as books_count FROM authors WHERE ( @@ -26,8 +26,8 @@ SELECT ARRAY( ) AND EXISTS ( SELECT * FROM book_authors - LEFT JOIN books ON books.id = book - WHERE author = authors.id AND books.is_deleted = 'f' + LEFT JOIN books ON (books.id = book AND books.is_deleted = 'f') + WHERE author = authors.id ) ) SELECT fauthors.id FROM filtered_authors as fauthors diff --git a/fastapi_book_server/app/services/book.py b/fastapi_book_server/app/services/book.py index 3299f36..a73052b 100644 --- a/fastapi_book_server/app/services/book.py +++ b/fastapi_book_server/app/services/book.py @@ -12,7 +12,7 @@ GET_OBJECT_IDS_QUERY = """ SELECT ARRAY( WITH filtered_books AS ( SELECT id, similarity(title, :query) as sml FROM books - WHERE books.title % :query + WHERE books.title % :query AND books.is_deleted = 'f' ) SELECT fbooks.id FROM filtered_books as fbooks ORDER BY fbooks.sml DESC, fbooks.id diff --git a/fastapi_book_server/app/services/sequence.py b/fastapi_book_server/app/services/sequence.py index 8afb8b0..c7b5f2e 100644 --- a/fastapi_book_server/app/services/sequence.py +++ b/fastapi_book_server/app/services/sequence.py @@ -11,15 +11,15 @@ SELECT ARRAY ( similarity(name, :query) as sml, ( SELECT count(*) FROM book_sequences - LEFT JOIN books ON books.id = book - WHERE sequence = sequences.id AND books.is_deleted = 'f' + LEFT JOIN books ON (books.id = book AND books.is_deleted = 'f') + WHERE sequence = sequences.id ) as books_count FROM sequences WHERE name % :query AND EXISTS ( SELECT * FROM book_sequences - LEFT JOIN books ON books.id = book - WHERE sequence = sequences.id AND books.is_deleted = 'f' + LEFT JOIN books ON (books.id = book AND books.is_deleted = 'f') + WHERE sequence = sequences.id ) ) SELECT fsequences.id FROM filtered_sequences as fsequences