I have a queries like below. Can these be optimized or can someone suggest a how to index?
SELECT t1.`Geo ID`,
t1.`Sub Unit Geo ID`,
t1.`Sub Unit SW ID`,
t1.`Building No`,
t1.`Building Name`,
t1.Road,
t2.ID `Matching NEW_ID`,
t2.Sub,
t2.SUB_BUILDING_NAME,
t2.BUILDING_NAME,
t2.BUILDING_NUMBER,
t2.THOROUGHFARE,
t2.E - t1.Easting `East Difference`,
t2.N - t1.Northing `North Difference`
FROM upcdata t1 JOIN
newer t2 ON (t2.E * 1000) BETWEEN t1.Easting - 25000 AND t1.Easting + 25000
AND (t2.N * 1000) BETWEEN t1.Northing - 25000 AND t1.Northing + 25000
AND t1.Road = t2.THOROUGHFARE
AND t1.`Building Name` = t2.BUILDING_NAME
AND t1.`Building Name` <> ""
ORDER BY t1.`Geo ID`
Indexes aren’t used for calculated values, but you can alter your query so solve that!
Change your
betweento put the multiplication on the values:The comparison is mathematically identical, but it means the between has 2 fixed values to look up on, rather than having to multiply the t2 values for every row and not use an index because of it.
Even without indexes, just avoiding millions of computations will make it much faster