This is a simple database optimization many people overlook.

One of the most common causes of poor application performance is poorly designed database queries. When a software routine accesses a database, it typically has to send a message over the network to another server, which in turn has to access physical disk drives using a large number of operations. Before solid-state drives, this was even slower, involving small mechanical arms and rotating disks.
This problem can become thousands or millions of times worse if database actions are performed inside loops. Many inexperienced programmers make the mistake of collecting data from multiple tables by performing actions in loops. In most cases, however, a well-designed SQL query can join all of the necessary tables in a single, smart query.
So the key thing to remember here is that you should not put database queries, inserts, updates, or deletes in loops. Also make sure your ORM is not inadvertently creating loops, either.
Make this a single database update or request where possible.