[Feat] 하이브리드 방식 인덱스 개선 및 비교#35
Merged
1024andrew merged 3 commits intodevfrom May 5, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a search_tsvector column to the regulation_chunk table to optimize hybrid search performance by pre-calculating search vectors. It includes database migrations, model updates, and logic in the repository to utilize the new column. Feedback focuses on optimizing the data population process to reduce database round trips, improving migration safety for large datasets by using concurrent index creation and batching updates, and eliminating redundant embedding API calls in the chat service logic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
유형
작업 내용
하이브리드 검색의 키워드 검색 성능 개선을 위해
regulation_chunk에 검색용tsvector컬럼을 추가하고, 해당 컬럼에 GIN 인덱스를 적용했습니다.기존에는 검색 시점마다
chunk_text,regulation_document.content,keywords를 조합해to_tsvector(...)를 반복 계산했는데, 이제 검색 대상 텍스트를 미리search_tsvector에 저장하고 인덱스를 통해 조회하도록 변경했습니다.변경사항은 노션 백엔드 - 디비 문서에 정리했습니다.
또한 개선 전후 성능을 비교할 수 있도록 하이브리드 검색 벤치마크 스크립트를 추가했습니다.
변경 사항 (있다면)
regulation_chunk.search_tsvector컬럼 추가idx_regulation_chunk_search_tsvectorGIN 인덱스 추가regulation_chunk데이터에 대한search_tsvectorbackfill migration 추가to_tsvector(...)를rc.search_tsvector사용으로 변경search_tsvector가 갱신되도록 처리성능 비교
이번 변경은 전체 챗봇 응답 시간이 아니라 하이브리드 검색 DB 조회 구간을 최적화한 변경입니다. 따라서 성능 비교는 repository 함수 실행 시간과 PostgreSQL EXPLAIN 실행계획을 기준으로 진행했습니다.
동일한 20개 질문 케이스에 대해 개선 전후 하이브리드 검색 benchmark를 실행했습니다.
측정 조건:
결과 요약:
regulation_chunk_id변경: 0건실행계획 변화:
Seq Scan중심idx_regulation_chunk_search_tsvector사용, keyword branch에서Bitmap Index Scan확인리뷰 포인트
search_tsvector컬럼 추가, backfill, GIN 인덱스 생성 순서가 적절한지rc.search_tsvector를 사용하도록 변경됐는지search_tsvector갱신 로직이 누락 없이 동작하는지CREATE INDEX사용이 현재 데이터 규모와 운영 방식에 적절한지리뷰 반영 요약
search_tsvector생성 방식 개선search_tsvector를 채웠습니다.chunk_text,document.content,keywords를 가지고 있으므로 INSERT 시func.to_tsvector(...)를 함께 저장하도록 변경했습니다.기존 데이터 backfill 방식 개선
regulation_chunk_id기준 batch 단위로 나눠 갱신하도록 변경했습니다.keyword 검색 텍스트 정리
rc.keywords::text대신 keyword 배열 원소를 공백으로 합쳐 검색 벡터에 포함하도록 변경했습니다.확장 쿼리 embedding 재사용
rewritten_queryembedding이 있으면 fallback query expansion 검색에서 재사용하도록 변경했습니다.테스트
/docs수동 확인pytest실행스크린샷