Hi Michael,
An update. I restored a recent backup to a new DB server yesterday, and thought I would test out index rebuilding etc, before running a comparison test. To my surprise, the new server, which is vastly more powerful the the AWS instance used to compare against, showed little improvement, and was approx 100x slower. The same query was being used, and the same result generated. An EXPLAIN statement showed that the larger machine was not selecting the spatial index. Instead, it seemed to pick up on the final ORDER BY instead.
Forcing the query to use the spatial index did the expected, and reduced the run time from 8-9 sec to 0.1 sec. I’ve checked the settings for both DB servers, run SHOW WARNINGS after the EXPLAIN, and done an ANALYZE TABLE and EXPLAIN againt the table, there appear to be no differences.
Where else might I look to see what is causing this difference in behaviour ?
Code snippet, results and EXPLAIN follow, startpoint, endpoint, and startpointFipped are the spatial indexes.
SELECT
r0_.routeId AS routeId0,
IF(
length(r0_.startpoint) = 0,
“”,
ST_AsText(r0_.startpoint)
) AS startpoint1,
IF(
length(r0_.startpointFlipped) = 0,
“”,
ST_AsText(r0_.startpointFlipped)
) AS startpointFlipped2,
IF(
length(r0_.endpoint) = 0,
“”,
ST_AsText(r0_.endpoint)
) AS endpoint3,
r0 defs follow
FROM
route_metadata r0_
WHERE
MBRWITHIN(
r0_.startpoint,
ST_GeomFromText(
‘MULTIPOINT(50.758851 5.122327, 51.909877 6.964633)’
)
)
AND
(
r0_.owner = ‘1234567’
OR r0_.isPublic = 1
)
AND r0_.category IN (‘10’, ‘41’, ‘72’, ‘30’, ‘11’)
AND r0_.difficulty IN (0, 3, 2, 5, 1, 4)
AND r0_.length >= ‘500’
AND r0_.length < ‘1000’
AND r0_.rating >= ‘0’
AND (
r0_.owner = ‘1234567’
OR r0_.price <= ‘0’
)
ORDER BY
r0_.qualityScore DESC
LIMIT
10 OFFSET 0;
RESULT
Primary DB
±-------------±--------------------------±--------------------------±--------------------------±--------±--------±--------±----------±----------±------------------------±---------------±---------------------------±------------------------------------------------------+±-------------------------------------------------------±--------±-----------±-------------±----------±-----------+
| routeId0 | startpoint1 | startpointFlipped2 | endpoint3 | length4 | rating5 | price6 | hasImage7 | hasAudio8 | shortDescriptionLength9 | qualityScore10 | name11 | shortDescription12 || coverImage27 | owner28 | category29 | difficulty30 | surface31 | supplier32 |
±-------------±--------------------------±--------------------------±--------------------------±--------±--------±--------±----------±----------±------------------------±---------------±---------------------------±------------------------------------------------------+±-------------------------------------------------------±--------±-----------±-------------±----------±-----------+
| 841_23509889 | POINT(51.258987 6.732483) | POINT(6.732483 51.258987) | POINT(51.258987 6.732483) | 765.83 | 0 | 0.00000 | 1 | 0 | 222 | 45000 | CARAVAN SALON Route Nord 1 | CARAVAN SALON Route N1 startet in || http ://img/280/210/36868436/.jpg | 1234567 | 10 | 0 | 3 | 12600 |
±-------------±--------------------------±--------------------------±--------------------------±--------±--------±--------±----------±----------±------------------------±---------------±---------------------------±------------------------------------------------------+±-------------------------------------------------------±--------±-----------±-------------±----------±-----------+
1 row in set (9.11 sec)
AWS Instance
±-------------±--------------------------±--------------------------±--------------------------±--------±--------±--------±----------±----------±------------------------±---------------±---------------------------±------------------------------------------------------+±-------------------------------------------------------±--------±-----------±-------------±----------±-----------+
| routeId0 | startpoint1 | startpointFlipped2 | endpoint3 | length4 | rating5 | price6 | hasImage7 | hasAudio8 | shortDescriptionLength9 | qualityScore10 | name11 | shortDescription12 || coverImage27 | owner28 | category29 | difficulty30 | surface31 | supplier32 |
±-------------±--------------------------±--------------------------±--------------------------±--------±--------±--------±----------±----------±------------------------±---------------±---------------------------±------------------------------------------------------+±-------------------------------------------------------±--------±-----------±-------------±----------±-----------+
| 841_23509889 | POINT(51.258987 6.732483) | POINT(6.732483 51.258987) | POINT(51.258987 6.732483) | 765.83 | 0 | 0.00000 | 1 | 0 | 229 | 45000 | CARAVAN SALON Route Nord 1 |
CARAVAN SALON Route N1 startet in || http ://img/280/210/25277664/.jpg | 1234567 | 10 | 0 | 3 | 12600 |
±-------------±--------------------------±--------------------------±--------------------------±--------±--------±--------±----------±----------±------------------------±---------------±---------------------------±------------------------------------------------------+±-------------------------------------------------------±--------±-----------±-------------±----------±-----------+
1 row in set (0.10 sec)
EXPLAIN
Primary DB
±—±------------±------±-----------±------±------------------------------------------------------------------±-------------±--------±-----±-----±---------±------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±------±------------------------------------------------------------------±-------------±--------±-----±-----±---------±------------+
| 1 | SIMPLE | r0_ | NULL | index | rating,difficulty,price,owner,category,length,isPublic,startpoint | qualityScore | 4 | NULL | 5431 | 0.00 | Using where |
±—±------------±------±-----------±------±------------------------------------------------------------------±-------------±--------±-----±-----±---------±------------+
1 row in set, 1 warning (0.01 sec)
AWS Instance
±—±------------±------±-----------±------±------------------------------------------------------------------±-----------±--------±-----±-----±---------±----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±------±------------------------------------------------------------------±-----------±--------±-----±-----±---------±----------------------------+
| 1 | SIMPLE | r0_ | NULL | range | rating,difficulty,price,owner,category,length,isPublic,startpoint | startpoint | 34 | NULL | 2775 | 0.03 | Using where; Using filesort |
±—±------------±------±-----------±------±------------------------------------------------------------------±-----------±--------±-----±-----±---------±----------------------------+
1 row in set, 1 warning (0.01 sec)
Mike