MySql left join 多表連接查詢優化語句


 

先過濾條件然后再根據表連接 同時在表中建立相關查詢字段的索引這樣在大數據多表聯合查詢的情況下速度相當快

 

 創建索引:

create index ix_register_year ON dbo.selects(register_year)

復制代碼

SELECT M.*,SS.SensorCode,SS.SensorStatus,SS.ManufacturerId,SS.Electricity,
SS.Voltage,SS.MinElectricity,SS.MinVoltage,SS.Temperature,SS.StatusUpdteDate,SS.UpdateStatus ,tp.PricingStrategyid,tps.FreeDuration,bat.BerthTypeId 
FROM
(SELECT T.* , BS.ParkStatus,BS.ChangeTime ,CA.CantonName, SE.SectionName
 FROM
(SELECT A.* ,B.BerthId,B.BerthCode,B.BerthAddress,B.BerthStatus,B.LineDirection,B.CantonId,B.SectionId 
FROM 
(SELECT AR.AreaId,AR.AreaCode,AR.AreaName FROM   SYS_Area  AS AR  WHERE 1=1   AND AR.AreaCode='110' ) A 
LEFT JOIN  SYS_Berth AS B ON B.AreaId=A.AreaId ) T
JOIN SYS_BerthStatus AS BS ON T.BerthCode=BS.BerthCode  
JOIN SYS_Canton AS CA ON T.CantonId=CA.CantonId  
JOIN SYS_Section AS SE ON T.SectionId=SE.SectionId )M

LEFT JOIN SYS_Sensor SS ON M.BerthCode=SS.BerthCode 
LEFT JOIN TRA_PricingBerth AS tp ON tp.BerthCode=M.BerthCode 
  
LEFT JOIN TRA_PricingStrategy  AS tps ON tps.PricingStrategyId=tp.PricingStrategyId 
LEFT  JOIN SYS_BerthAndType AS bat ON bat.BerthCode=M.BerthCode 
ORDER BY BerthCode ASC
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM