I have following tables have multiple records each (~50k) and the tables are growing.
Table1
BatchID ID Record1
Table 2
BatchID ID Record2
Table 3
BatchID ID Record3
Table 4
BatchID ID Record4
The following query takes forever to run (as the join is the cartisian product of the four tables).
Select table1.batchid,
table1.ID,
table1.Record1,
table2.Record2,
Table3.Record3,
Table4.Record4
from Table1 JOIN Table 2
on table1.batchID = table2.batchID and table1.ID = table2.ID
JOIN table3
on table1.BatchID=table3.batchID and table1.ID = table3.ID
JOIN table4
on table1.ID = table4.ID and table1.batchID = table4.batchID
What should be the best way to do this.
You should add indexes with both columns you are using in the ON
clauses. For example:
ALTER TABLE `table2` ADD INDEX `IDX_batchid-ID` (`batchid`, `ID`);
The first thought is that you want a composite index on (batchid, id) on all four tables. This will best match the join condition in the query.
How many records in the tables match each other with the same batchid and id? There may be other ways to phrase the query.
Your join looks fine. Try creating an index on the combination of BatchID and ID.
Do you actually need all these JOINs? Can't you just merge these four tables together?
For example:
CREATE TABLE MergedTable (
BatchID INT,
ID INT,
Record1 ... ,
Record2 ... ,
Record3 ... ,
Record4 ... ,
PRIMARY KEY (BatchID, ID)
)
And then:
INSERT INTO MergedTable <your query>
You can now SELECT directly from MergedTable, without need for any JOINs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.