I have a problem where I have a lot of data to graph. The chart is setup such that initially only a small number of points are plotted, but you can “zoom” in to get more points for a specific date range (this happens via an AJAX request to a Java backend)
I have thought of two approaches and would love some feedback as to which might be more efficient:
- Simply perform a new sql query for each request with the date range in question
OR - Preload all the records for all date ranges into ORM classes within my Java framework. Add these to a binary search tree. Then when the user makes a request, simply perform the binary search.
I assume the database has some kind of tree-based indexing already, but with the second approach I could avoid the overhead of the repeated db calls. Thanks for any help!
The best way to see is to profile both solutions, but my instinct is that your DBMS will do this much fast than you can do it. DBMS’s are designed specifically to handle queries like this very very fast. Their optimizers are probably better than what you would implement in Java.
This is assuming your DB is properly configured and optimized to handle queries like the ones you described. I suspect that the cost of repeated DB calls would not be enough to offset the DBMS performance.
Letting the DMBS do this search is also cleaner, since it minimizes the code you need to write yourself.
Again, the best way to know for sure is to quickly prototype both solutions and profile.