alda, (edited ) My advice about backend development:
Database operations are generally cheaper and quicker than doing things in-app.
The people who made your relational database system have probably already figured out an optimisation for whatever it is that you're attempting to do.