Hands-On Lab: Chunking Strategies & Vector Search Latency on pgvector vs DiskANN

Retrieval-Augmented Generation (RAG) performance depends entirely on two variables: how documents are chunked and how vectors are indexed. In this hands-on lab, we benchmark chunking strategies and compare vector retrieval latency across Azure PostgreSQL Flexible Server and Azure Cosmos DB for NoSQL.

Written by Maya Patel (Principal Database Systems Architect) · Peer Reviewed by Rohan (Cloud & Infrastructure Engineer) · Last reviewed:

Lab Objective & Architecture Overview

Domain 2 of the AI-200 exam requires you to architect scalable vector stores for AI retrieval pipelines. Candidates often memorize the syntax without understanding the operational characteristics of index algorithms like HNSW, IVFFlat, and DiskANN.

This lab implements a controlled testbed comparing:

  • Dataset: 12,500 pages of enterprise technical and cloud architecture documentation.
  • Embedding Model: Azure OpenAI text-embedding-3-small (1,536 dimensions).
  • Chunking Strategies: 256 tokens (30-token overlap), 512 tokens (50-token overlap), and 1,024 tokens (100-token overlap) using TikToken cl100k_base.
  • Database Systems: Azure PostgreSQL Flexible Server (D4ds_v5, 16 GiB RAM) with pgvector 0.7+ vs Azure Cosmos DB for NoSQL (10,000 autoscale RU/s) with diskANN.

Chunking Benchmark Results

Smaller chunks provide higher cosine similarity scores because the embedding vector is concentrated on a specific topic. However, they lack broad context unless combined with parent-document retrieval.

Chunk Size (Tokens) Total Chunks Generated Avg Index Time (min) Top-3 Precision @ 10 Context Sufficiency
256 Tokens 48,200 4.2 min 89.4% Low (Frequently truncated multi-step procedures)
512 Tokens (Recommended) 24,150 2.1 min 92.1% Optimal (Captures full conceptual paragraphs)
1,024 Tokens 12,080 1.1 min 84.7% Diluted (Embedding captures too many competing concepts)

Query Latency: pgvector (HNSW) vs Cosmos DB (DiskANN)

We executed 1,000 concurrent vector queries with top_k = 5 over a 50,000 chunk vector index. Latency was measured from an Azure Container App running in the same East US region over private networking.

Engine & Index Configuration Avg Latency (P50) P95 Latency P99 Latency Throughput (QPS)
pgvector (HNSW, m=16, ef_search=40) 3.8 ms 7.2 ms 12.4 ms 245 QPS
pgvector (HNSW, m=16, ef_search=100) 6.4 ms 11.8 ms 18.1 ms 160 QPS
pgvector (IVFFlat, lists=100, probes=10) 8.2 ms 16.5 ms 24.0 ms 115 QPS
Cosmos DB NoSQL (diskANN, single-partition) 6.1 ms 10.4 ms 15.2 ms 190 QPS
Cosmos DB NoSQL (diskANN, cross-partition) 14.8 ms 28.5 ms 42.0 ms 72 QPS
Lab Observation & Exam Rule

Single-partition vector search in Cosmos DB is fast (6ms) and economical (~3–5 RUs). However, cross-partition vector search fans out to all physical partitions, tripling latency and multiplying RU consumption. For global unpartitioned vector search, pgvector or Azure AI Search is significantly more cost-predictable.

Lab Code: Parameterized pgvector Query with PgBouncer

Below is the production query pattern tested in the lab using async Python with connection pooling:

import asyncpg

async def search_pgvector(pool, query_embedding, company_id, top_k=5):
    # Operator <=> is Cosine Distance (range 0 to 2)
    # Cosine Similarity = 1 - distance
    query = """
        SELECT id, chunk_content, 
               1 - (embedding <=> $1::vector) AS similarity_score
        FROM technical_docs
        WHERE tenant_id = $2
        ORDER BY embedding <=> $1::vector ASC
        LIMIT $3;
    """
    async with pool.acquire() as conn:
        # Tune recall dynamically per session
        await conn.execute("SET LOCAL hnsw.ef_search = 64;")
        rows = await conn.fetch(query, str(query_embedding), company_id, top_k)
        return [dict(r) for r in rows]

Key Takeaways for AI-200 Exam Day

  1. Operator Memorization: pgvector uses <=> for cosine distance, <-> for L2 Euclidean, and <#> for negative inner product.
  2. HNSW vs IVFFlat: HNSW delivers higher recall and doesn't require upfront clustering on non-empty tables; IVFFlat requires less memory but suffers lower recall.
  3. Cosmos DB DiskANN: Defined in the container's vectorEmbeddingPolicy and indexingPolicy. Must specify dimensions (e.g. 1536) and distance metric (cosine).
  4. Cost Optimization: Always scope vector searches to a partition key in Cosmos DB whenever possible to avoid cross-partition RU explosions.

Frequently asked questions

What is the optimal chunk size for Azure RAG systems on the AI-200 exam?

For technical and regulatory documentation, 512 tokens with a 10% overlap (50 tokens) offers the optimal balance between semantic specificity and retrieval context. 256 tokens frequently splits related sentences, while 1024 tokens dilutes embedding precision.

When does pgvector outperform Cosmos DB DiskANN in vector latency?

pgvector with an HNSW index outperforms DiskANN when vector datasets fit comfortably within RAM buffer pools (<500,000 vectors) and relational JOINs with user/tenant metadata are required. DiskANN outperforms pgvector at massive multi-gigabyte scale on SSD with high concurrent write throughput.

Why is cosine distance preferred over Euclidean distance for OpenAI embeddings?

OpenAI text-embedding-3 models produce normalized unit vectors (length 1.0). For normalized vectors, cosine distance and Euclidean distance yield identical rankings, but cosine distance calculations (<=> in pgvector) avoid magnitude recalculations and align directly with cosine similarity.

How does ef_search impact pgvector query performance?

In HNSW, ef_search defines the size of the dynamic candidate list evaluated during index traversal. Increasing ef_search improves recall (accuracy) at the cost of higher query execution latency and lower QPS.