2012-12-21 101 views
2

我有這個查詢包含一個dblink,因爲我需要連接到另一個數據庫,它似乎很慢(只有124記錄50.343秒)。有什麼辦法可以讓它變快嗎?下面是代碼:postgresql慢查詢(dblink和內部聯接)

select * 
from 
    customer 
    INNER JOIN 
    dblink('host=192.168.3.9 dbname=db2 user=postgres password=postgres', ' 
     SELECT 
      status, 
      last_churn_1, 
      attempts, 
      last_dialed, 
      lead_id, 
      date_added 
     FROM campaign_customer 
     ') AS table2 (
      status char(50), 
      last_churn_1 char(50), 
      attempts int, 
      last_dialed char(250), 
      lead_id char(8), 
      date_added char(50) 
     ) ON customer.phone1 = table2.last_dialed 
where customer.leadid = '3434' and table2.lead_id='3434' 
+1

你能告訴'解釋這個查詢ANALYZE'? –

+2

儘可能地將where子句移入dblink查詢中,否則執行程序將檢索整個遠程表。也可能你的'table2'應該是's'? –

+0

@DanielVérité可以試試看。 –

回答

3

丹尼爾·建議:

select * 
from 
    customer 
    INNER JOIN 
    dblink('host=192.168.3.9 dbname=db2 user=postgres password=postgres', $$ 
     SELECT 
      status, 
      last_churn_1, 
      attempts, 
      last_dialed, 
      lead_id, 
      date_added 
     FROM campaign_customer 
     where lead_id='3434' 
     $$) AS table2 (
      status char(50), 
      last_churn_1 char(50), 
      attempts int, 
      last_dialed char(250), 
      lead_id char(8), 
      date_added char(50) 
     ) ON customer.phone1 = table2.last_dialed 
where customer.leadid = '3434' 
+0

謝謝。這一個工程:D –