2016-03-02 100 views
0
LEFT JOIN (SELECT xx_coinType AS yy_type, xx_sell AS sell_xx 
      FROM xxs 
      ORDER BY xx_id DESC) AS xxs 
       ON xxs.yy_type = zz.zz_coin 
LEFT JOIN (SELECT xx_coinType AS yy_type, xx_sell AS old_sell_xx 
      FROM xxs 
      WHERE xx_time < UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 24 HOUR)) 
      ORDER BY xx_id DESC) AS old_xxs 
       ON old_xxs.coin_type = zz.zz_coin 

所以我有兩個左連接在我的查詢從同一表中選擇,但與一個在哪裏。由於我從查詢中選擇的同一個表格超慢。我怎樣才能解決這個問題?左連接同一個表兩次慢查詢

+0

「因爲它我從查詢選擇相同的表是超慢」 - 我不知道什麼是「加入同一個表「和」查詢超慢「必須相互處理 - 也許你在連接列上沒有索引? – Smutje

+1

'派生表中的ORDER BY'?奇怪... – dnoeth

+0

你可以CTE你加入的桌子和加入CTE。 –

回答

1

首先,不需要在派生表中使用order by子句。
其次,你的第一個派生表只是選擇表中的所有記錄,所以我沒有看到它的重點。

試試這個:

LEFT JOIN xxs 
       ON xxs.yy_type = zz.zz_coin 
LEFT JOIN (SELECT xx_coinType AS yy_type, xx_sell AS old_sell_xx 
      FROM xxs 
      WHERE xx_time < UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 24 HOUR)) 
      ) AS old_xxs 
       ON old_xxs.coin_type = zz.zz_coin 

或者這樣:

LEFT JOIN (SELECT xx_coinType AS yy_type, xx_sell AS sell_xx 
      FROM xxs 
      WHERE xx_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 24 
      ) AS xxs 
       ON xxs.yy_type = zz.zz_coin 
LEFT JOIN (SELECT xx_coinType AS yy_type, xx_sell AS old_sell_xx 
      FROM xxs 
      WHERE xx_time < UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 24 HOUR)) 
      ) AS old_xxs 
       ON old_xxs.coin_type = zz.zz_coin