Re: workaround for expensive KNN? - Mailing list pgsql-hackers

From Tom Lane
Subject Re: workaround for expensive KNN?
Date
Msg-id 12816.1302278343@sss.pgh.pa.us
Whole thread Raw
In response to Re: workaround for expensive KNN?  (Oleg Bartunov <oleg@sai.msu.su>)
List pgsql-hackers
Oleg Bartunov <oleg@sai.msu.su> writes:
> what if you create index (price,title) ?

I think that SELECT ... WHERE ... ORDER BY ... LIMIT is basically an
intractable problem.  We've recognized the difficulty in connection with
btree indexes for a long time, and there is no reason at all to think
that KNNGist will somehow magically dodge it.  You can either visit
*all* of the rows satisfying WHERE (and then sort them), or you can
visit the rows in ORDER BY order and hope that you find enough of them
satisfying the WHERE in a reasonable amount of time.  Either of these
strategies loses badly in many real-world cases.

Maybe with some sort of fuzzy notion of ordering it'd be possible to go
faster, but as long as you insist on an exact ORDER BY result, I don't
see any way out of it.

One way to be fuzzy is to introduce a maximum search distance:

SELECT ... WHERE x < limit AND other-conditions ORDER BY x LIMIT n

which essentially works by limiting the damage in the visit-all-the-rows
approach.  Hans didn't do that in his example, but I wonder how much
it'd help (and whether the existing GIST support is adequate for it).
        regards, tom lane


pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name
Next
From: Merlin Moncure
Date:
Subject: Re: WIP: Allow SQL-language functions to reference parameters by parameter name