Database & AI Infrastructure
Công Cụ Tính Toán RAM & Kích Thước Chỉ Mục pgvector HNSW
Mô hình hóa nhu cầu bộ nhớ RAM cho chỉ mục PostgreSQL pgvector HNSW trước khi triển khai thực tế. Tránh nghẽn I/O đĩa và xuất cấu hình SQL chuẩn tối ưu.
1. Dataset & Vector Dimensions
Vector Count (Rows)1,000,000 vectors
2. Vector Precision & Quantization
3. HNSW Graph Hyperparameters
Higher m increases recall at the cost of index RAM and build time.
Controls graph build quality. Does not affect index RAM size.
Fast (20)Balanced (100)Max Recall (400)
4. PostgreSQL Host Server Specs
Memory & Sizing Estimates
Index fits cleanly in RAM
HNSW graph and vector payloads (7.05 GB) fit comfortably inside your 32 GB server memory for sub-15ms search latency.
HNSW Index Size (RAM):7.05 GB
Raw Table Footprint:6.39 GB
Total Recommended RAM:> 8.8 GB
Recommended maintenance_work_mem:7943 MB
Est. Index Build Time:~21s
Key SRE Rule for pgvector
Unlike B-tree indexes where partial cache hits still yield acceptable performance, HNSW vector search relies on non-local random graph walks. If the index spills from RAM to disk, latency degrades from 5ms to 800ms+.
-- 1. Enable pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;
-- 2. Create optimized vector storage table
CREATE TABLE IF NOT EXISTS documents (
id BIGSERIAL PRIMARY KEY,
content TEXT NOT NULL,
metadata JSONB DEFAULT '{}'::jsonb,
embedding vector(1536) NOT NULL
);
-- 3. Set dedicated build memory for the current session (avoids disk spills)
SET maintenance_work_mem = '7943MB';
SET max_parallel_maintenance_workers = 6;
-- 4. Create HNSW index for sub-15ms semantic search
CREATE INDEX IF NOT EXISTS idx_documents_embedding_hnsw
ON documents
USING hnsw (embedding vector_cosine_ops)
WITH (
m = 16,
ef_construction = 64
);Advertisement
How to use
- Step 1: Chọn số lượng vector (100k, 1M, 10M) và mô hình embedding (OpenAI 1536d, BGE 768d).
- Step 2: Chọn chuẩn lượng tử hóa (Float32 vector, Float16 halfvec, hoặc Bit nhị phân).
- Step 3: Điều chỉnh tham số m và ef_construction.
- Step 4: Sao chép mã SQL DDL và postgresql.conf để đưa vào sản phẩm.
Frequently asked questions
Tại sao chỉ mục pgvector HNSW cần nằm hoàn toàn trong RAM?
HNSW thực hiện duyệt đồ thị ngẫu nhiên. Nếu chỉ mục bị tràn ra đĩa, độ trễ truy vấn sẽ tăng từ 5ms lên hơn 800ms. Toàn bộ chỉ mục cần nằm trong RAM máy chủ.
Sự khác biệt giữa vector và halfvec là gì?
halfvec sử dụng số thực 16-bit thay vì 32-bit, giúp giảm 50% dung lượng RAM và đĩa mà vẫn giữ được hơn 99% độ chính xác tìm kiếm.
The Senior Full-Stack Production Kit (2026)
Checklists, Docker recipes, and SSR error fixes in one dense PDF.