If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > counting rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-04, 23:40
starra starra is offline
Registered User
 
Join Date: Sep 2003
Location: manila
Posts: 21
Lightbulb counting rows

anyone can contribute his/her 2 cents...

i believe these 2 sql statement will produce the same result, however, which is better in terms of performance:

select count(col1)
from table
where col2 = :ws-col2
and col3 = :ws-col3

or

select count(*)
from table
where col2 = :ws-col2
and col3 = :ws-col3
Reply With Quote
  #2 (permalink)  
Old 01-15-04, 00:25
cgprakash cgprakash is offline
Registered User
 
Join Date: Feb 2002
Posts: 96
Optimized(rewritten) statement shows same sql for both queries when i do the explain. So it shows both will give you the same performance.

Regards
Prakash
Reply With Quote
  #3 (permalink)  
Old 01-15-04, 00:42
starra starra is offline
Registered User
 
Join Date: Sep 2003
Location: manila
Posts: 21
i did just the same and was wondering if there are any difference at all.
Thanks very much!
Reply With Quote
  #4 (permalink)  
Old 01-15-04, 00:48
sudhakarkreddy sudhakarkreddy is offline
Registered User
 
Join Date: Jan 2004
Posts: 3
Quote:
Originally posted by starra
i did just the same and was wondering if there are any difference at all.
Thanks very much!
I feel the first one is more optimized version....
(While i working in a project where DB2 is used extensively, it a standard to write count(column1) to count the number of records).


Thanks
Sudhakar
Reply With Quote
  #5 (permalink)  
Old 01-15-04, 00:52
starra starra is offline
Registered User
 
Join Date: Sep 2003
Location: manila
Posts: 21
Quote:
Originally posted by sudhakarkreddy
I feel the first one is more optimized version....
(While i working in a project where DB2 is used extensively, it a standard to write count(column1) to count the number of records).


Thanks
Sudhakar

thanks! you're right! it is usually suggested that the column be written instead of using "*"
Reply With Quote
  #6 (permalink)  
Old 01-15-04, 02:32
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
I think the suggestion to use the column name instead of * applies to "select *" and not "select count(*)". I don't think that “select count(*)” is any worse performance, it just tells DB2 to count the number of rows.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #7 (permalink)  
Old 01-15-04, 04:01
RKrick RKrick is offline
Registered User
 
Join Date: Feb 2002
Location: Germany
Posts: 141
Re: counting rows

<snip>
i believe these 2 sql statement will produce the same result...
</snip>

The fact that you have the same access path doesn't mean that you get the same result!!! If col1 allows nulls, and you have some entities with col1 = null, you WILL get different results.

HTH,
__________________
Rodney Krick
Reply With Quote
  #8 (permalink)  
Old 01-15-04, 06:05
starra starra is offline
Registered User
 
Join Date: Sep 2003
Location: manila
Posts: 21
Re: counting rows

Quote:
Originally posted by RKrick
<snip>
i believe these 2 sql statement will produce the same result...
</snip>

The fact that you have the same access path doesn't mean that you get the same result!!! If col1 allows nulls, and you have some entities with col1 = null, you WILL get different results.

HTH,
thanks for pointing that out. the column i was trying to test does not allow nulls. I just wanna know the difference on performance perspective. thanks!
Reply With Quote
  #9 (permalink)  
Old 01-15-04, 06:25
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,

Please read article about "Select count(*)..." performance.

Hope this helps,
Grofaty
Reply With Quote
  #10 (permalink)  
Old 01-15-04, 08:45
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
The article mentioned above refers to determining existence, not for determining a count when a count is needed. These are two different things.

I don’t believe that the author compared using count(*) with count(col1), which are the options under discussion in this thread.

Also note that the test was done on DB2 for OS/390 version 6. In any event, even for determining existence, it does not appear that the results for tests run in the article are significantly different if the same access path is used (index vs. table space scan).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #11 (permalink)  
Old 01-16-04, 10:28
gsreejith gsreejith is offline
Registered User
 
Join Date: Sep 2003
Location: Massachusetts
Posts: 27
My Explain shows that both them gives the same result. i.e. both of them uses the index and does NOT result in a tablespace scan.

So what I think is that whenever DB2 sees a COUNT(*) and if there is a clustered unique index for the table, DB2 just uses that index column to do a COUNT. Internally DB2 converts the SQL as COUNT(COL1).
__________________
"It is Monday morning 3:02 AM. What is your SQL response time ?"
Reply With Quote
  #12 (permalink)  
Old 01-16-04, 12:28
starra starra is offline
Registered User
 
Join Date: Sep 2003
Location: manila
Posts: 21
Quote:
Originally posted by gsreejith
My Explain shows that both them gives the same result. i.e. both of them uses the index and does NOT result in a tablespace scan.

So what I think is that whenever DB2 sees a COUNT(*) and if there is a clustered unique index for the table, DB2 just uses that index column to do a COUNT. Internally DB2 converts the SQL as COUNT(COL1).
Thanks for your input. I really appreciate it! =)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On