This is question related to question here, my question is rather from the perpective of Java application. How to optimally query the database, if I have a webservice that returns data from a database depending on what client is allowed to get?
- does it make sense to do a select with the maximal set of columns and filter values in Java code?
- or is it better to construct many SQL PreparedStatements/NativeQueries, each would ask only for columns which will be really returned?
- is a good compromise to construct basic queries determined only by joined tables and not by selected columns (these would be all)?
What would be good both for database and maintainable application? What I know at this time:
- most JDBC drivers/SQL databases optimize the SQL query and caches optimized version for future requests. Every new query must be analysed and compared, optimized version must be found or created ... and hold in memory ... in worst case every new variant will produce new optimized version. Will be the optimized version same for these simple selects?
select name, surname from person
select name from person
- writing many variants of selects is ugly - every variant must be tested, maintained, fast ...