import sqlite3 import numpy as np import time conn = sqlite3.connect(":memory:") c = conn.cursor() c.execute('''CREATE TABLE memory ( id INTEGER PRIMARY KEY, type TEXT, content TEXT, ts INTEGER, embedding BLOB )''') def to_blob(x): return x.astype(np.float32).tobytes() embeddings = np.random.randn(10000, 1536) start = time.time() for i in range(10000): c.execute("INSERT INTO memory (type, content, ts, embedding) VALUES (?, ?, ?, ?)", ('summary', f"content_{i}", i, to_blob(embeddings[i]))) conn.commit() print(f"SQLite 10k row insert: {(time.time()-start)*1000:.2f} ms") def search(query_embed, k=5): c.execute("SELECT content, embedding FROM memory WHERE type='summary' AND ts BETWEEN 0 AND 5000") rows = c.fetchall() X = np.array([np.frombuffer(row[1], dtype=np.float32) for row in rows]) dists = np.linalg.norm(X - query_embed, axis=1) topk = np.argsort(dists)[:k] return [rows[i][0] for i in topk] qvec = np.random.randn(1, 1536) start = time.time() results = search(qvec[0], k=5) print(f"SQLite hybrid filter + search latency: {(time.time()-start)*1000:.2f} ms")