Skip to main content
LearnMLAcademy
Intermediate10 min read

Vector Databases

Select and query vector databases using HNSW indexing and metadata filtering, with token walkthroughs, prompt design tasks, and retrieval exercises.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Once you have vectors, how do you manage them?

A RAG system may need to search thousands or millions of passage embeddings and still keep track of the original text, document ID, date, tenant, and permissions.

A vector database or vector-capable data store keeps vectors together with metadata and provides similarity-search capabilities. The embedding model creates the vector; the database stores and searches it.

What is stored for one passage?

Illustrative record

Illustrative record
FieldExampleWhy it matters
TextEmployees receive 12 casual-leave days...Material returned to RAG
Embedding[0.18, -0.04, ...]Used for similarity search
Document IDhr-policy-2026Traceability
Version/date2026-08-01Freshness
Tenant / ACLcompany-A / HR-readAuthorization filtering

Follow the record rather than only its vector. The identifier connects a search hit back to source text; metadata lets the application keep the result within the appropriate version and scope.

A vector database stores more than the vector

Embedding model

source text → vector

Vector record

IDchunk-204
Sourcetext/reference
Embedding[0.12, −0.08, …]
Metadataproduct · language · tenant/access · embedding_version
shield

Authorized query

identity + scope surround retrieval

The application applies authorization before returning records; a topic filter alone is not an access-control boundary.

Exact search versus approximate nearest neighbours

For a small collection, comparing the query with every eligible vector can be simple and completely exact. As collections grow, scanning everything may become too expensive.

Search trade-off

Search trade-off
ApproachStrengthTrade-off
Exact nearest-neighbour searchChecks every eligible vector and finds the true nearest neighbours under the chosen metricWork grows with collection size
Approximate nearest-neighbour (ANN) indexReduces search work and scales to larger collectionsMay miss some true nearest neighbours; recall must be measured

ANN reduces search work by accepting a recall trade-off

Exact scan

Compare every eligible vector. Simple and exact; often sufficient when the collection is small.

ANN index

Navigate an HNSW-like graph or IVF-like cluster region. Fewer comparisons; some true neighbours may be missed.

Measure latency AND recall@k

Exact and approximate approaches should be compared on both latency and recall@k for the real collection.

Metadata filters are useful—but authorization is stronger

A topic filter such as department = "HR" can improve relevance. It does not automatically prove that the current user is allowed to read every HR document.

Do you always need a specialized vector database?

No. If a team already uses PostgreSQL, has a moderate vector workload, and needs relational joins or transactional metadata, a vector extension in the existing database can be a sensible starting point. Specialized infrastructure becomes useful when measured scale, latency, indexing, or operational requirements justify it.

Why the next lesson matters

The vector store can return candidates. How do chunk boundaries, hybrid search, reranking, and retrieval metrics improve what reaches the LLM?

Advanced RAG →

Summary / Key Takeaways

  • The embedding model creates vectors; the vector-capable database stores and searches them.
  • A useful record keeps the vector together with source text and traceable metadata.
  • Exact search can be simple and fully accurate for small enough collections.
  • ANN indexes reduce search work but can lose neighbour recall, so the trade-off should be measured.
  • Metadata relevance filters do not replace explicit authorization and tenant isolation.
  • An existing relational database with vector support may be enough before specialized infrastructure is justified.

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Vector Databases"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Knowledge Check

Progress: 0/5

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 5conceptual
Which component creates an embedding vector?
Question 2 of 5interpretation
Why can exact nearest-neighbour search be a good choice for a small collection?
Question 3 of 5conceptual
What is the central trade-off of ANN search?
Question 4 of 5debugging/diagnostic
A user can retrieve another tenant's document because the vector query filtered by topic but not permission. What failed?
Question 5 of 5practical selection
A team already operates PostgreSQL and has a moderate vector workload with relational joins. What is a reasonable starting point?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1Which component creates an embedding vector?▼

Correct Answer: The embedding model

The embedding model maps content to vectors; the database stores/searches those vectors.

2Why can exact nearest-neighbour search be a good choice for a small collection?▼

Correct Answer: It examines all eligible vectors and may be simple enough at small scale

Brute-force exact comparison can be operationally simple and fully accurate when the dataset is small enough.

3What is the central trade-off of ANN search?▼

Correct Answer: Higher scalability/lower search work in exchange for possible neighbour recall loss

Approximate indexes reduce search work but may miss some true nearest neighbours.

4A user can retrieve another tenant's document because the vector query filtered by topic but not permission. What failed?▼

Correct Answer: Authorization/scope enforcement

Metadata relevance filters do not automatically enforce access control; authorization must be explicit.

5A team already operates PostgreSQL and has a moderate vector workload with relational joins. What is a reasonable starting point?▼

Correct Answer: Evaluate a vector extension in the existing relational system before adding specialized infrastructure

A relational database with vector support can be sufficient; choose specialized infrastructure only when measured requirements justify it.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Next