简体   繁体   中英

Slow ST_CONTAINS query in GEOMETRY type SPATIAL INDEX in MySQL 5.7

I have a table with spatial index. When I run this query, it takes more than 0.7 to return results. Is there a way to make this query faster?? (MySQL 5.7.17 on AWS RDS, InnoDB)

CREATE TABLE

CREATE TABLE `geofences` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `sticker_key` VARCHAR(50) NOT NULL COLLATE 'utf8mb4_bin',
    `geofence` GEOMETRY NOT NULL,
    `location` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_bin',
    `expires` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8mb4_bin',
    PRIMARY KEY (`id`),
    SPATIAL INDEX `geofence` (`geofence`)
)COLLATE='utf8mb4_bin'
ENGINE=InnoDB;

QUERY

SELECT geofences.sticker_key FROM geofences WHERE ST_CONTAINS(geofences.geofence, POINT(126.924394,37.552754)) GROUP BY sticker_key;

EXPLAIN 说明

Example Data

This is the smallest row in my geofence table.

MULTIPOLYGON(((126.982169151306 37.5796080752196,126.984980106354 37.5792084484235,126.985055208206 37.5801097313528,126.985623836517 37.5800672182523,126.985656023026 37.5802542757132,126.986804008484 37.5801947574811,126.9868683815 37.5831110948965,126.986482143402 37.5831961175971,126.986385583878 37.5830685835098,126.986138820648 37.5828475239075,126.98569893837 37.582711486903,126.985720396042 37.5819207668924,126.985355615616 37.5817762257676,126.984947919846 37.5817677233397,126.984915733337 37.5814616352904,126.9846367836 37.5814616352904,126.984508037567 37.5817082063176,126.984218358994 37.581878254826,126.983692646027 37.5820398005493,126.98362827301 37.5822608625499,126.983746290207 37.5823373838587,126.983370780945 37.5834086739237,126.983295679092 37.5834766918201,126.983370780945 37.5843269102809,126.983531713486 37.5843609188173,126.983789205551 37.5849730698168,126.982952356338 37.5850580903908,126.982877254486 37.5845139570391,126.982491016388 37.5845479654901,126.982126235962 37.5845564676004,126.982115507126 37.5846329865497,126.981954574585 37.5846329865497,126.981739997864 37.5837147539679,126.981611251831 37.5823118767645,126.981300115585 37.5819972885508,126.980999708176 37.5815041475947,126.98145031929 37.5812235659375,126.981514692307 37.581036510912,126.982115507126 37.5800162024996,126.982308626175 37.5799226735289,126.982169151306 37.5796080752196)))

Some are bigger (like entire nation and such)

Simplify your geometries!

The more complex a shape is, the longer it takes for MySQL to tell whether it contains a point or not -- and some countries have very complex shapes indeed. For example, the boundary of Indonesia (which you mentioned in a comment) contains over 18,000 points, most of which define the complex border between Sarawak and Kalimantan.

Unless your application specifically needs to know exactly where a user sits with regard to that border, you'll probably be much better off with a simpler version. There are a number of tools available for this purpose; one popular one is mapshaper .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM