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;

我需要通过从多个表中选择值来更新单个表。现在第一个太慢的选项可以正确执行,但效率不高。我一直在阅读连接更有效,实际上这个查询是从我以前的游标迁移过来的,而且太慢了。

4

1 回答 1

0

这似乎是一个艰难的过程。我可能会尝试将这个更新分解成几个更新语句。我可能会对每个源表(inx、ccyusd 等)进行一次更新查询。然后,并行发出查询。

这将让 mysql 专用更多线程来查找您想要的所有数据,然后随着线程完成,您将有更新的锁争用。

于 2012-09-11T14:39:21.423 回答