优化内部联接SQL查询

时间:2017-03-06 15:26:31

标签: mysql sql

我想在我的数据库中搜索最接近lat-long& amp;的城市。范围。为此,我使用此查询:

SELECT ville_nom_reel FROM inspitravel.villes_france_free 
WHERE ville_latitude_deg BETWEEN 48.0462165495 AND 48.9471174505 
AND ville_longitude_deg BETWEEN -0.45045045045 AND 0.45045045045

此查询以0.0123秒执行。

现在我想在另一个数据库中搜索,并从1st Query中的 ville_nom_reel 字段中检索另外5个字段。为此,我使用了INNER JOIN:

SELECT villes_france_free.ville_nom_reel, villes_booking.full_name, villes_booking.number_of_hotels, villes_booking.deeplink  FROM inspitravel.villes_france_free 
INNER JOIN inspitravel.villes_booking ON villes_france_free.ville_nom_reel = villes_booking.full_name 
WHERE ville_latitude_deg BETWEEN 48.0462165495 AND 48.9471174505 
AND ville_longitude_deg BETWEEN -0.45045045045 AND 0.45045045045 
AND ville_population_2012 > 3000 Limit 1

现在这个查询在4秒内完成......
如何优化此功能? 谢谢。

2 个答案:

答案 0 :(得分:0)

您可以尝试首先评估纬度/经度?也许:

SELECT  * 
FROM    (
        SELECT  *
        FROM    inspitravel.villes_france_free 
        WHERE   ville_latitude_deg BETWEEN 48.0462165495 AND 48.9471174505 
                AND ville_longitude_deg BETWEEN -0.45045045045 AND 0.45045045045 
        ) villes
JOIN    inspitravel.villes_booking 
ON      villes.ville_nom_reel = villes_booking.full_name 
WHERE   ville_population_2012 > 3000
LIMIT   1

答案 1 :(得分:0)

感谢您的帮助。
首先我删除了*所有我需要的字段名称。谢谢@apomene

SELECT villes_france_free.ville_nom_reel, villes_booking.full_name, villes_booking.number_of_hotels, villes_booking.deeplink  FROM inspitravel.villes_france_free 
INNER JOIN inspitravel.villes_booking ON villes_france_free.ville_nom_reel = villes_booking.full_name 
WHERE ville_latitude_deg BETWEEN 48.0462165495 AND 48.9471174505 
AND ville_longitude_deg BETWEEN -0.45045045045 AND 0.45045045045 
AND ville_population_2012 > 3000 Limit 1

然后,就像你说的那样,我必须参与ON条款中的索引列。
谢谢@scaisEdge 现在这个查询在0.100秒内执行!完美。