การสร้างดัชนี IVFFlat เพื่อความเร็ว
สร้างดัชนี IVFFlat ใน pgvector เพื่อเร่งการค้นหาเพื่อนบ้านที่ใกล้ที่สุดแบบประมาณค่า ทำให้การค้นหาเร็วขึ้น
การสร้างดัชนี IVFFlat เพื่อความเร็ว เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vector Databases: Pinecone, Weaviate & pgvector และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Faster Searches with IVFFlat
Welcome to optimizing pgvector performance! For small datasets, a simple vector similarity query might be fast enough. But as your data grows, brute-force search becomes too slow.
This lesson introduces IVFFlat indexing, a powerful technique to accelerate Approximate Nearest Neighbor (ANN) searches in pgvector.
Why Vector Indexes are Key
Imagine searching for the closest person to you in a massive crowd. Without any organization, you'd have to check every single person.
Similarly, without an index, pgvector has to compare your query vector to every single vector in your table. This is called a brute-force search, and it's inefficient for large datasets.
- Indexes organize data.
- They make lookups much faster.
- Crucial for scalable vector search.
IVFFlat: The Core Idea
IVFFlat (Inverted File Index with Flat quantizer) is an ANN indexing algorithm. Its core idea is to group similar vectors into 'lists' or 'clusters'.
Think of it like sorting books by genre before looking for a specific title. You first pick the right genre, then search within that smaller section.
How IVFFlat Works: Two Steps
When you query an IVFFlat index, it performs a two-step process:
- Find nearest lists: It quickly identifies a few (or more) clusters that are closest to your query vector.
- Search within lists: It then only searches for nearest neighbors within those selected clusters, ignoring the rest of the dataset.
This significantly reduces the number of comparisons needed, speeding up your queries.
The `lists` Parameter Explained
When creating an IVFFlat index, the most important parameter is lists. This defines how many clusters (or partitions) your data will be divided into.
- More
lists: Each cluster has fewer vectors. This can lead to faster searches (less data to scan per cluster). - Fewer
lists: Each cluster has more vectors. Searches might be slower, but you're less likely to miss true nearest neighbors.
It's a trade-off between search speed and recall (how many of the true nearest neighbors you actually find).
Prepare Your Vector Table
Before creating an index, you need a table with a vector column. If you haven't already, ensure the vector extension is enabled.
Here's a quick setup for a table with 3-dimensional vectors:
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE items (
id serial PRIMARY KEY,
embedding vector(3)
);
INSERT INTO items (embedding) VALUES
('[1,2,3]'),
('[1.1,2.1,3.1]'),
('[4,5,6]'),
('[4.1,5.1,6.1]'),
('[7,8,9]'),
('[7.1,8.1,9.1]');Creating an IVFFlat Index
Now, let's create an IVFFlat index on our embedding column. We'll specify the vector_l2_ops operator class for L2 distance (Euclidean distance), and set the lists parameter.
A good starting point for lists is rows / 1000 for up to 1M rows, or sqrt(rows) for larger datasets.
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 4);Querying with the Index
Once the IVFFlat index is created, your standard similarity queries will automatically benefit from it! pgvector will use the index to find approximate nearest neighbors much faster.
Let's find the 3 closest items to [1,2,3]:
SELECT id, embedding <-> '[1,2,3]' AS distance
FROM items
ORDER BY distance
LIMIT 3;The `probes` Query Parameter
At query time, you can further tune the search with the ivfflat.probes parameter. This setting controls how many of the nearest 'lists' (clusters) are actually searched.
- More
probes: Better recall, but slower search. - Fewer
probes: Faster search, but potentially lower recall.
The default probes value is 1. You can set it for your session:
SET ivfflat.probes = 2;
SELECT id, embedding <-> '[1,2,3]' AS distance
FROM items
ORDER BY distance
LIMIT 3;IVFFlat Indexing Check
Let's test your understanding of IVFFlat indexing.
IVFFlat: Key Takeaways
Great job! You've learned the fundamentals of IVFFlat indexing in pgvector.
- IVFFlat speeds up ANN searches by partitioning data.
- The
listsparameter (index creation) controls the number of clusters. - The
ivfflat.probesparameter (query time) controls how many clusters are searched. - Both involve a trade-off between search speed and recall.
Next, we'll explore HNSW indexing, another powerful option for even higher recall.
คำถามที่พบบ่อย
บทเรียน “การสร้างดัชนี IVFFlat เพื่อความเร็ว” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างดัชนี IVFFlat เพื่อความเร็ว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างดัชนี IVFFlat เพื่อความเร็ว”
สร้างดัชนี IVFFlat ใน pgvector เพื่อเร่งการค้นหาเพื่อนบ้านที่ใกล้ที่สุดแบบประมาณค่า ทำให้การค้นหาเร็วขึ้น คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างดัชนี IVFFlat เพื่อความเร็ว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม
ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างดัชนี IVFFlat เพื่อความเร็ว
- การสร้างดัชนี HNSW เพื่อความครอบคลุม
- การปรับแต่งประสิทธิภาพคำค้น
- เพิ่มประสิทธิภาพการค้นหาแบบมีตัวกรอง