From: jkatz (@jkatz) <noreply+jkatz@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #424: Update cost estimation to not use index when expected tuples is too low
Date: Mon, 22 Jan 2024 04:43:48 +0000
Message-ID: <gh-pgvector-pgvector-424-c1903240316@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-424@github.com>
References: <gh-pgvector-pgvector-424@github.com>
> The odds should be the same with the same ef_search. The index will find ef_search results, and only 5% of them will match.
I'm not following. The selectivity in the patch is being measured from the reduction in tuples in the table based on the non-IVFFLAT/HNSW filters. I do agree that the odds are the same if we're only considering selectivity. But if we're considering the actual cardinality, the odds do change. In the example I gave, 5% of 100K is 5K, whereas 5% of 100MM is 5MM, which is a far greater set of tuples to search from.
But -- and as this patch would address -- if the filtering is occurring as a result of the index, then flipping to a different index scan would increase the number of tuples returned, though possibly at a performance cost.
>> I wonder if another approach would be to error when there's a LIMIT/ef_search mismatch.
> That'd be another option (but not sure I like it as much as the others).
This at least gives a deterministic error that an app developer can adapt to. This would be encountered at programming time, and the developer can then adapt the query to use a higher `hnsw.ef_search` value, which could be adapted into the code. This avoids a "surprise" behavior in a query plan flip that the developer doesn't expect, and as a result has to do additional debugging.