Keep out of the loop.

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: