对于以下查询,我已经在主键 (ID) 上有一个空间索引和索引。除此之外,我想知道我应该如何设置我的索引以快速进行以下查询。在某些情况下,查询非常慢(超过 3 秒)。我只在需要快速的表上使用 SELECT 语句,因此 INSERT 需要更长的时间不是问题。
我有两个不同的查询在使用。
问题 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
问题 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
我的问题是:如何为上述查询设置表索引?