MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why SELECT * is slower than SELECT COUNT(*) in DB2?

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
satyenderd
Beginner


Joined: 26 Aug 2005
Posts: 144
Topics: 73

PostPosted: Thu Sep 03, 2009 4:34 pm    Post subject: Why SELECT * is slower than SELECT COUNT(*) in DB2? Reply with quote

Hi all,

I have a small question.

I would like to know, Why SELECT * is slower than SELECT COUNT(*) in DB2?

I have gone thru few forums, but i didnt found the correct one.

Could anyone please throw some light on this question?

Thanks in advance to all.
_________________
Satya
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12380
Topics: 75
Location: San Jose

PostPosted: Thu Sep 03, 2009 5:31 pm    Post subject: Reply with quote

satyenderd,

I don't think they are even comparable in the first place. Select * retrieves ALL the columns of the table where as select count(*) retrieves just 1 row 1 column. DB2 is sensitive to the number of columns selected. Remember the number of columns DB2 needs to adjust the work area buffers.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
satyenderd
Beginner


Joined: 26 Aug 2005
Posts: 144
Topics: 73

PostPosted: Thu Sep 03, 2009 5:55 pm    Post subject: Reply with quote

Thank you very much Kolusu.
_________________
Satya
Back to top
View user's profile Send private message
Anuj Dhawan
Intermediate


Joined: 19 Jul 2007
Posts: 298
Topics: 7
Location: Mumbai,India

PostPosted: Thu Sep 17, 2009 4:26 am    Post subject: Reply with quote

Why do you want to compare Oranges with Apples? One is merely the count of rows while other is entire data. Neutral
_________________
Regards,
Anuj
Back to top
View user's profile Send private message
psmadhusudhan
Beginner


Joined: 28 Nov 2006
Posts: 143
Topics: 48

PostPosted: Thu Sep 17, 2009 5:44 am    Post subject: Reply with quote

I have small query. Suppose I have table TBL1 has comprised of 95 columns and has 3000 million rows. what would I get if I fire query Select count(7) from ADM.TBL1.
_________________
Thanks
Madhu Sudhan
Back to top
View user's profile Send private message
Anuj Dhawan
Intermediate


Joined: 19 Jul 2007
Posts: 298
Topics: 7
Location: Mumbai,India

PostPosted: Thu Sep 17, 2009 5:51 am    Post subject: Reply with quote

1. Why do you append your query to a totally un-related thread?
2. What happend when you try it at your end?
_________________
Regards,
Anuj
Back to top
View user's profile Send private message
psmadhusudhan
Beginner


Joined: 28 Nov 2006
Posts: 143
Topics: 48

PostPosted: Thu Sep 17, 2009 6:36 am    Post subject: Reply with quote

Dear Anuj,

1. The thread is abt SELECT COUNT(*) and I have asked abt SELECT COUNT(7).

2. I dont have access to DB2 currently. So I am unable to test it otherwise I would have done myself rather posting it here.

Please reply if you know the solution.
_________________
Thanks
Madhu Sudhan
Back to top
View user's profile Send private message
Anuj Dhawan
Intermediate


Joined: 19 Jul 2007
Posts: 298
Topics: 7
Location: Mumbai,India

PostPosted: Thu Sep 17, 2009 11:25 am    Post subject: Reply with quote

Quote:
Please reply if you know the solution.
I'll try...I'll try...Smile

The COUNT function returns the number of rows in a query. The syntax for the COUNT function is:
Code:
SELECT COUNT(expression)
FROM tables
WHERE predicates;

Note: The COUNT function will only count those records in which the field in the brackets is NOT NULL.

For example, if you have the following table called suppliers:
Code:
Supplier_ID|   Supplier_Name |State
       1   |   Anuj          | CA
       2   |   Madhu         |   
       3   |   Sudhan        |
So the result for the below query will return 3.
Code:
Select COUNT(Supplier_ID) from suppliers;
where suppliers is table name.

While the result for the next query will only return 1, since there is only one row in the suppliers table where the State field is NOT NULL.
Code:
Select COUNT(State) from suppliers;


Having said that, since the COUNT function will return the same results regardless of what NOT NULL field(s) you include as the COUNT function parameters (ie: within the brackets), you can change the syntax of the COUNT function to COUNT(1) or any number as you choose 7, to get better performance as the database engine will not have to fetch back the data fields.

For example, the following syntax would result in better performance:
Code:
SELECT department, COUNT(1) as "Number of employees"
FROM employees
WHERE salary > 25000
GROUP BY department;
instead of
Code:
SELECT department, COUNT(*) as "Number of employees"
FROM employees
WHERE salary > 25000
GROUP BY department;

Now, in former query, the COUNT function does not need to retrieve all fields from the employees table as it had to when you used the COUNT(*) syntax. It will merely retrieve the numeric value of 1 for each record that meets your criteria.
_________________
Regards,
Anuj
Back to top
View user's profile Send private message
psmadhusudhan
Beginner


Joined: 28 Nov 2006
Posts: 143
Topics: 48

PostPosted: Thu Sep 17, 2009 11:50 pm    Post subject: Reply with quote

Thanks Anuj for your detailed reply Very Happy . My doubt is cleared now.
_________________
Thanks
Madhu Sudhan
Back to top
View user's profile Send private message
Anuj Dhawan
Intermediate


Joined: 19 Jul 2007
Posts: 298
Topics: 7
Location: Mumbai,India

PostPosted: Thu Sep 17, 2009 11:58 pm    Post subject: Reply with quote

Glad to hear. Hopefully I'd been helpful... Smile
_________________
Regards,
Anuj
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group