Basic points to be considered when starting with SQL Server – – Part 4

Hello Friends, Yesterday I was fetching some records from table and Then I thought about ‘*’.

Is asterisk is our friend or enemy ?


Basically this is very easy to write sql queries with asterisk, but is it worth? Do we need to use it like that? What if we use it and if not?
Lets answer these.

Consider a scenario where we need to fetch names of employees who has salary greater than Rs.50000,

We can do it simply by:-

SELECT * FROM Employee WITH(NOLCOK) WHERE SAL>50000;

But this is not a correct way. When we uses asterisk it will get all the columns of the returned rows. But we only need Name and other columns will be junk for us.

Will this impact us in any Way? What happen if I fetch extra Data and use the one needed?
Yes this will Impact very badly. Since, it will reduce the performance of the app using this query.

How??
Each and every column has a datatype and Based on the type that will take sufficient amount of bytes. Now if we will fetch more columns then we need more bytes, and those more bytes will be transferred over network to app. This will directly Impact the performance of the app.

So we never use Asterisk?
Answer is Yes.

Conclusion
Never use Asterisk if you need performance from your SQL queries. It is an enemy that looks like friend. That will reduce some words in query but will reduce performance too.
Thanks for reading this, Please keep in touch soon will post more…..

<<Previous Post                                                                              Next Post>>

Written By: Ankit Arora, Sr. Software Engineer, Mindfire Solutions

Advertisements

Posted on September 11, 2014, in SQL and tagged , , , . Bookmark the permalink. Leave a comment.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: