Joins on spatial mysql indexes

后端 未结 4 2030
攒了一身酷
攒了一身酷 2021-02-08 03:59

I have two tables: one with points, the other with polys.

CREATE TABLE `points` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `point` point NOT NULL,
  PR         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 04:36

    You can force MySQL to use the index by encapsulating the subselect in a function.

    For example:

    DELIMITER $$
    
    DROP FUNCTION IF EXISTS `GetMyPolygon`$$
    CREATE DEFINER=`root`@`localhost` FUNCTION `GetMyPolygon`(p POINT) RETURNS INTEGER
    BEGIN
    
    DECLARE ret INTEGER;
    
    SET ret = (SELECT range_id FROM ranges WHERE ST_CONTAINS(poly, p) ;
    
    RETURN ret;
    
    END$$
    

    If polygons do not overlap, you can then make:

    SELECT *, GetMyPolygon(point) FROM points 
    

    If they do overlap, but there a few, you could make a similar function that makes group_concat...

提交回复
热议问题