pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #727: Why is the hnsw index not used?
6+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #727: Why is the hnsw index not used?
@ 2024-12-07 17:56 "aropb (@aropb)" <noreply+aropb@github.com>
  0 siblings, 0 replies; 6+ messages in thread

From: aropb (@aropb) @ 2024-12-07 17:56 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

**PostgreSQL 16
pgvector 0.7.4
OS Windows**

**Table:**

CREATE TABLE IF NOT EXISTS public."km-default"
(
    id text COLLATE pg_catalog."default" NOT NULL,
    tags text[] COLLATE pg_catalog."default" NOT NULL DEFAULT '{}'::text[],
    payload jsonb NOT NULL DEFAULT '{}'::jsonb,
    content text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::text,
    embedding vector(1536),
    CONSTRAINT "km-default_pkey" PRIMARY KEY (id)
)

CREATE INDEX IF NOT EXISTS idx_embedding
    ON public."km-default" USING hnsw
    (embedding vector_cosine_ops)
    TABLESPACE pg_default;

SELECT count(*) FROM public."km-default"
Count: 18 286

**Use case:**

SET enable_seqscan = OFF;
SET enable_indexscan = ON; 
SET enable_bitmapscan = ON;
SET enable_indexonlyscan = ON;

**1. Order By**

EXPLAIN ANALYZE 
SELECT id,tags,content,payload
FROM public."km-default"
ORDER BY embedding <=> '[...]' desc
LIMIT 1;

**Result:**

"Limit  (cost=10000002801.71..10000002801.71 rows=1 width=998) (actual time=188.376..188.378 rows=1 loops=1)"
"  ->  Sort  (cost=10000002801.71..10000002847.24 rows=18212 width=998) (actual time=188.375..188.375 rows=1 loops=1)"
"        Sort Key: ((embedding <=> '[...]'::vector)) DESC"
"        Sort Method: top-N heapsort  Memory: 30kB"
"        ->  Seq Scan on ""km-default""  (cost=10000000000.00..10000002710.65 rows=18212 width=998) (actual time=0.093..182.550 rows=18286 loops=1)"
"Planning Time: 0.114 ms"
"Execution Time: 188.403 ms"

**2. Where and Order By**

EXPLAIN ANALYZE 
SELECT id,tags,content,payload
FROM public."km-default"
WHERE "embedding" <=> '[...]' < 0.001
ORDER BY "embedding" <=> '[...]' desc
LIMIT 1;

**Result:**

"Limit  (cost=10000002801.71..10000002801.72 rows=1 width=998) (actual time=184.837..184.839 rows=1 loops=1)"
"  ->  Sort  (cost=10000002801.71..10000002816.89 rows=6071 width=998) (actual time=184.836..184.837 rows=1 loops=1)"
"        Sort Key: ((embedding <=> '[...]'::vector)) DESC"
"        Sort Method: top-N heapsort  Memory: 27kB"
"        ->  Seq Scan on ""km-default""  (cost=10000000000.00..10000002771.36 rows=6071 width=998) (actual time=37.617..184.802 rows=4 loops=1)"
"              Filter: ((embedding <=> '[...]'::vector) < '0.001'::double precision)"
"              Rows Removed by Filter: 18282"
"Planning Time: 0.131 ms"
"Execution Time: 184.870 ms"


Why is the hnsw index not used?


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

* Re: [pgvector/pgvector] issue #727: Why is the hnsw index not used?
@ 2024-12-07 18:26 ` "aropb (@aropb)" <noreply+aropb@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: aropb (@aropb) @ 2024-12-07 18:26 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I understood the reason, it is necessary to use:

ORDER BY dist asc


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

* Re: [pgvector/pgvector] issue #727: Why is the hnsw index not used?
@ 2024-12-07 18:32 ` "aropb (@aropb)" <noreply+aropb@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: aropb (@aropb) @ 2024-12-07 18:32 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@aropb closed this.

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

* Re: [pgvector/pgvector] issue #727: Why is the hnsw index not used?
@ 2024-12-07 18:36 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: jkatz (@jkatz) @ 2024-12-07 18:36 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Yes,the ordering needs to be in ascending order.

Additionally, if you use the default storage type for larger vectors (EXTERNAL), PostgreSQL may determine that a sequential scan is cheaper as it undercounts the amount of effort to complete the scan.

Additionally, pgvector 0.8.0 improves the overall costing methodology for calculating the cost of a HNSW scan.

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

* Re: [pgvector/pgvector] issue #727: Why is the hnsw index not used?
@ 2024-12-07 18:39 ` "aropb (@aropb)" <noreply+aropb@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: aropb (@aropb) @ 2024-12-07 18:39 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Please explain this "larger vectors (EXTERNAL)"?

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

* Re: [pgvector/pgvector] issue #727: Why is the hnsw index not used?
@ 2024-12-07 18:40 ` "aropb (@aropb)" <noreply+aropb@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: aropb (@aropb) @ 2024-12-07 18:40 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Also, please tell me where can I download pgvector 0.8.0 binaries for linux/windows?

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


end of thread, other threads:[~2024-12-07 18:40 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-12-07 17:56 [pgvector/pgvector] issue #727: Why is the hnsw index not used? "aropb (@aropb)" <noreply+aropb@github.com>
2024-12-07 18:26 ` "aropb (@aropb)" <noreply+aropb@github.com>
2024-12-07 18:32 ` "aropb (@aropb)" <noreply+aropb@github.com>
2024-12-07 18:36 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-12-07 18:39 ` "aropb (@aropb)" <noreply+aropb@github.com>
2024-12-07 18:40 ` "aropb (@aropb)" <noreply+aropb@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