Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql spatial data speed

Tags:

mysql

spatial

I have a table with (among others) x and y fields of SMALLINT type and pt of POINT type, set to POINT(x,y);
x and y have normal indecies and pt has a spatial index set.

Profiling typical query

select sql_no_cache
    count(0) from `table_name`
    where (x between -50 and 50)
    and (y between -50 and 50);
-- vs
set @g = GeomFromText('Polygon((-50 -50, 50 -50, 50 50, -50 50, -50 -50))');
select sql_no_cache
    count(0) from `table_name`
    where MBRContains(@g, `pt`);

... shows that query via x and y is 1.5 times faster:
3.45±0.10ms vs 4.61±0.14ms over 10 queries.
x and y would always be INT and only rectangular (even square) areas would be queried. Yes, this is carved in stone ;-)

The main question is:
Have I missed something about indecies or is spatial data an overkill in such case?

  • MySQL version is 5.1.37
  • DB Engine type is MyISAM (default)
  • Current table size is 5k rows, 10-30k planned in production.

I have had some experience with MySQL, but never worked with spatial data types and spatial indecies.

like image 902
kirilloid Avatar asked Dec 08 '25 23:12

kirilloid


1 Answers

Do you have a combined x & y INDEX on the table? If so then yes, this is extremely fast. I believe Spatial indexes have more broad use. A polygon structure can have many vertices and the rectangle is a single case of a more generic construct.

If a rectangular boundary area is enough for your needs then I would rather suggest you go with the x and y fields solution than adding the complexity of the geospatial extension features.

like image 155
georgepsarakis Avatar answered Dec 11 '25 22:12

georgepsarakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!