I have an application written in Java where I am accessing data from an SQL database. In a few particular scenarios I have the ability to Query the database for information or I could write code in java to obtain the data i need based off of objects that are in scope. My question is this, is it more efficient to use queries when possible or write java code to achieve what is needed. Maybe a specific scenario would help.
I have a scenario where I query the database in order to get a list. After that i write some JAVA code to select only one of those particular objects from the list. In this scenario i used java to establish the object i needed.
An alternative would be to query the database and in my query join two tables together and only return the one object that is needed. In this scenario I would use a Querying language to establish the object i needed.
Any Tips or advice would be appreciated thank you.
Think this way.
SQL queries are much closer to datathan java code. Also java needs to load all the data to perform filtering. If you can write efficient SQL, SQL is always better than Java.Java filtering is preferable in the scenarios where lot of computation and business logic is involved. I personally prefer writing complex business logic in Java when compared to database.