0Pricing
AI Engineering Academy · 课时

为什么需要向量数据库

了解暴力相似度搜索的局限性、HNSW 等近似最近邻算法的工作方式,以及向量数据库在生产环境中解决的问题。

为什么需要向量数据库 是 CoddyKit 上的免费 AI Engineering Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Engineering Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Engineering Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Limits of In-Memory Search

NumPy semantic search works well for small corpora, but it has a fundamental scalability problem: every search scans every vector. At 1 million documents, each query requires 1.5 billion floating-point multiplications, taking hundreds of milliseconds. Worse, all vectors must fit in RAM.

Production AI systems need search over millions of documents in under 50ms. This is what vector databases are designed to deliver.

Approximate Nearest Neighbor Search

Approximate Nearest Neighbor (ANN) algorithms trade a small amount of accuracy for dramatic speed improvements. Instead of checking every vector, ANN algorithms use smart indexing structures to skip large portions of the search space.

In practice, ANN returns the true nearest neighbor over 95% of the time while being 100-1000x faster than exact search. For RAG, this tradeoff is almost always worth it.

How HNSW Works

HNSW (Hierarchical Navigable Small World) is the dominant ANN algorithm used by Pinecone, Weaviate, Qdrant, and pgvector. It builds a multi-layer graph where each node connects to its nearest neighbors. Search starts at the top sparse layer, navigates to the approximate region, then descends to the dense bottom layer for precision.

HNSW offers excellent query speed (logarithmic in dataset size) and high recall, but requires building the index upfront.

What Vector Databases Add on Top

A vector database is more than an ANN index. It also provides:

  • Metadata filtering — retrieve only vectors where category='finance' or date > '2024-01-01'
  • Persistent storage — data survives restarts and scales beyond RAM
  • CRUD operations — insert, update, and delete individual vectors
  • Namespace isolation — separate collections for different customers or environments
  • Horizontal scaling — distribute millions of vectors across shards

Metadata Filtering in Practice

Metadata filtering lets you restrict retrieval to a relevant subset before running ANN search. For example, in a multi-tenant RAG system, you would filter by tenant_id so users only see their own documents. Without metadata filtering, you would need a separate index per tenant.

This is one of the most important capabilities that separates vector databases from simple ANN libraries like FAISS.

# Conceptual example — Pinecone query with metadata filter
results = index.query(
    vector=query_embedding,
    top_k=5,
    filter={
        'tenant_id': {'$eq': 'acme_corp'},
        'document_type': {'$in': ['invoice', 'contract']},
        'date': {'$gte': '2024-01-01'}
    },
    include_metadata=True
)

FAISS: High-Performance ANN Library

FAISS (Facebook AI Similarity Search) is an open-source ANN library from Meta — the fastest option for GPU-accelerated search. It is not a full database: it has no persistence, no metadata, and no built-in serving.

FAISS is ideal when you need maximum throughput on a single machine and manage persistence yourself. Chroma, Weaviate, and pgvector all use FAISS or HNSW under the hood.

import faiss
import numpy as np

d = 1536  # dimension
n = 10000  # number of vectors

# Build a flat (exact) index as a baseline
index = faiss.IndexFlatIP(d)  # Inner Product = dot product

# Add random vectors (pretend these are embeddings)
vectors = np.random.randn(n, d).astype('float32')
faiss.normalize_L2(vectors)  # normalize for cosine sim
index.add(vectors)

query = np.random.randn(1, d).astype('float32')
faiss.normalize_L2(query)

scores, indices = index.search(query, k=5)
print('Top 5 indices:', indices[0])
print('Top 5 scores:', scores[0])

Vector Databases vs Traditional Databases

Traditional SQL databases like PostgreSQL are optimized for exact lookups and range queries on structured data. They are not designed for high-dimensional nearest neighbor search. Even with the pgvector extension, pure PostgreSQL is slower than purpose-built vector databases for large corpora.

However, pgvector is an excellent choice when your application already runs on PostgreSQL and your corpus is under a few million documents, since it avoids adding another infrastructure component.

Managed vs Self-Hosted Options

Vector database choices fall into two categories:

  • Managed (serverless): Pinecone, Weaviate Cloud — no infrastructure to manage, pay per query/storage, immediate scalability
  • Self-hosted: Qdrant, Chroma, Weaviate open-source, pgvector — full control, lower cost at scale, but you manage backups, upgrades, and scaling

For early-stage projects, start with a managed service to move quickly. Evaluate self-hosting when monthly costs exceed $200-300.

Index Types: Flat, IVF, and HNSW

Different index types offer different trade-offs:

  • Flat: Exact search, no approximation, slow at scale but zero accuracy loss — good for baseline benchmarking
  • IVF (Inverted File): Partitions vectors into clusters, searches only the nearest clusters — fast but needs tuning of nlist and nprobe
  • HNSW: Graph-based, best recall-speed trade-off for most workloads, the default in most production databases

Quantization for Memory Reduction

Vector quantization compresses each 32-bit float in a vector to fewer bits, dramatically reducing memory usage at the cost of slight accuracy loss:

  • FP32: 1536 dims × 4 bytes = 6KB per vector
  • FP16: 3KB per vector — 2x compression, negligible accuracy loss
  • INT8: 1.5KB per vector — 4x compression, ~1% recall drop

At 10 million vectors, INT8 quantization reduces memory from 60GB to 15GB, making the difference between fitting in RAM or not.

When to Upgrade from NumPy to a Vector DB

Consider switching from in-memory NumPy search to a vector database when:

  • Your corpus exceeds 50,000 documents and query latency degrades
  • You need metadata filtering (by date, user, category, etc.)
  • You need persistence that survives application restarts
  • Multiple services or users need to share the same index
  • You need to update or delete individual documents without re-indexing everything

Quick Check

Test your understanding of AI Engineering concepts from this lesson.

Lesson Recap

In this lesson you learned: brute-force NumPy search does not scale beyond tens of thousands of documents, HNSW enables fast approximate nearest neighbor search by navigating a hierarchical graph, and vector databases add metadata filtering, persistence, and CRUD operations on top of ANN indexes. Next up we set up Pinecone, the most popular managed vector database, and index our first documents.

常见问题解答

「为什么需要向量数据库」课时是免费的吗?

是的 — 「为什么需要向量数据库」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Engineering Academy 课程的其余内容,请升级到 CoddyKit PRO。 AI Engineering Academy 课程共包含 4 节课。

「为什么需要向量数据库」这节课中我会学到什么?

了解暴力相似度搜索的局限性、HNSW 等近似最近邻算法的工作方式,以及向量数据库在生产环境中解决的问题。 你通过在浏览器中直接运行的动手代码来练习 AI Engineering Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AI Engineering Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AI Engineering Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「为什么需要向量数据库」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AI Engineering Academy 课中编写并运行代码吗?

能。每节 AI Engineering Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为什么需要向量数据库
  2. Pinecone 入门
  3. pgvector:PostgreSQL 中的嵌入
  4. 选择并基准测试向量存储
← 返回 AI Engineering Academy