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 > count rows fo a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-01-06, 19:31
udbdb2 udbdb2 is offline
Registered User
 
Join Date: Nov 2003
Posts: 121
count rows fo a table

what is the fastest way to count rows for a table as opposed to do count(*)?
db2 8.2 on AIX.

TIA
Reply With Quote
  #2 (permalink)  
Old 11-02-06, 00:20
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
That is the only way to get an accurate count. If you want just an estimate and you RUNSTATS are current, then you can: SELECT CARD from syscat.tables where tabname = 'mytable' and tabschema = 'myschema'

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 11-03-06, 01:35
bala_e bala_e is offline
Registered User
 
Join Date: Jan 2004
Posts: 49
Use select count(0)

In production environments runstats will be updated on every night.... and etc. Better to use select count(0) instead of count(*).


Quote:
Originally Posted by ARWinner
That is the only way to get an accurate count. If you want just an estimate and you RUNSTATS are current, then you can: SELECT CARD from syscat.tables where tabname = 'mytable' and tabschema = 'myschema'

HTH

Andy
Reply With Quote
  #4 (permalink)  
Old 11-03-06, 01:58
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by bala_e
In production environments runstats will be updated on every night.... and etc. Better to use select count(0) instead of count(*).
Why would runstats be updated every night? Maybe in your production environment, but not everyone's.

There is no difference in performance between select count(*) from table-name, and select count(0) from table-name.

I did not see select count(0) documented in the SQL Reference Vol 1. Although it does work like count(*), I would not use any undocumented syntax.
__________________
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
  #5 (permalink)  
Old 11-03-06, 02:10
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
I agree with Marcus_A, don't use undocumented syntax, because it can stop working in new version! So you can have problems when migrating the db2 to new version.

BTW, has anyone created a SQL access plan to prove the performance statement?
Hope this helps,
Grofaty
Reply With Quote
  #6 (permalink)  
Old 11-03-06, 12:56
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by grofaty
Hi,
BTW, has anyone created a SQL access plan to prove the performance statement?
Hope this helps,
Grofaty
I ran visual explain with both (* and 0) and the timerons were the same. I tried it on a table with 10 million rows and another one with 37 rows.

DB2 will scan the leaf pages of the smallest index to do the count.
__________________
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 11-03-06, 13:36
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
"select Count(0) from table1" is valid SQL ... It is the similar to as "select 0 from table1" ...

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #8 (permalink)  
Old 11-03-06, 14:30
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by sathyaram_s
"select Count(0) from table1" is valid SQL ... It is the similar to as "select 0 from table1" ...

Sathyaram
OK, so any literal (numeric or character) will do. But there is no advantage over count(*) that I can see when trying to count the rows in a table.
__________________
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
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