Chapter 37
Vector Search
MySQL Vector Search and AI Integration
MySQL 9.0 introduces the VECTOR(dimensions) data type, enabling semantic search and RAG architectures without a separate vector database.
VECTOR Type (MySQL 9.0+)
CREATE TABLE docs (
id INT AUTO_INCREMENT PRIMARY KEY,
content TEXT,
embedding VECTOR(1536) NOT NULL,
VECTOR INDEX idx_emb (embedding)
);
-- ANN similarity search
SELECT id, content,
1 - VECTOR_DISTANCE(embedding, STRING_TO_VECTOR(?), 'COSINE') AS similarity
FROM docs
ORDER BY VECTOR_DISTANCE(embedding, STRING_TO_VECTOR(?), 'COSINE') ASC
LIMIT 10;
RAG Pattern
- Embed the user's question
- Vector search MySQL for top-K relevant chunks (similarity > 0.7)
- Build prompt: context + question
- Send to LLM and stream response
Alternatives
- MySQL 9.0 VECTOR: zero ops overhead, good for <5M vectors
- pgvector: HNSW index, better performance, requires PostgreSQL
- Milvus/Qdrant: billion-scale, dedicated ops cost