Let the database work for you.
Not that this is an absolute truth. But bringing a lot of data to the filtering application is almost always a mistake. I say it almost because there can always be a scenario that I don’t know can be useful or the only viable way. But I can remember one like that in my head.
Bringing in too much data costs the database, infrastructure and application, not to mention that you’ll have an extra code to deal with.
This case is clearly best to use full SQL and bring only what you need. See if you can make the three queries in one query only. Probably can’t, because the most used technologies hinder this, but it depends a little on how everything is organized.
There may be some very specific and odd case that it would be better to bring everything, even then it would be little gain and I find something so out of the curve that I doubt it would actually happen.
Of course you need to architecture the application to work well, you need to see if the database is properly configured to support this query adequately.
There are controversies as to which limit should be passed to the database. Many scalability problems occur because the developer overloads the database with things that would look better in the application. There will always be the discussion on the use of Stored Procedures or not.
I say and I repeat, many of the techniques and tools that exist today were created to solve a problem created by another technique that was unnecessary or worse than simple. Often people ignore the Razor of Occam. Of course there will always be the argument that the more complex is doing something more. There are cases that are true, but there are cases that are just complicated by not evaluating the whole, or by liking it, or just knowing how to do it.
The "good" part is that it created a huge industry because of so much new problem. We look like hamsters on the wheel.
Related: Why using "SELECT * FROM table" is bad?.
If you think that the sales table can contain millions of records, it is clear that making the 3 selects is "cheaper".
– NilsonUehara
Thanks for the reply, it was as I imagined. Now! Please. I changed the scenario a little and I made a comment in the reply below. If possible, could you comment there as well?
– Henrique Santiago
@Henriquesantiago: Contact the database whenever possible. Generally sgbd generates optimized execution plan to filter, especially if there is index that meets the query. // In addition, it decreases network traffic. //
– José Diz
@Henriquesantiago: In some database managers it is possible to make the 3 queries in a single reading of the data, using GROUPING SETS. . The result returns as a single data set.
– José Diz
sorry but did not understand, it is a question with answer "predefined" and chosen, only to give the reward?
– Ricardo Pontual
@Augustovasques doesn’t make sense to open a reward for it when you see... the goal of a reward is to encourage a better response, so you get an extra reputation... open a Bunty and write "look it’s not for pariticpar pq this bouty is for so-and-so’s answer" I don’t think it makes sense from Bounty’s point of view. If you want to reward someone, leave it there and choose the person’s answer (in fact it was already chosen), but leaving the text like this does not seem right "with the OS community". A question is a question, anyone can answer, it’s not a private site
– Ricardo Pontual
@Augustovasques if you put your mouse on the icon that identifies an answer as an accepted answer, you will see that I accepted the answer in July 2017. This feature is Stackoverflow, even a question already with accepted answer is likely to receive more answers.
– Henrique Santiago
@Henriquesantiago It is not about your acceptance, we are discussing the reward. Rest assured.
– Augusto Vasques