第一批問題被認為與另一篇文章太相似(事實並非如此)並被關閉...所以我在這裡進行了另一次嘗試,希望這次措辭更好。
一些背景:這裡我們有關係。 證券(硬幣) 貨幣(基礎幣)形成一對。 一對和經紀人形成資產(我將每個經紀人/對視為特定資產)
我還有一個外匯表,用於儲存即時貨幣匯率。
在我的交易歷史表(history_price)中,有一列包含大量 NULL(歐元交易量),我根據交易量進行計算。我想做的就是填充它,為此我必須(對於每個 NULL 行):
以下是我對查詢進行分段的方式:
SELECT `history_price`.`param_basecoin`.`Symbol` FROM `history_price`.`param_asset` INNER JOIN `param_pair` ON `history_price`.`param_asset`.`id_pair` = `history_price`.`param_pair`.`pair_id` INNER JOIN `history_price`.`param_basecoin` ON `history_price`.`param_pair`.`Coin2_id` = `history_price`.`param_basecoin`.`basecoin_id` WHERE `history_price`.`param_asset`.`Ticker` LIKE '???'
SELECT `Rate` FROM `history_price`.`param_forex` WHERE `Coin` LIKE '???' AND `Basecoin` LIKE 'EUR'
UPDATE `history_price` SET `history_price`.`eur_v` = (`history_price`.`Basecoin_v` * ???) WHERE `history_price`.`eur_v` IS NULL
UPDATE `history_price` SET `history_price`.`eur_v` = (`history_price`.`Basecoin_v` * (SELECT `Rate` FROM `history_price`.`param_forex` WHERE `Coin` LIKE '???' AND `Basecoin` LIKE 'EUR')) WHERE `history_price`.`eur_v` IS NULL
UPDATE `history_price` SET `history_price`.`eur_v` = (`history_price`.`Basecoin_v` * ( SELECT `Rate` FROM `history_price`.`param_forex` WHERE `Coin` LIKE ( SELECT `history_price`.`param_basecoin`.`Symbol` FROM `history_price`.`param_asset` INNER JOIN `param_pair` ON `history_price`.`param_asset`.`id_pair` = `history_price`.`param_pair`.`pair_id` INNER JOIN `history_price`.`param_basecoin` ON `history_price`.`param_pair`.`Coin2_id` = `history_price`.`param_basecoin`.`basecoin_id` WHERE `history_price`.`param_asset`.`Ticker` LIKE `history_price`.`Ticker` ) AND `Basecoin` LIKE 'EUR' ) ) WHERE `history_price`.`eur_v` IS NULL;
它確實有效,但是太慢了... 有沒有辦法優化它並使其更快?
P粉1059715142023-09-13 00:06:37
使用JOIN
而不是相關子查詢。
UPDATE history_price AS hp JOIN param_asset AS pa ON pa.Ticker = hp.Ticker JOIN param_pair AS pp ON pp.id_pair = pa.id_pair JOIN param_basecoin AS pb ON pb.basecoin_id = pp.Coin2_id JOIN param_forex AS pf ON pf.Coin = pb.Symbol SET hp.eur_v = hp.Basecoin_v * pf.Rate WHERE pf.Basecoin = 'EUR' AND hp.eur_v IS NULL