我有一個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)
好像應該有應該有一些語法,做一個「大多是」內部連接 - 也就是說,在兩個表格作出關於內內加盟的第一個表,而是彼此之間的完全結合。
我認爲這樣做,但不幸的是在這樣的左外連接中使用ST_DWithin不會使用它的索引。 postgres對該查詢的解釋返回的成本超過2萬億! – bsellersful