2012-09-09 58 views
1

此更新語句太慢更新表中選擇多個表

UPDATE planner_ccy a 
    SET  dxy = (SELECT b.close FROM dxy b WHERE b.trade_date <= a.trade_date ORDER BY b.trade_date DESC LIMIT 1), 
     usd_rate = (SELECT c.interest_rate FROM usd_ir c WHERE c.set_date <= a.trade_date ORDER BY c.set_date DESC LIMIT 1), 
     ccy_rate = (SELECT d.interest_rate FROM ccy_ir d WHERE d.set_date <= a.trade_date ORDER BY d.set_date DESC LIMIT 1), 
     coal = (SELECT e.price FROM coal e WHERE e.reported_date <= a.trade_date ORDER BY e.reported_date DESC LIMIT 1), 
     oil = (SELECT f.price FROM oil f WHERE f.reported_date <= a.trade_date ORDER BY f.reported_date DESC LIMIT 1), 
     ump = (SELECT g.ump_rate FROM usd_ur g WHERE g.reported_month <= a.trade_date ORDER BY g.reported_month DESC LIMIT 1), 
     inx = (SELECT h.close FROM inx h WHERE h.trade_date <= a.trade_date ORDER BY h.trade_date DESC LIMIT 1)/(SELECT i.gdp FROM fed_ i WHERE i.reported_year <= a.trade_date ORDER BY i.reported_year DESC LIMIT 1), 
     total = (SELECT j.total FROM fed_ j WHERE j.reported_year <= a.trade_date ORDER BY j.reported_year DESC LIMIT 1)/(SELECT k.gdp FROM fed_ k WHERE k.reported_year <= a.trade_date ORDER BY k.reported_year DESC LIMIT 1), 
     dfn = (SELECT n.dfn FROM fed_ n WHERE n.reported_year <= a.trade_date ORDER BY n.reported_year DESC LIMIT 1)/(SELECT o.gdp FROM fed_ o WHERE o.reported_year <= a.trade_date ORDER BY o.reported_year DESC LIMIT 1), 
     tax = (SELECT p.tax_rate*p.tax_bracket FROM usd_tax p WHERE p.set_date <= a.trade_date ORDER BY p.set_date DESC LIMIT 1)/(SELECT q.gdp FROM fed_ q WHERE q.reported_year <= a.trade_date ORDER BY q.reported_year DESC LIMIT 1)/1000000000, 
     forecast = (SELECT r.close FROM ccyusd r WHERE r.trade_date <= a.trade_date ORDER BY r.trade_date DESC LIMIT MasterSkip,1) 

     WHERE a.trade_date >= Start; 

當我嘗試使用下面的左邊加入,只有幾行得到更新。我怎樣才能使用左連接?

UPDATE planner_ccy_ a 
    LEFT JOIN (SELECT * FROM dxy ORDER BY trade_date DESC LIMIT 1) AS b 
    ON b.trade_date <= a.trade_date 

    LEFT JOIN (SELECT * FROM usd_ir ORDER BY set_date DESC LIMIT 1) AS c 
    ON c.set_date <= a.trade_date 

    LEFT JOIN (SELECT * FROM ccy_ir ORDER BY set_date DESC LIMIT 1) AS d 
    ON d.set_date <= a.trade_date 

    LEFT JOIN (SELECT * FROM coal ORDER BY reported_date DESC LIMIT 1) AS e 
    ON e.reported_date <= a.trade_date 

    LEFT JOIN (SELECT * FROM oil ORDER BY reported_date DESC LIMIT 1) AS f 
    ON f.reported_date <= a.trade_date 

    LEFT JOIN (SELECT * FROM usd_ur ORDER BY reported_month DESC LIMIT 1) AS g 
    ON g.reported_month <= a.trade_date 

    LEFT JOIN (SELECT * FROM inx ORDER BY trade_date DESC LIMIT 1) AS h 
    ON h.trade_date <= a.trade_date 

    LEFT JOIN (SELECT * FROM fed_ ORDER BY reported_year DESC LIMIT 1) AS i 
    ON i.reported_year <= a.trade_date 

    LEFT JOIN (SELECT * FROM usd_tax ORDER BY set_date DESC LIMIT 1) AS j 
    ON j.set_date <= a.trade_date 

    LEFT JOIN (SELECT * FROM ccyusd ORDER BY trade_date DESC LIMIT MasterSkip,1) AS k 
    ON K.trade_date <= a.trade_date 

    SET  a.dxy = b.close, 
     a.usd_rate = c.interest_rate, 
     a.ccy_rate = d.interest_rate, 
     a.coal = e.price, 
     a.oil = f.price, 
     a.ump = g.ump_rate, 
     a.inx = h.close /i.gdp, 
     a.total = i.total/i.gdp, 
     a.dfn = i.dfn/i.gdp, 
     a.tax = j.tax_rate*j.tax_bracket/i.gdp/1000000000, 
     a.forecast = K.close 

    WHERE a.trade_date >= Start; 

我需要通過從多個表中選擇值來更新單個表。現在,第一個選項太慢,可以正確執行,但效率不高。我一直在閱讀,認爲連接更有效率,實際上這個查詢是從我以前的遊標遷移過來並且速度太慢。

回答

0

這似乎是一個艱難的。我可能會試着將這個更新分解成幾個更新語句。我可能會爲每個源表(inx,ccyusd等)進行一次更新查詢。然後,並行發出查詢。

這將讓MySQL專用更多線程來查找所需的所有數據,然後在線程完成時您將獲得更新的鎖定爭用。