Home > Database > Mysql Tutorial > body text

mysql-MySQL two table association query is slow

WBOY
Release: 2016-08-20 08:47:56
Original
1783 people have browsed it

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>
Copy after login
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

Reply content:

1.Change AGT_GRP_ID in (999) to AGT_GRP_ID in =999;

2. Create an index on the required fields

Reference from:

How to optimize nested Join in MySQL http://www.data.5helpyou.com/article272.html

Write the two tables in reverse, then change the internal link to an external link, and change AGT_GRP_ID in (999) to 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; Try changing it to this

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template