'Which is faster in MySQL? Joining 2 tables or calling table2 on iterative result row of table1?
Hi please suggest to me which approach is better to use while retrieving data from two tables.
1.
List<xObjectType> result1 = DB.Query("SELECT * FROM table1");
for(int i = 0; i < result1.size(); i++){
xObjectType tempObj = result1.get(0);
yObjectType result2 = DB.Query("SELECT * from table2 WHERE table2.z = tempObj.x");
// use tempObj and tempObj and use them.
}
List<xyCustomObjectType> result = DB.Query("SELECT * FROM table1 JOIN table2 WHERE table1.x = table2.z");
for(int i = 0; i < result.size(); i++){
xyCustomObjectType tempObj = result.get(0);
// use tempObj and write business logic.
}
ResultSet rSet = DB.executeQuery("SELECT * FROM table1 JOIN table2 WHERE table1.x = table2.z");
while(rSet.next()){
xyCustomObjectType tempObj = result.get(0);
// Fetch the results and perform operations
}
Solution 1:[1]
It is best to use a single custom query to retrieve data from the database
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pawan.Java |