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.
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.
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…..