JournalSQLQuery structure3 min

Why shouldn’t we use SELECT * queries

The common answer to why we should SELECT col1, ..., colN rather than SELECT * is that we reduce network traffic. In most cases, for reasonably normalized databases, and with today’s high-speed network connections, this is unlikely to be a huge deal.

  • SELECT *
  • covering index
  • sequential scan
  • network traffic

Control yourself; Take only what you need from it

The common answer to why we should SELECT col1, ..., colN rather than SELECT * is that we reduce network traffic. In most cases, for reasonably normalized databases, and with today’s high-speed network connections, this is unlikely to be a huge deal. In one example involving 326/89K rows, the difference in network traffic caused by selecting a single column versus all columns was about 10%.

A better reason to limit columns to only what’s needed is index lookups. We want the planner to hit indexes instead of doing a sequential scan of the table itself. We generally optimize this by creating indexes on important columns such as foreign keys and of course primary keys come with an index by default. These are great, helping to identify the disk location of rows that match whatever constraints we have in a query for easier lookup or ordering.

We can also use covering indexes, which include the specific columns and expressions useful to a query, to store all of the relevant information. This prevents the second step of reading from the table itself and can provide a big performance gain. Using * as the select list in a query would likely not make use of a covering index unless you index the entire table and keep it up-to-date with added columns.

It's returning too much data, right?

That's the common answer, but I don't think it's the right one. If you're working with a reasonably normalized database, the actual network traffic difference is pretty small.

Let's take a look at a sample. The following two queries select 326 rows from the TransactionHistoryArchive table in the AdventureWorks database (which has a total of 89K rows). The first uses a SELECT * query, the second selects a specific column:

SELECT * FROM Production.TransactionHistoryArchive 
WHERE ReferenceOrderID < 100

SELECT ReferenceOrderLineID FROM Production.TransactionHistoryArchive 
WHERE ReferenceOrderID < 100

In this case, the difference in network traffic is only 15K, roughly a 10% difference (180K vs. 165K). It's worth fixing, but not a huge difference.

SELECT * makes the Table / Index Scan Monster come

Often, the bigger problem with SELECT * is the effect it will have on the execution plan. While SQL Server primarily uses indexes to look up your data, if the index contains all the columns you’re requesting it doesn’t even need to look in the table. That concept is known as index coverage. In the above example, the first query results in a Clustered Index Scan, whereas the second query uses a much more efficient Index Seek. In this case, the Index seek is one hundred times more efficient than the Clustered Index Scan.

http://farm2.static.flickr.com/1198/849940280_b55bd2970d_o.jpg

Unless you've indexed every single column in a table (which is almost never a good idea), a SELECT * query can't take advantage of index coverage, and you're likely to get (extremely inefficient) scan operations.

If you just query the rows you'll actually be using, it's more likely they'll be covered by indexes. And I think that's the biggest performance advantage of ignoring SELECT * queries.

The Stability Aspect

SELECT * queries are also bad from an application maintenance point of view as well, since it introduces another outside variable to your code. If a column is added to a table, the results returned to your application will change in structure. Well programmed applications should be referring to columns by name and shouldn't be affected, but well programmed applications should also minimize the ways in which they are vulnerable to external changes.

References: