2016-06-07 25 views
0

我有一個PostgreSQL數據庫9.5三個表:INNER JOIN與另外兩個人一個桌子,彷彿它們是外連接

big_table: 
- geom_location 
a: 
- geom_location 
- name 
b: 
- geom_location 
- name 

的geom_location領域是已索引PostGIS的領域。 a和b表格大約爲200K行,而big_table大約爲20M行。
我需要創建產生以下結果的select語句:

  • a_name - 如果在表內的geom_location 2公里一個BIG_TABLE項,這將是其關聯的名稱。否則,它將爲空。
  • b_name - 如果表b中geom_location的2Km內有一個big_table條目,這將是它的關聯名稱。否則,它將爲空。
  • geom_location - 表a,b或兩者中的條目2Km內的geom_location。
  • 我不想找回其中a_name和b_name都爲空的任何行。

以下是接近我想要的東西,但它需要所有三個geom_locations是2公里範圍內:

SELECT t.geom_location, a.name as a_name, b.name as b_name 
FROM big_table t 
INNER JOIN a ON ST_DWithin(t.geom_location, a.geom_location, 2000) 
INNER JOIN b ON ST_DWithin(t.geom_location, b.geom_location, 2000) 

這也是八九不離十,但它並沒有合併行的路上我想:

SELECT t.geom_location, a.name as a_name, null as b_name 
FROM big_table t 
INNER JOIN a ON ST_DWithin(t.geom_location, a.geom_location, 2000) 
UNION 
SELECT t.geom_location, null as a_name, b.name as b_name 
FROM big_table t 
INNER JOIN b ON ST_DWithin(t.geom_location, b.geom_location, 2000) 

好像應該有應該有一些語法,做一個「大多是」內部連接 ​​- 也就是說,在兩個表格作出關於內內加盟的第一個表,而是彼此之間的完全結合。

回答

0

這是做你想做的嗎?

SELECT t.geom_location, a.name as a_name, b.name as b_name 
FROM big_table t LEFT JOIN 
    a 
    ON ST_DWithin(t.geom_location, a.geom_location, 2000) LEFT JOIN 
    b 
    ON ST_DWithin(t.geom_location, b.geom_location, 2000) AND a.name IS NULL 
WHERE a.name IS NOT NULL OR b.name IS NOT NULL; 

或者,你可以結合ab在一起:

SELECT t.geom_location, ab.name, ab.which 
FROM big_table t JOIN 
    ((SELECT a.name a.geom_location, 'a' as which 
     FROM a 
    ) UNION ALL 
     (SELECT a.name b.geom_location, 'b' as which 
     FROM b 
    ) 
    ) ab 
    ON ST_DWithin(t.geom_location, ab.geom_location, 2000) ; 

編輯:

其實我在想,上述方案可能有一些性能困難。它適用於簡單的數據類型,但複雜的類型總是有點麻煩。但是,你仍然可以做你想做的事。這是你的union方法的變化:

與助教( SELECT t.geom_location,a.name爲a_name,null作爲b_name FROM BIG_TABLE牛逼INNER JOIN 一個 ON ST_DWithin

(t.geom_location,一。 geom_location,2000 ) SELECT TA * 自TA UNION ALL SELECT t.geom_location,null作爲a_name,b.name如b_name FROM BIG_TABLE噸INNER JOIN b ON ST_DWithin(t.geom_location,b.geom_location ,2000) 不存在(SELECT 1 FROM ta WHERE t.geom_location = ta.geom_location );

我很確定=應該在位置列上工作。

+0

我認爲這樣做,但不幸的是在這樣的左外連接中使用ST_DWithin不會使用它的索引。 postgres對該查詢的解釋返回的成本超過2萬億! – bsellersful

相關問題