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 など) に対して 1 つの更新クエリを作成する場合があります。次に、クエリを並行して発行します。

これにより、必要なすべてのデータを見つけるために mysql がより多くのスレッドを専用に割り当てることができるようになり、スレッドが完了すると、更新のためのロック競合が発生します。

于 2012-09-11T14:39:21.423 に答える