1

有什么方法可以提高以下查询的准确性?

SELECT DISTINCT a.localgeo, a.foreigngeo, b.lat, b.lng, c.lat, c.lng
FROM tbl_correlateddata a
JOIN tbl_geodetail b ON a.localgeo = b.code
JOIN tbl_geodetail c ON a.foreigngeo = c.code

我正在访问带有 2 个国家/地区代码的表格

localgeo|foreigngeo
--------------------
JP      |AD
JP      |AD
US      |HK
AD      |JP
HK      |US
AD      |JP
CN      |US

并尝试用另一个表中的经度和纬度值填充它

code|lat|lng
------------
JP  |1  |2
US  |3  |3
HK  |1  |4
AD  |3  |4
CN  |4  |4

结果看起来像这样

localgeo|lat|lng|foreigngeo |lat|lng|
------------------------------------
JP      |1  |2  |AD         |3  |4
US      |3  |3  |HK         |1  |4
HK      |1  |4  |US         |3  |3
AD      |3  |4  |JP         |1  |2
CN      |4  |4  |US         |3  |3

但查询需要很多时间。有什么办法可以加快这个速度吗?

4

1 回答 1

2

问题是您对所有字段都使用了 distinct,而您没有这样做(),而且这需要很长时间。尝试仅对一列使用 distinct

像这样

    SELECT DISTINCT (a.localgeo), a.foreigngeo, b.lat, b.lng, c.lat, c.lng
    FROM tbl_correlateddata a 
    JOIN tbl_geodetail b 
      ON a.localgeo = b.code 
    JOIN tbl_geodetail c 
      ON a.foreigngeo = c.code

编辑:

     SELECT DISTINCT a,b,c FROM t

大致相当于:

     SELECT a,b,c FROM t GROUP BY a,b,c

所以你应该这样做

     SELECT a.localgeo, a.foreigngeo, b.lat, b.lng, c.lat, c.lng
    FROM tbl_correlateddata a 
    JOIN tbl_geodetail b 
      ON a.localgeo = b.code 
    JOIN tbl_geodetail c 
      ON a.foreigngeo = c.code
     GROUP BY a.localgeo ,a.foreigngeo
于 2012-12-17T08:27:38.647 回答