慢mysql查询,设置索引或优化

时间:2011-12-14 11:32:20

标签: mysql

我有如下查询。这些可以优化,还是有人建议如何索引?

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`

1 个答案:

答案 0 :(得分:2)

索引不用于计算值,但您可以更改查询以解决此问题! 更改您的between以将乘法放在值上:

....
join newer t2
    ON t2.E BETWEEN (t1.Easting - 25000)/1000 AND (t1.Easting + 25000)/1000
    AND t2.N BETWEEN (t1.Northing - 25000)/1000 AND (t1.Northing + 25000)/1000
....

比较在数学上是相同的,但它意味着中间有2个固定值要查找,而不是必须将每行的t2值相乘,因为它不使用索引。

即使没有索引,只需避免数百万次计算就可以加快速度