Currently we retrieve a List of objects from the Spring SimpleJdbcTemplate. The first query is a select * to retrieve all records that we will want to be our “keys” . This then leads to a foreach loop over the collection which in turn involves N queries back to the database where N is the size of the List. Would it be more efficient to do these N queries or do 2 large select * statements and map them through a for loop that does an if statement? See code below:
N Queries
List keyList = Select * from keyTable;
for(Object o: keyList)
{
List valueList = Select * from valueTable;
//Map valueTable to keyTable via rowmapper
}
Y Java if statements
List keyList = Select * from keyTable;
List valueList = Select * from valueTable;
for(Object o: keyList)
{
for(Object z : valueList)
{
if(z.key == o.key) //set the value
}
}
I’m not a java programmer, but wouldn’t it be simpler to have the database perform the join, since most modern database engines are designed and built to optimize the join process? Guessing at the syntax, you’d have the query
somehow within your code, perhaps like so: