L
L
l4m3r2019-06-02 14:07:32
SQL
l4m3r, 2019-06-02 14:07:32

Order by distance. How?

There is a large table of x, y, z coordinates.
Is it possible somehow to get sorted by distance by a certain point in one query (say [50, 50, 50]).
Or can the table be designed differently?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Shumov, 2019-06-02
@inoise

Make a calculated column and sort by it. The formula can be Googled for for short distances. For big ones - not just

A
Andrey Nikolaev, 2019-06-02
@gromdron

If you do not want to deal with a rather complicated mathematical calculation, then it will be easier to create a separate table with distances: from, to and distance, and then recalculate using it. Well and accordingly each time to update.
Of course, you will lose on updates, but select will be very fast.

T
TheRonCronix, 2019-06-03
@TheRonCronix

You can calculate, for this you will have to go through all the rows of the table and for each of the points calculate the distance to your point, and then sort it. But, if you need to select only a few nearest points or just points not too far from the given one, then you can use the bounding-box technique: imagine that there is a cube or square of a given size around the point and first calculate within the boundaries of the cube. This will allow you to somehow use the indexes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question