Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
Selecting columns in a query - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on Web, HTML5, Mobile, MonoTouch for iPhone, MonoDroid for Android, and Windows Azure.

News

Personal Blog

Work Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

Selecting columns in a query

I've always known this, but I thought I would share it.

I just did a quick performance test on two queries using the Sql Server Profiler.  The difference between the two queries was

Query 1: select * from table where...........

Query 2: select col1, col2, ..... from table where........

There is a small, but noticable performance difference in the two queries.  Query 1 was significantly slower on the first call versus Query 2, somewhat slower on the second call, and performed the same on subsequent calls.  While this was particular to my application, I think it is something I would suggest to all developers to specifically specify the columns in a query.

Wally

Comments

Geoff said:

Another great reason for ALWAYS specifying the columns is that if the underlying data-structure changes you do not have to re-write your code. You can just use the "Select newcol1 as col1" to keep your code working.
# March 23, 2005 5:53 PM

Ron Krauter said:


How many columns does "table" have?
Is "select *" slower if the number of columns increases.
# March 23, 2005 8:28 PM

Charlie Barker said:

Select * From = Naughty/Lazy programming ! :o)

# March 24, 2005 1:51 PM

Michael K. Campbell said:

Wally,

Another great reason to ALWAYS specify your column names: what if someone later on adds a varchar(300) column on to your table? Your app certainly doesn't need it... so no sense marshalling that info over the wire...

Explicitly specifying your columns in a SELECT is a best practice.
# March 30, 2005 12:27 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)