MySQL内连接在大表上获得max(date)效率太低

时间:2018-03-28 18:59:09

标签: mysql database

物业(房屋)表:2200万行 - EPC表:1400万行

这两个表都有重复的房子,INSPECTION_DATE不同。

以下声明正常,但结果包括重复的房屋。我只想退回最新的房子(最多(INSPECTION_DATE))。

SELECT 
    property.paon,
    property.saon,
    property.street,
    property.postcode,
    property.lastSalePrice,
    DATE(property.lastTransferDate),
    epc.ADDRESS1,
    epc.POSTCODE,
    epc.TOTAL_FLOOR_AREA,
    epc.INSPECTION_DATE,
    GLENGTH(LINESTRINGFROMWKB(LINESTRING(GEOMFROMTEXT(ASTEXT(POINTFROMWKB(POINT(longitude, latitude)))),
                            GEOMFROMTEXT(ASTEXT(POINTFROMWKB(POINT(-2.13609, 53.61405))))))) * 100 AS distance
FROM
    property property
        INNER JOIN
    epc ON property.postcode = epc.POSTCODE
        AND CONCAT(property.paon, ', ', property.street) = epc.ADDRESS1 
WHERE
    property.lastTransferDate >= CURRENT_DATE() - INTERVAL 5 YEAR

HAVING distance < 1.2
ORDER BY property.lastTransferDate DESC
LIMIT 10

我在类似问题(SQL select only rows with max value on a column)的帮助下尝试了以下声明,但我并不完全确定我是否已正确实施。

希望有人帮助我完成我的陈述,以便我能够识别并更好地理解我的用例

    SELECT 
    property.paon,
    property.saon,
    property.street,
    property.postcode,
    property.lastSalePrice,
    DATE(property.lastTransferDate),
    epc.ADDRESS1,
    epc.POSTCODE,
    epc.TOTAL_FLOOR_AREA,
    GLENGTH(LINESTRINGFROMWKB(LINESTRING(GEOMFROMTEXT(ASTEXT(POINTFROMWKB(POINT(longitude, latitude)))),
                            GEOMFROMTEXT(ASTEXT(POINTFROMWKB(POINT(-2.13609, 53.61405))))))) * 100 AS distance
FROM
    property property
        INNER JOIN 
    epc ON property.postcode = epc.POSTCODE
        AND (select max(epc.INSPECTION_DATE) from epc WHERE CONCAT(property.paon, ', ', property.street) = epc.ADDRESS1)
WHERE
    property.lastTransferDate >= CURRENT_DATE() - INTERVAL 5 YEAR 

HAVING distance < 1.2
ORDER BY property.lastTransferDate DESC
LIMIT 10

说明问题的示例结果: Statement Result Img

0 个答案:

没有答案