Skip to content

Commit 97ba93f

Browse files
committed
last
1 parent ec99e88 commit 97ba93f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- PostgreSQL Full-Text Search 마이그레이션
2+
3+
-- 1. 카테고리 인덱스, 최신순 정렬을 위한 인덱스 추가
4+
CREATE INDEX idx_post_category ON post(category);
5+
CREATE INDEX idx_post_created_desc ON post(created_date DESC);
6+
7+
-- 2. 기존 제목 인덱스 - 풀텍스트 서치 영향 미치지 않으므로 제거
8+
DROP INDEX IF EXISTS idx_post_title;
9+
10+
-- 3. 검색 컬럼 추가
11+
ALTER TABLE post ADD COLUMN search_vector tsvector;
12+
13+
-- 4. GIN 인덱스 생성
14+
CREATE INDEX idx_post_search_vector ON post USING GIN (search_vector);
15+
16+
-- 5. 자동 업데이트 함수 + 트리거
17+
CREATE OR REPLACE FUNCTION post_search_vector_update()
18+
RETURNS TRIGGER AS $$
19+
BEGIN
20+
NEW.search_vector :=
21+
setweight(to_tsvector('simple', COALESCE(NEW.title, '')), 'A') ||
22+
setweight(to_tsvector('simple', COALESCE(NEW.content, '')), 'B');
23+
RETURN NEW;
24+
END;
25+
$$ LANGUAGE plpgsql;
26+
27+
CREATE TRIGGER tsvector_update
28+
BEFORE INSERT OR UPDATE OF title, content ON post
29+
FOR EACH ROW EXECUTE FUNCTION post_search_vector_update();
30+
31+
-- 6. 기존 데이터 업데이트
32+
UPDATE post SET search_vector =
33+
setweight(to_tsvector('simple', COALESCE(title, '')), 'A') ||
34+
setweight(to_tsvector('simple', COALESCE(content, '')), 'B')
35+
WHERE search_vector IS NULL;

0 commit comments

Comments
 (0)