From: shikaiwei1 (@shikaiwei1) <noreply+shikaiwei1@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #785: Continued Issues with HNSW Index Queries Using ORDER BY and LIMIT Despite Parameter Adjustments
Date: Tue, 25 Feb 2025 09:09:10 +0000
Message-ID: <gh-pgvector-pgvector-785@github.com> (raw)
**Description:**
After following recommendations from the previous issue (#719), I'm still encountering problems with HNSW index queries when combining `ORDER BY` and `LIMIT` clauses, particularly under specific filtering conditions. Adjusting parameters like `hnsw.iterative_scan`, `hnsw.scan_mem_multiplier`, and `hnsw.max_scan_tuples` partially mitigates the issue but introduces significant performance trade-offs.
### Problem Details
**Query Structure:**
```sql
SELECT
(vector <#> '[VECTOR]') AS score
from modeldata
where 1=1
AND dataset_id in ('67b709f9460b03446665ac40')
order by score
limit 100;
```
**Expected Behavior:**
The query should return the top 100 nearest neighbors efficiently, leveraging the HNSW index while applying filters.
**Actual Behavior:**
- **No results or incomplete results** unless specific parameters are adjusted.
- Even with adjustments, performance degrades significantly (e.g., 229ms → 3228ms).
---
### Reproduction Steps
1. **Index Configuration:**
```sql
CREATE INDEX vector_index ON public.modeldata USING hnsw (vector vector_ip_ops) WITH (m='32', ef_construction='128')
```
2. **Query Execution:**
- Run the above query without parameter adjustments → **0 results**.
- Adjust parameters as follows:
```sql
SET hnsw.iterative_scan = 'relaxed_order';
SET hnsw.scan_mem_multiplier = 2; -- Higher values increase latency
SET hnsw.max_scan_tuples = 20000;
```
→ Returns **4/100 results** in 229ms.
- Further increase `scan_mem_multiplier` (e.g., 2) → Returns more results but with **~3x latency** (3228ms).
---
### Test Results
| Parameter | Results Returned | Latency |
|----------------------------------|-------------------|----------|
| Default settings | 0 | N/A |
| `relaxed_order` only | 4 | 229ms |
| `scan_mem_multiplier=2` | Improved results | 3228ms |
| `max_scan_tuples=20000` | Partial results | Variable|
**Key Observations:**
- `hnsw.iterative_scan=relaxed_order` helps but does not fully resolve recall issues.
- Increasing memory/scan thresholds trades latency for completeness, but scalability concerns remain (e.g., billion-row datasets).
---
### Environment
- **pgvector Version:** 0.8.0
- **PostgreSQL Version:** 15 .0
- **Index Type:** HNSW
- **Data Scale:** Millions of rows (testing), targeting billions.
---
### Request
1. **Investigate why parameter adjustments are insufficient** to guarantee complete results without prohibitive latency.
2. **Optimize HNSW index scans** to handle strict `ORDER BY + LIMIT` with filters more efficiently.
3. Consider enabling iterative scans (`relaxed_order`) by default or improving documentation for such edge cases.
This issue persists despite following recommendations from #719. Any guidance or optimizations would be greatly appreciated!
**Related Issues:** #719, #721