mysql
有A,B兩個表,
A表是所有客戶端的日誌,數量在兩百萬
B表是客戶端明細,數量在兩萬
現在需要篩選出符合某些條件的客戶端的日誌,SQL如下:
<code>SELECT A.*FROM `VIEW_DATA.basic_LOG.20160523` AINNER JOIN (SELECT AGT_ID FROM VIEW_AGENT where AGT_GRP_ID in (999)) B ON A.`BAS_AGT_ID` = B.AGT_IDORDER BY `BAS_TIME` DESC, `ID` DESC LIMIT 7;</code>
1 SIMPLE basic_log index IX_BASIC_LOG_BAS_AGT_ID IX_BASIC_LOG_BAS_TIME_ID 10 7 100 Using whereS_AGT_ID IX_BASIC_LOG_BAS_TIME_ID 10 7 100 Using where
1 SularLEd eq_ref 1 100
1 SIMPLE b eq_ref PRIMARY PRIMARY 4 ocular3.a.AGT_GRP_ID 1 100 Using where; Using index
1.將AGT_GRP_ID in (999)改為AGT_GRP_ID in =999;
2.在需要的欄位建立索引
參考自:
MySQL如何最佳化巢狀Join http://www.data.5helpyou.com/article272.html
兩張表反過來寫,然後把內鏈接改為外鏈接,將AGT_GRP_ID in (999)改為AGT_GRP_ID in =999;
SELECT A.*,B.AGT_ID
FROM VIEW_DATA.basic_LOG.20160523 A ,VIEW_AGENT B where B.AGT_GRP_ID=999 and A.BAS_AGT_ID = B.AGT_ID
ORDER BY A.BAS_TIME DESC, A.ID DESC LIMIT 7; 你改成這樣試試