Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need a spatial index in my database?

I am designing an application that needs to save geometric shapes in a database. I haven't choosen the database management system yet.

In my application, all database queries will have an bounding box as input, and as output I want all shapes within that database. I know that databases with a spatial index is used for this kind of application. But in my application there will not be any queries of type "give me objects nearby x/y" or other more complex queries that are useful in a GIS application.

I am planning of having a database without a spatial index and have queries looking like:

SELECT * FROM shapes WHERE x < max_x AND x > min_x AND y < max_y AND y > min_y

And have an index on the columns x (double) and y (double). As long I can see, I don't really need a database with an spatial index, howsoever my application is close to that kind of applications.

And even if I would like to have nearby queries, then I could create a big enough bounding box around that point. Or will this lead to poor performance?

Do I really need a spatial database? And when is a spatial index needed?

EDIT: The search queries will actually be a little bit more advanced than the one I wrote above, since I deal with geometric shapes I will input an Bounding box that will return multiple shapes (with bounding box) that are inside or interfere with the box in the query. But I still think I can do this without a spatial index, after been reading all good answers.

like image 892
Jonas Avatar asked Mar 31 '10 22:03

Jonas


People also ask

What is a spatial index in database?

A spatial index is a type of extended index that allows you to index a spatial column. A spatial column is a table column that contains data of a spatial data type, such as geometry or geography.

Why do I need an index in a database?

Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records.

Why are spatial indexing capabilities important in spatial databases?

A spatial index is a data structure that allows for accessing a spatial object efficiently. It is a common technique used by spatial databases. Without indexing, any search for a feature would require a "sequential scan" of every record in the database, resulting in much longer processing time.

What is a spatial index MySQL?

SPATIAL INDEX creates an R-tree index. For storage engines that support non-spatial indexing of spatial columns, the engine creates a B-tree index. A B-tree index on spatial values is useful for exact-value lookups, but not for range scans. For more information on indexing spatial columns, see CREATE INDEX.


2 Answers

Do I really need a spatial database?

It looks like what you are doing will work fine for your application.

And even if I would like to have nearby queries, then I could create a big enough bounding box around that point.

You may want to consider creating an index on a Geohash instead. This method is recommended for indexing geo-spatial points on the Google App Engine, for example, where the indexing features are limited. (Source)

And when is a spatial index needed?

There are many scenarios where a spatial index is useful. First of all, spatial indexes can deal with not just points, but with polylines, polygons, and other shapes. In addition, as you already mentioned, there are many complex query operations that can be applied on spatial data, where a proper spatial index would be essential.

like image 129
Daniel Vassallo Avatar answered Sep 22 '22 08:09

Daniel Vassallo


No, you don't need a spatial index for this.

Spatial indices are needed for calculating distance between objects, seeing if a point is within a certain radius of another point, etc... mostly when you need to take geographical coordinates into consideration with. Southern hemisphere, northern hemisphere, etc... it all changes the distance a bit when you have to take the curve of the earth into consideration.

If you're always looking for both x and y than you would benefit from having an index on both items at the same time. So... not an index on column x and an index on column y, but an index on column x and y combined.

like image 23
Wolph Avatar answered Sep 20 '22 08:09

Wolph