如何在MySQL中优化嵌套选择

时间:2019-01-29 15:14:48

标签: mysql query-optimization

我有一个查询,该查询在尝试运行时超时。我有一些嵌套的选择,我知道可以通过某种方式加入来更好地编写它们,但我似乎无法使其正常工作。这是我当前的查询。

SELECT `BD#` AS BreakdownNumber
   , CALL_DATE AS Date
   , TRAILER_CODE TrailerNumber
   , HUB AS Hub
   , CLOSE_DATE CloseDate
   , sbs_hdr.HRS AS Hours
   , (SELECT TOTAL FROM arinv WHERE arinv.code = (
        SELECT LIN_AR_INVOICE FROM woopnh 
        LEFT JOIN woopnl ON woopnh.`HDR_VCHR_NO` = woopnl.`LIN_VCHR_NO`
        WHERE HDR_FN_BD_NO = `bd#` AND LIN_AR_INVOICE <> 9999999 LIMIT 1)) AS Amount
   , (SELECT LIN_AR_INVOICE FROM woopnh 
      LEFT JOIN woopnl ON woopnh.`HDR_VCHR_NO` = woopnl.`LIN_VCHR_NO` 
      WHERE HDR_FN_BD_NO = `bd#` AND LIN_AR_INVOICE <> 9999999 LIMIT 1) AS InvoiceId
FROM sbs_hdr
WHERE sbs_hdr.`CUSTOMER_CODE` = '12345'

0 个答案:

没有答案