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 > row count in the table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-08-11, 18:07
db2aix db2aix is offline
Registered User
 
Join Date: Jul 2009
Location: USA
Posts: 50
row count in the table

Looking for a way to get the row count in the table without using SELECT or executing RUNSTATS. Any options?
Reply With Quote
  #2 (permalink)  
Old 12-08-11, 21:30
fengsun2 fengsun2 is offline
Registered User
 
Join Date: Nov 2011
Posts: 124
How about MQT
it NEEDS your base table must have a unique index or pk....
and still have to excute select from mqt.....
But it will be much more efficient than count(*) from basetable.....

create table mqt as ( select pk,count(*) as cnt from basetable group by pk) data initially deferred refresh immediate
Reply With Quote
  #3 (permalink)  
Old 12-09-11, 08:07
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
MQT seems good idea.
But, I doubt wheather it is effective or not.

The MQT has same number of rows as basetable.
(because, pk is also unique, then group by pk results all count(*) are one.)
So, if compared index scan of pk of basetable and table space scan of MQT,
I'm not sure the MQT is more effective.
Reply With Quote
  #4 (permalink)  
Old 12-09-11, 11:50
db2aix db2aix is offline
Registered User
 
Join Date: Jul 2009
Location: USA
Posts: 50
I cannot query the table or use RUNSTATS. Any other options?
Reply With Quote
  #5 (permalink)  
Old 12-09-11, 12:31
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Quote:
Originally Posted by db2aix View Post
I cannot query the table or use RUNSTATS. Any other options?
Try a psychic.
__________________
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
  #6 (permalink)  
Old 12-09-11, 13:41
db2aix db2aix is offline
Registered User
 
Join Date: Jul 2009
Location: USA
Posts: 50
Quote:
Originally Posted by Marcus_A View Post
Try a psychic.

Already did. What else can you suggest?
Reply With Quote
  #7 (permalink)  
Old 12-09-11, 14:20
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Quote:
Originally Posted by db2aix View Post
Already did. What else can you suggest?
I don't think you can count the rows unless you count them.

However, you might want to look at using sampling with runstats if you think a full runstats takes too long (see manual). Obviously, the number many not be exact.

Also, if your table has a transaction date of some kind, or a PK that always gets larger (like an indentity column) you could only count the new rows since the last time you counted them, to save some time.

If using an SQL statement, rather than runstats, use count(*) and not count (column-name). This will let DB2 to use whichever method it thinks is fastest to count, and will propbably use an index only and and actually touch the data pages.
__________________
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
  #8 (permalink)  
Old 12-09-11, 19:27
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Db2pd -tcbstats

Sshows the number of inserts and deletes since the last runstats or since last db activation, whichever is later. If there has been a runstats since db activation, you can add/subtract to card in the catalog.



I know, the next condtion is, you dont want to issue a command
Reply With Quote
  #9 (permalink)  
Old 12-09-11, 21:03
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by sathyaram_s View Post
I know, the next condtion is, you dont want to issue a command
I wonder what is the ultimate goal of db2aix. I have a feeling that it is different from "counting table rows without counting".
Reply With Quote
  #10 (permalink)  
Old 12-11-11, 09:02
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
If you don't want any access to table/indexes, don't want to execute runstats and willing to unload all data, then you can use db2dart /ddel and count the lines in a file it creates.
Reply With Quote
  #11 (permalink)  
Old 12-11-11, 11:49
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
If you track all inserts & deletes (e.g. through a trigger) you'll never need to issue count(*) [unless maybe initially, when the triggers are created] to obtain an exact row count.

That's essentially what the "real-time statistics" (RTS) do (at least, on DB2 for z/OS; don't know whether this exists on DB2 for LUW). The counts are stored in catalog table SYSIBM.TABLESPACESTATS (per tablespace partition, but that's per table for non-partitioned single-table tablespaces).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
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