การสร้างดัชนี HNSW เพื่อความครอบคลุม
สำรวจการสร้างดัชนี HNSW สำหรับ pgvector เพื่อให้ได้อัตราความครอบคลุมที่สูงขึ้นในการค้นหาความคล้ายคลึง โดยสมดุลระหว่างความเร็วและความถูกต้อง
การสร้างดัชนี HNSW เพื่อความครอบคลุม เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vector Databases: Pinecone, Weaviate & pgvector และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Boost Recall with HNSW
Welcome to HNSW indexing! In the previous lesson, we explored IVFFlat for speed. Now, we'll dive into Hierarchical Navigable Small World (HNSW), an advanced indexing technique in pgvector.
HNSW is excellent when you need to find most of the relevant results, even if it means a slight trade-off in query speed compared to IVFFlat. This is known as high recall.
HNSW vs. IVFFlat: A Quick Look
Remember IVFFlat indexes? They partition data for faster, approximate searches, optimizing for speed. HNSW takes a different approach to prioritize recall.
- IVFFlat: Faster queries, good enough recall.
- HNSW: Higher recall (finds more true positives), potentially slower build and query times.
Choosing between them depends on your application's needs: speed or comprehensive results.
How HNSW Indexes Work
Imagine HNSW as a multi-layered graph. It connects similar vectors across different layers:
- Top layers: Sparse graphs, quickly navigate large distances.
- Bottom layers: Dense graphs, fine-tune search for nearest neighbors.
This structure allows for efficient approximate nearest neighbor (ANN) search, quickly narrowing down the search space to find highly similar vectors.
Creating an HNSW Index
To use HNSW, you first need the pgvector extension. Then, you can create an HNSW index on your vector column. Here's the basic syntax:
CREATE INDEX ON items USING HNSW (embedding vector_l2_ops);
The vector_l2_ops specifies using L2 (Euclidean) distance. Other operators like vector_cosine_ops for cosine similarity are also available.
HNSW Parameter: `m` (Max Connections)
The m parameter determines the maximum number of connections a node (vector) has in the HNSW graph on each layer. It's crucial for index quality:
- Higher
m: More connections, better recall, but increases index size and build time. - Lower
m: Fewer connections, smaller index, faster build, but lower recall.
A common value for m is between 8 and 16, but it depends on your dataset and desired accuracy.
HNSW Parameter: `ef_construction`
The ef_construction parameter controls the size of the dynamic candidate list during graph construction. It impacts how thoroughly the index is built:
- Higher
ef_construction: More thorough search during build, better index quality (higher recall), but significantly slower build time. - Lower
ef_construction: Faster build, but potentially lower recall.
It's generally recommended to set ef_construction to a value 2-4 times m, or even higher for very high recall needs.
Code: Create an HNSW Index
Let's create a table and then an HNSW index with specific parameters. This example uses m=16 and ef_construction=64.
CREATE EXTENSION IF NOT EXISTS vector;
DROP TABLE IF EXISTS docs;
CREATE TABLE docs (
id serial PRIMARY KEY,
embedding vector(3)
);
INSERT INTO docs (embedding) VALUES
('[1,2,3]'),
('[1.1,2.1,3.1]'),
('[10,11,12]'),
('[10.5,11.5,12.5]'),
('[100,101,102]');
CREATE INDEX ON docs USING HNSW (embedding vector_l2_ops) WITH (
m = 16,
ef_construction = 64
);Querying with HNSW Indexes
Once your HNSW index is built, pgvector automatically uses it for similarity queries. The query syntax is the same as for other vector indexes:
SELECT id, embedding <-> '[1,2,3]' AS distance FROM docs ORDER BY distance LIMIT 3;
However, HNSW introduces another parameter at query time: ef_search.
HNSW Parameter: `ef_search`
The ef_search parameter controls the size of the dynamic candidate list during the actual search operation. You set this via a session variable:
- Higher
ef_search: More thorough search at query time, higher recall, but slower query execution. - Lower
ef_search: Faster queries, but potentially lower recall.
You typically set ef_search equal to or higher than ef_construction for optimal results, or tune it based on real-world query performance.
HNSW Trade-offs & Considerations
While HNSW offers superior recall, it comes with trade-offs:
- Memory Usage: HNSW indexes are generally larger and consume more memory than IVFFlat.
- Build Time: Index creation can be significantly slower, especially with high
mandef_construction. - Query Latency: Queries might be slightly slower than IVFFlat, depending on
ef_search.
Always test with your specific dataset to find the best balance of parameters for your application.
Check Your HNSW Knowledge
Which HNSW parameter primarily affects the recall and build time of the index by controlling the thoroughness of the graph construction?
Recap: HNSW for Recall
Great job! You've explored HNSW indexing in pgvector.
- HNSW prioritizes recall, aiming to find most relevant results.
- It works by building a multi-layered graph structure.
- Key parameters are
m(max connections) andef_construction(build thoroughness). ef_searchtunes query-time recall and speed.- HNSW indexes can be larger and slower to build/query than IVFFlat, but offer higher recall.
Next, we'll learn how to tune queries for optimal performance!
เรียนรู้ Vector Databases: Pinecone, Weaviate & pgvector ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การสร้างดัชนี HNSW เพื่อความครอบคลุม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างดัชนี HNSW เพื่อความครอบคลุม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างดัชนี HNSW เพื่อความครอบคลุม”
สำรวจการสร้างดัชนี HNSW สำหรับ pgvector เพื่อให้ได้อัตราความครอบคลุมที่สูงขึ้นในการค้นหาความคล้ายคลึง โดยสมดุลระหว่างความเร็วและความถูกต้อง คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างดัชนี HNSW เพื่อความครอบคลุม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม
ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างดัชนี IVFFlat เพื่อความเร็ว
- การสร้างดัชนี HNSW เพื่อความครอบคลุม
- การปรับแต่งประสิทธิภาพคำค้น
- เพิ่มประสิทธิภาพการค้นหาแบบมีตัวกรอง