Let’s say that I have a round 10000 lines in MySql database and I want to calculate multiple values(sum. median, average…).
- Which Is is faster, fetch all the 10000 with a simple SQL query and iterate over it using Java and do the calculation or perform directly SQL queries (select sum(..); select count(*)…) ? and is it possible to do only one query that returns the wanted values ?
EDIT
- Is there any metric/statistics on the subject ?
- The data can be fetched by joining 2 or 3 tables
Thanks
it depends
Most people here said it is faster because it is closer to the data etc.
However it is not necessary true.
For example, if the filtering criteria is complicated, and if number of result is close to total number of record, and if the DB machine is not super fast while your app being super slow, in such case having DB doing the simple job (getting all records) and do the work in your application can be much faster instead. (I haven’t even mentioned the low scalability of DB, which there are more scenario that may cause filtering in DB slower than in app)
So, there is no rule of thumb.
My suggestion is, put simple and straight-forward filtering/aggregation in DB, let the DB does the work it is good at. If you need extra complication calculations for filtering, do it in application code (and perform on the DB-filtered result).