For the below queries I already have a spatial index and indexes on the primary key (ID). Other than that I would like to know how I should set my indexes to make the below queries fast. In some cases the queries are very slow (more than 3 seconds). I'm only using SELECT statements on the tables which need to be fast, so it is not a problem in INSERT is taking longer.
I have two different queries in use.
QUERY 1:
SELECT *
FROM B
LEFT JOIN A AS C
ON B.id = C.id
LEFT JOIN A AS D
ON B.arid = D.id
LEFT JOIN E
ON B.opid = E.id
WHERE C.type='aa' AND D.type='aa'
AND
MBRWithin(C.lat_lng_point, GeomFromText('Polygon((57.5708387771 -6.08774442528, 54.3332809958 -6.08774442528, 54.3332809958 -0.305217949274, 57.5708387771 -0.305217949274, 57.5708387771 -6.08774442528))'))
AND
MBRWithin(destinationsto.lat_lng_point, GeomFromText('Polygon((50.4721888907 -0.111395950546, 47.2346311093 -0.111395950546, 47.2346311093 4.80899595055, 50.4721888907 4.80899595055, 50.4721888907 -0.111395950546))'))
LIMIT 0, 50
QUERY 2:
SELECT *
FROM (
SELECT (GLength(
LineStringFromWKB(
LineString(
lat_lng_point,
GeomFromText('POINT(55.9520598864937 -3.19648118727903)')
)
)
))
AS distance
FROM (
SELECT * FROM A
WHERE MBRWithin(lat_lng_point, GeomFromText('Polygon((56.2218563683 -3.67835839361, 55.6822634047 -3.67835839361, 55.6822634047 -2.71460398094, 56.2218563683 -2.71460398094, 56.2218563683 -3.67835839361))'))
AND A.type = 'rr'
) AS A
LEFT JOIN B
ON A.id = B.id
ORDER BY distance ASC, main DESC
) AS t
GROUP BY trid
ORDER BY distance ASC
LIMIT 30
My question is: How do I set my table indexes for the queries above?