Wednesday, August 8, 2012

which is faster ? select * from table or select 'all Columns' from table ??


which is faster ? select * from table or select 'all Columns' from table ??
Many of us would have come across a scenario where  listing column names in select SQL  was found to be faster then using select * from table  command . This indeed is interesting .


The reason being ,

In case of using
 "select * from table" , A extra stage is added  where * is replaced by column names by teradata  and then it would fetch the data .

But using
 "select <all Columns > from table "  eliminates this extra stage of verifying and fetching on columns from the table.

Hence it is always recommended to use "select <all Columns > from table "




Problem :
There is a requirement to convert column data into rows.
Consider the country table with data as follows
 

Table has 4 columns (country , value1,value2,value3)
NZ  50  60  70
AUS 110 120 130

The output should be displayed in following manner
country    value
NZ  50
NZ  60
NZ  70
AUS 110
AUS 120
AUS 130

Resulting query:
select country, value1 as value from T_country
union all
select country, value2 as v from T_country
union all
select country, value3 as v from T_country;

There might be various  approaches to this problem.Union was found out to be efficient and served the purpose.

No comments:

Post a Comment

Thank you :
- kareem