-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_search_optimization.py
More file actions
330 lines (284 loc) · 12.9 KB
/
Copy pathvector_search_optimization.py
File metadata and controls
330 lines (284 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# =============================================================================
# Vector Search Optimization - Python pgvector Examples
# pip install: psycopg[binary] pgvector numpy
# =============================================================================
import numpy as np
import psycopg
from pgvector.psycopg import register_vector
# =============================================================================
# Example 1: Create and Compare Index Types
# =============================================================================
def create_indexes():
"""Create HNSW and IVFFlat indexes and compare build times."""
conninfo = (
"host=myserver.postgres.database.azure.com "
"port=5432 dbname=vectordb user=adminuser "
"password=secret sslmode=require"
)
with psycopg.connect(conninfo) as conn:
conn.autocommit = True
register_vector(conn)
with conn.cursor() as cur:
# Create table
cur.execute("""
CREATE TABLE IF NOT EXISTS vec_items (
id SERIAL PRIMARY KEY,
content TEXT,
embedding vector(256)
)
""")
# Insert random vectors if table is empty
cur.execute("SELECT count(*) FROM vec_items")
count = cur.fetchone()[0]
if count == 0:
print("Inserting 50,000 random vectors...")
vectors = np.random.rand(50000, 256).astype(np.float32)
for i, vec in enumerate(vectors):
cur.execute(
"INSERT INTO vec_items (content, embedding) VALUES (%s, %s)",
(f"document_{i}", vec.tolist()),
)
print("Done inserting vectors.")
# Build HNSW index
cur.execute("DROP INDEX IF EXISTS idx_hnsw")
cur.execute("SET maintenance_work_mem = '512MB'")
print("Building HNSW index...")
cur.execute("""
CREATE INDEX idx_hnsw ON vec_items
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64)
""")
print("HNSW index created.")
# Build IVFFlat index (lists = 50 for 50K rows)
cur.execute("DROP INDEX IF EXISTS idx_ivfflat")
print("Building IVFFlat index...")
cur.execute("""
CREATE INDEX idx_ivfflat ON vec_items
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 50)
""")
print("IVFFlat index created.")
# Show index sizes
cur.execute("""
SELECT indexname,
pg_size_pretty(pg_relation_size(indexname::regclass)) AS size
FROM pg_indexes
WHERE tablename = 'vec_items' AND indexname LIKE 'idx_%'
""")
print("\nIndex sizes:")
for row in cur.fetchall():
print(f" {row[0]}: {row[1]}")
cur.execute("RESET maintenance_work_mem")
# =============================================================================
# Example 2: Measure Recall at Different Parameter Settings
# =============================================================================
def measure_recall():
"""Compare recall between exact search and ANN search at various settings."""
conninfo = (
"host=myserver.postgres.database.azure.com "
"port=5432 dbname=vectordb user=adminuser "
"password=secret sslmode=require"
)
with psycopg.connect(conninfo) as conn:
register_vector(conn)
k = 20 # number of nearest neighbors
with conn.cursor() as cur:
# Get a query vector
cur.execute("SELECT embedding FROM vec_items WHERE id = 1")
query_vec = cur.fetchone()[0]
# Ground truth: exact nearest neighbors (sequential scan)
cur.execute("SET enable_indexscan = off")
cur.execute("SET enable_bitmapscan = off")
cur.execute(
"SELECT id FROM vec_items WHERE id != 1 "
"ORDER BY embedding <=> %s LIMIT %s",
(query_vec, k),
)
ground_truth = {row[0] for row in cur.fetchall()}
cur.execute("SET enable_indexscan = on")
cur.execute("SET enable_bitmapscan = on")
print(f"Ground truth ({k} nearest neighbors): {sorted(ground_truth)}")
# Test HNSW with different ef_search values
# Ensure HNSW index exists
cur.execute("DROP INDEX IF EXISTS idx_ivfflat")
print("\n--- HNSW Recall ---")
for ef in [10, 40, 100, 200]:
cur.execute(f"SET hnsw.ef_search = {ef}")
cur.execute(
"SELECT id FROM vec_items WHERE id != 1 "
"ORDER BY embedding <=> %s LIMIT %s",
(query_vec, k),
)
ann_results = {row[0] for row in cur.fetchall()}
recall = len(ground_truth & ann_results) / len(ground_truth)
print(f" ef_search={ef:3d}: recall={recall:.2f} "
f"({len(ground_truth & ann_results)}/{k} correct)")
# Rebuild IVFFlat and test with different probes
cur.execute("""
CREATE INDEX idx_ivfflat ON vec_items
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 50)
""")
cur.execute("DROP INDEX idx_hnsw")
print("\n--- IVFFlat Recall ---")
for probes in [1, 5, 10, 25]:
cur.execute(f"SET ivfflat.probes = {probes}")
cur.execute(
"SELECT id FROM vec_items WHERE id != 1 "
"ORDER BY embedding <=> %s LIMIT %s",
(query_vec, k),
)
ann_results = {row[0] for row in cur.fetchall()}
recall = len(ground_truth & ann_results) / len(ground_truth)
print(f" probes={probes:3d}: recall={recall:.2f} "
f"({len(ground_truth & ann_results)}/{k} correct)")
# Recreate HNSW for other examples
cur.execute("""
CREATE INDEX idx_hnsw ON vec_items
USING hnsw (embedding vector_cosine_ops)
""")
# =============================================================================
# Example 3: EXPLAIN ANALYZE to Verify Index Usage
# =============================================================================
def explain_analyze():
"""Use EXPLAIN ANALYZE to check whether a vector index is being used."""
conninfo = (
"host=myserver.postgres.database.azure.com "
"port=5432 dbname=vectordb user=adminuser "
"password=secret sslmode=require"
)
with psycopg.connect(conninfo) as conn:
register_vector(conn)
with conn.cursor() as cur:
cur.execute("SELECT embedding FROM vec_items WHERE id = 1")
query_vec = cur.fetchone()[0]
# Check query plan
cur.execute(
"EXPLAIN ANALYZE "
"SELECT id, embedding <=> %s AS distance "
"FROM vec_items "
"ORDER BY embedding <=> %s LIMIT 10",
(query_vec, query_vec),
)
plan = cur.fetchall()
print("Query plan:")
for line in plan:
text = line[0]
print(f" {text}")
# Flag whether index is used
if "Index Scan" in text:
print(" >>> INDEX IS BEING USED")
elif "Seq Scan" in text:
print(" >>> WARNING: SEQUENTIAL SCAN (no index used)")
# =============================================================================
# Example 4: Operator Class Mismatch Detection
# =============================================================================
def detect_operator_mismatch():
"""Demonstrate and detect operator class mismatches."""
conninfo = (
"host=myserver.postgres.database.azure.com "
"port=5432 dbname=vectordb user=adminuser "
"password=secret sslmode=require"
)
with psycopg.connect(conninfo) as conn:
register_vector(conn)
with conn.cursor() as cur:
cur.execute("SELECT embedding FROM vec_items WHERE id = 1")
query_vec = cur.fetchone()[0]
# Check which indexes exist and their operator classes
cur.execute("""
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'vec_items' AND indexname LIKE 'idx_%'
""")
print("Current indexes:")
for row in cur.fetchall():
print(f" {row[0]}: {row[1]}")
# Test cosine operator (should use index if vector_cosine_ops exists)
cur.execute(
"EXPLAIN (FORMAT TEXT) "
"SELECT id FROM vec_items ORDER BY embedding <=> %s LIMIT 10",
(query_vec,),
)
plan_cosine = [row[0] for row in cur.fetchall()]
uses_index = any("Index Scan" in line for line in plan_cosine)
print(f"\nCosine <=> query: {'USES INDEX' if uses_index else 'SEQ SCAN'}")
# Test L2 operator (will seq scan if only cosine index exists)
cur.execute(
"EXPLAIN (FORMAT TEXT) "
"SELECT id FROM vec_items ORDER BY embedding <-> %s LIMIT 10",
(query_vec,),
)
plan_l2 = [row[0] for row in cur.fetchall()]
uses_index = any("Index Scan" in line for line in plan_l2)
print(f"L2 <-> query: {'USES INDEX' if uses_index else 'SEQ SCAN (operator mismatch!)'}")
# =============================================================================
# Example 5: Hybrid Search — Vector + Full-Text
# =============================================================================
def hybrid_search():
"""Combine full-text keyword filtering with vector similarity ranking."""
conninfo = (
"host=myserver.postgres.database.azure.com "
"port=5432 dbname=vectordb user=adminuser "
"password=secret sslmode=require"
)
with psycopg.connect(conninfo) as conn:
conn.autocommit = True
register_vector(conn)
with conn.cursor() as cur:
# Ensure tsvector column and GIN index exist
cur.execute("""
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'vec_items' AND column_name = 'content_tsv'
) THEN
ALTER TABLE vec_items ADD COLUMN content_tsv tsvector
GENERATED ALWAYS AS (
to_tsvector('english', coalesce(content, ''))
) STORED;
CREATE INDEX idx_fts ON vec_items USING GIN (content_tsv);
END IF;
END $$;
""")
# Update some rows with searchable content
updates = [
(1, "Azure OpenAI provides GPT models for text generation"),
(2, "PostgreSQL pgvector enables vector similarity search"),
(3, "Azure Cognitive Search supports hybrid search patterns"),
(4, "Azure Functions runs serverless compute workloads"),
(5, "Azure OpenAI embeddings convert text to vectors"),
]
for doc_id, content in updates:
cur.execute(
"UPDATE vec_items SET content = %s WHERE id = %s",
(content, doc_id),
)
# Get query vector
cur.execute("SELECT embedding FROM vec_items WHERE id = 1")
query_vec = cur.fetchone()[0]
# Hybrid query: keyword filter + vector ranking
cur.execute(
"SELECT id, content, embedding <=> %s AS distance "
"FROM vec_items "
"WHERE content_tsv @@ plainto_tsquery('english', %s) "
"ORDER BY embedding <=> %s "
"LIMIT 5",
(query_vec, "Azure OpenAI", query_vec),
)
results = cur.fetchall()
print("Hybrid search results (keyword: 'Azure OpenAI', ranked by vector distance):")
for row in results:
print(f" id={row[0]}, distance={row[2]:.4f}, content={row[1]}")
if __name__ == "__main__":
print("=== Example 1: Create and Compare Indexes ===")
create_indexes()
print("\n=== Example 2: Measure Recall ===")
measure_recall()
print("\n=== Example 3: EXPLAIN ANALYZE ===")
explain_analyze()
print("\n=== Example 4: Operator Class Mismatch ===")
detect_operator_mismatch()
print("\n=== Example 5: Hybrid Search ===")
hybrid_search()