首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过从多个表中选择来更新表

通过从多个表中选择来更新表
EN

Stack Overflow用户
提问于 2012-09-09 17:10:58
回答 1查看 574关注 0票数 1

此update语句太慢

代码语言:javascript
复制
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;

当我尝试使用下面的左连接时,只更新了几行。如何使用左连接?

代码语言:javascript
复制
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;

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

EN

回答 1

Stack Overflow用户

发布于 2012-09-11 22:39:21

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

这将使mysql专门使用更多的线程来查找您想要的所有数据,然后当线程完成时,您将面临更新的锁争用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12337871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档