Home:ALL Converter>Database Data Filtering Best Practice

Database Data Filtering Best Practice

Ask Time:2014-06-11T22:44:24         Author:Giles Thompson

Json Formatter

I am currently using raw JDBC to query records in a MySql database; each record in the subsequent Resultset is ultimately extracted, placed in a domain specific model, and stored to a List Instance.

My query is: in circumstances where there is a requirement to further filter that data (incidentally based on columns that exist in the SAME Table) which of the following approaches would generally be considered best practice:

1.The issuance of further WHERE clause calls into the database. This will effectively offload the filtering process to the database but obviously results in an additional query or queries where multiple filters are applied consecutively.

2.Explicitly filtering the aforementioned preprocessed List at the Application level, thus negating the need to have to make additional calls into the database each time the records are filtered.

3.Some hybrid combination of the above two approaches, perhaps where all filtering operations are initially undertaken by the database server but THEN preprocessed to a application specific model and implicitly cached to a collection for some finite amount of time. Further filter queries, received within this interval, would then be serviced from the data stored in the cache.

It is important to note that the Database Server in this scenario is actually located on an external machine, therefore the overhead and latency of sending query traffic over the local network also has to be factored into the approach we ultimately elect to take.

I am patently aware of the age-old mantra that stipulates that: "The database server should be used to do what its good at." however in this scenario it just seems like a less than adequate solution to be making numerous calls into the database to filter data that I ALREADY HAVE at the application level.

Your thoughts and insights would be greatly appreciated.

Author:Giles Thompson,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24165920/database-data-filtering-best-practice
Jeff Miller :

I have used the hybrid approach on many applications with good results. \n\nDatabase filtering works good especially for columns that are indexed. This reduces network overhead since fewer rows are sent to application.\n\nDatabase filtering can be really slow for some columns depending upon the quantity of rows in the results and the lack of indexes. The network overhead can be negligible compared to database query time so application filtering may be faster for this situation.\n\nI also find that application filtering in Java easier to write and understand instead of complex SQL.\n\nI usually experiment manually to get the fewest rows in a reasonable time with plain SQL. Then write Java to refine to the desired rows. ",
2014-06-11T15:37:39
yy