You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To optimize full-text search for raffles, we have implemented PostgreSQL GIN (Generalized Inverted Index) indexes on the `raffle_metadata` table.
4
+
5
+
## Implementation Details
6
+
7
+
### 1. tsvector Column
8
+
We added a generated column `search_vector` of type `tsvector`. This column automatically aggregates and tokenizes text from the following columns:
9
+
-`title` (Weight A - Highest priority)
10
+
-`description` (Weight B)
11
+
-`category` (Weight C - Lowest priority)
12
+
13
+
The `english` dictionary is used for stemming (e.g., "raffles" matches "raffle").
14
+
15
+
### 2. GIN Index
16
+
A GIN index `idx_raffle_metadata_search_vector` was created on the `search_vector` column. Unlike B-tree indexes, GIN indexes are designed for composite values (like document vectors) and allow for very fast full-text searching.
17
+
18
+
### 3. Querying
19
+
The backend was updated to use the `@@` operator (via Supabase's `.textSearch()`) instead of the expensive `ilike %pattern%` operator. We use the `websearch` type to support advanced search syntax:
20
+
-`"exact phrase"`
21
+
-`word1 -word2` (exclude word2)
22
+
-`word1 OR word2`
23
+
24
+
## Performance Analysis
25
+
To verify performance improvements, run the following in the database console:
26
+
27
+
```sql
28
+
EXPLAIN ANALYZE
29
+
SELECT*
30
+
FROM raffle_metadata
31
+
WHERE search_vector @@ websearch_to_tsquery('english', 'your search query');
32
+
```
33
+
34
+
Expected results:
35
+
-**Index Scan** instead of **Sequential Scan**.
36
+
- Significant reduction in execution time as the number of raffles grows.
0 commit comments