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 > table count

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-27-04, 17:15
momon momon is offline
Registered User
 
Join Date: May 2004
Posts: 2
Question table count

Hi,

Is there a better / faster way of doing a table count?

Curently using:
select count(*)
from table
Thanks!
Reply With Quote
  #2 (permalink)  
Old 05-27-04, 17:42
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Depends on what you need it for. If a good estimate is sufficient (and provided statistics are current) you can use
Code:
select card from syscat.tables where tabname='TABLENAME'
Reply With Quote
  #3 (permalink)  
Old 05-27-04, 18:03
momon momon is offline
Registered User
 
Join Date: May 2004
Posts: 2
Thanks!
Reply With Quote
  #4 (permalink)  
Old 05-27-04, 18:23
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
I've wondered this myself. You could replace * with a constant, something like 'count(1)', or maybe just select an indexed column rather than *, i.e. count(indexed_column).

I tend to use count(*) or count(1), depending on which way the wind is blowing.

I've read articles that say that any one of the 3 concepts is the best but nothing to date has convinced me.

Maybe someone in this forum could enlighten us?
Reply With Quote
  #5 (permalink)  
Old 05-27-04, 19:02
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
If you get the card (row count) form syscat.tables, it is only accurate as of the last time runstats command was executed on that table.

When running a count(*) opn a table with no WHERE clause (or if a WHERE clause is used, all predicates are in the index), DB2 will read an index rather than the table to get the count of rows (in most situations).
__________________
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