pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #902: Optimizing distance + date queries
7+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #902: Optimizing distance + date queries
@ 2025-09-26 08:22  "gustavfridell (@gustavfridell)" <noreply+gustavfridell@github.com>
  0 siblings, 0 replies; 7+ messages in thread

From: gustavfridell (@gustavfridell) @ 2025-09-26 08:22 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi,

I have a use case where I need to retrieve all results within a certain distance threshold **and** order them by the `published_at` column. The challenge is that this prevents ordering by distance, which means I can't leverage an index to optimize these queries.

One idea I had was to first filter by date (using an index) to reduce the number of rows from millions to a few thousand, and then let Postgres perform a regular scan to check which rows are within the distance threshold. In practice, though, this approach is too slow for real-world usage.

Are there any practical strategies to make this more efficient using pgvector?

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries
@ 2025-09-26 23:09  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2025-09-26 23:09 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @gustavfridell, I'd expect the filter by date approach to perform well if only a few thousand rows match. What's the query and output of `EXPLAIN (ANALYZE, BUFFERS)`?

Edit: The output of `\d <table>` in `psql` would be helpful as well.

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries
@ 2025-09-29 10:11  "gustavfridell (@gustavfridell)" <noreply+gustavfridell@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: gustavfridell (@gustavfridell) @ 2025-09-29 10:11 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks @ankane, I've managed to improve it by pre-filtering using four hash slices of bit(128) + hamming distance threshold, and then doing the full inner product distance threshold filtering on the halfvec(1536) column only on the remaining results. Still, it is borderline too slow, and will likely become slower as my database grows. Without the pre-filtering this operation timed out (consistently took more than 30s). I'm thinking it could also be related to my setup, running on Supabase with a 2-core ARM CPU + 2 GB RAM.

Here's the EXPLAIN (ANALYZE, BUFFERS) output. I've removed the bit(128) / halfvec(1536) values for brevity.

```
...
```

The query:
```
...
```

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries
@ 2025-09-29 11:07  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2025-09-29 11:07 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I suspect increasing RAM will help, but a few more questions:

1. What's the output of `\d news` in `psql`?
2. What's the output of the query below in `psql`?

```sql
EXPLAIN (ANALYZE, BUFFERS)
SELECT id FROM news
WHERE
    published_at >= '2025-08-30 09:56:00.126+00' AND
    published_at <= '2025-09-29 09:56:00.126+00' AND
    embedding_vector <#> '[add vector here]' < -0.31
ORDER BY published_at DESC LIMIT 10;
```

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries
@ 2025-09-29 19:38  "gustavfridell (@gustavfridell)" <noreply+gustavfridell@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: gustavfridell (@gustavfridell) @ 2025-09-29 19:38 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

That query depends a lot on the specific vector I'm running and whether I've recently run the same one.  A fresh one gives this, for example:

```
...
```

This is the table definition:
```
...
```

No index on the vector columns as I don't order by the distance.

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries
@ 2025-09-29 20:25  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2025-09-29 20:25 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

It looks like very few rows match the distance filter (it scanned 36758 rows to find 1 matching). If this is typical, you'll likely see better performance by partitioning the table by `published_at`, adding an HNSW index, and updating the query to use it.

You'll likely also see better performance by adding more RAM (as buffers `read` indicates the query needed to read from disk).

Finally, please use `psql` in the future to make the EXPLAIN output more readable and ensure the table definition is accurate.

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #902: Optimizing distance + date queries
@ 2025-09-29 20:25  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2025-09-29 20:25 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2025-09-29 20:25 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-09-26 08:22 [pgvector/pgvector] issue #902: Optimizing distance + date queries "gustavfridell (@gustavfridell)" <noreply+gustavfridell@github.com>
2025-09-26 23:09 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-09-29 10:11 ` "gustavfridell (@gustavfridell)" <noreply+gustavfridell@github.com>
2025-09-29 11:07 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-09-29 19:38 ` "gustavfridell (@gustavfridell)" <noreply+gustavfridell@github.com>
2025-09-29 20:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-09-29 20:25 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox