Treating the database as not just dumb storage
You should avoid treating your database as just a place to store and retrieve your data, as a good relational database offers so much more.
In general, retrieving rows from a database table, performing operations on each row, and saving the modified row back to the database is relatively slow. If it is possible, it is much faster to write a database function for performing the same operation, and issue a single UPDATE
statement to update all rows at once. This is usually at least one order of magnitude faster, and sometimes multiple orders of magnitude faster.
Even in cases where you aren't updating data, you can use database operations to improve performance. For example, let's say you have a table with first names and last names, and you want to return a single string combining the first and last names. Do the concatenation in the database via a query such as this:
# SELECT first_name || ' ' || last_name...