Home >Database >Mysql Tutorial >mysql-MySQL two table association query is slow
mysql
There are two tables A and B.
Table A is the logs of all clients, and the number is two million.
Table B is the client details, and the number is 20,000. Now we need to filter out the clients that meet certain conditions. Log, SQL is as follows:
<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 where
1 SIMPLE a eq_ref PRIMARY PRIMARY 4 ocular3_data.20160523.basic_log.BAS_AGT_ID 1 100
1 SIMPLE b eq_ref PRIMARY PRIMARY 4 ocular3.a.AGT_GRP_ID 1 100 Using where; Using index
2. Create an index on the required fields
How to optimize nested Join in MySQL http://www.data.5helpyou.com/article272.html
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; Try changing it to this