gpt4 book ai didi

mysql - 从多个表中选择更新表

转载 作者:行者123 更新时间:2023-11-30 23:27:46 26 4
gpt4 key购买 nike

这个更新语句太慢了

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;

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

最佳答案

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

这将使 mysql 投入更多的线程来查找您想要的所有数据,然后您将在线程完成时争夺更新的锁。

关于mysql - 从多个表中选择更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12337871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com