SELECT * Analogy

The DBAs at the company I work for sent out a mass email this morning “reminding” developers that using SELECT * is a bad idea. One of my friends, who usually asks me about database questions was confused about one part of the email:

If implementing these changes for an existing application, the percentage decreases will be off the time to process columns and not necessarily off the total currently used by the SQL statement.

And I came up with what I thought was a pretty good analogy:

Let’s pretend the database table is a grocery store, and you need to buy ingredients so you can bake a cake.

“SELECT *” would be like taking every single product in the store home and then figuring out which ingredients you actually need to make a cake.

Selecting individual columns would be taking your recipe with you and getting only the ingredients you need.

It still might take you a while to go through the store, especially if it’s one of those huge mega-marts (a.k.a, a really big query with lots of joins) to find the ingredients you need, but only getting the products you need will make things a lot faster when you get home.

Leave a Reply

Your email address will not be published.