Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poor SQL for nearest neighbor join #343

Open
pramsey opened this issue Dec 5, 2017 · 0 comments
Open

Poor SQL for nearest neighbor join #343

pramsey opened this issue Dec 5, 2017 · 0 comments

Comments

@pramsey
Copy link

pramsey commented Dec 5, 2017

The closest.js implementation does some potentially very bad stuff in finding the nearest neighbors,

' SELECT * FROM (',
' SELECT *,',
' row_number() over(',
' {{? it.category !== null}}PARTITION BY {{=it.category}}{{?}}',
' ORDER BY source.the_geom <-> the_geom',
' ) AS ranking',
' FROM target',
' ) AS ranked',
' WHERE ranking <= {{=it.responses}}',

Running a ORDER BY <-> without a LIMIT will sort the whole relation for you. Then you number the rows and take the top N. Then because you're in a LATERAL CROSS JOIN you do it again. And again. And again.

It might be possible to do this while retaining the "top N by category" behaviour, but a short-term hack could just switch between a simpler formulation using SELECT * FROM foo ORDER BY __ <-> __ LIMIT <n> for the case where not category behaviour is requested, which will be much faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant