dbTalk Databases Forums  

Alternative to COUNT(*)

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Alternative to COUNT(*) in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
shorti
 
Posts: n/a

Default Alternative to COUNT(*) - 02-15-2010 , 02:44 PM






I am running DB2 V9.5 on AIX. We have a fairly small database (2
million records max on main table). We use COUNT(*) periodically. We
also schedule runstats and online reorg periodically. At times
COUNT(*) can be very slow and although a reorg/runstats speeds it up
we can have a window when the count is needed but reorg/runstats has
not yet run. Also, I cannot run reorg/runstats at the time a count is
requested since it would take longer to do it than it would be to just
wait for the count to return.

Any suggestions on what else can be used to get the count of a table?
The cardinality isnt accurante until a runstats is done so that is not
useful. Is there a quick "rowcount" type way to get this info? I
just need a speedy way to get number of records.

Thanks in advanced.

Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: Alternative to COUNT(*) - 02-15-2010 , 06:24 PM






Quote:
... a reorg/runstats speeds it up ...
Do you have indexes on tables?

How long time is allowed to get number of 2 million records max on
main table?

Reply With Quote
  #3  
Old   
Lennart
 
Posts: n/a

Default Re: Alternative to COUNT(*) - 02-15-2010 , 10:29 PM



On 15 Feb, 21:44, shorti <lbrya... (AT) juno (DOT) com> wrote:
Quote:
I am running DB2 V9.5 on AIX. *We have a fairly small database (2
million records max on main table). *We use COUNT(*) periodically. *We
also schedule runstats and online reorg periodically. *At times
COUNT(*) can be very slow and although a reorg/runstats speeds it up
we can have a window when the count is needed but reorg/runstats has
not yet run. *Also, I cannot run reorg/runstats at the time a count is
requested since it would take longer to do it than it would be to just
wait for the count to return.

Any suggestions on what else can be used to get the count of a table?
The cardinality isnt accurante until a runstats is done so that is not
useful. *Is there a quick "rowcount" type way to get this info? *I
just need a speedy way to get number of records.

If you want exact numbers I don't think theres much you can do ( I
assume mqt's is out of the question for this ). However, if you accept
approximative numbers you can use table samples as in

select 100*count(*) from T tablesample system(1)

There is also a Bernoulli sample which is more accurate, but slower.
See:

http://publib.boulder.ibm.com/infoce...74%65%6d%22%20

/Lennart

Reply With Quote
  #4  
Old   
shorti
 
Posts: n/a

Default Re: Alternative to COUNT(*) - 02-16-2010 , 10:03 AM



On Feb 15, 5:24*pm, Tonkuma <tonk... (AT) fiberbit (DOT) net> wrote:
Quote:
... a reorg/runstats speeds it up ...

Do you have indexes on tables?

How long time is allowed to get number of 2 million records max on
main table?
Yes we have indexes. If we have a somewhat fragmented database it can
take 45 secondsto get the count. After the reorg/runstats it takes
only a second or two.

Reply With Quote
  #5  
Old   
Ian
 
Posts: n/a

Default Re: Alternative to COUNT(*) - 02-17-2010 , 05:21 AM



On 2/16/10 9:03 AM, shorti wrote:
Quote:
On Feb 15, 5:24 pm, Tonkuma<tonk... (AT) fiberbit (DOT) net> wrote:
... a reorg/runstats speeds it up ...

Do you have indexes on tables?

How long time is allowed to get number of 2 million records max on
main table?

Yes we have indexes. If we have a somewhat fragmented database it can
take 45 secondsto get the count. After the reorg/runstats it takes
only a second or two.
What is causing the fragmentation? Do you insert and delete lots of
rows in this table, or are you getting lots of overflow rows?
Because clearly you need to fix (or mitigate) what is causing this.

How frequently are you having to do the reorg?

Reply With Quote
  #6  
Old   
Tonkuma
 
Posts: n/a

Default Re: Alternative to COUNT(*) - 02-17-2010 , 08:29 AM



Quote:
Yes we have indexes. *If we have a somewhat fragmented database it can
take 45 secondsto get the count. *After the reorg/runstats it takes
only a second or two.
The reason of getting faster after reorg might be buffer hit ratio
getting high.
I don't think that scanning fragmented table/index take more than 10
times slower than reorged table/index.

After you took 45 seconds to get the count, if you repeat the count,
you may get the count in shorter time.

Reply With Quote
  #7  
Old   
Hardy
 
Posts: n/a

Default Re: Alternative to COUNT(*) - 02-17-2010 , 06:57 PM



"Lennart" <erik.lennart.jonsson (AT) gmail (DOT) com> 写入消息
news:04c69e0c-619d-4f9a-a5a0-4dae105a9f80 (AT) b7g2000yqd (DOT) googlegroups.com...
Quote:
On 15 Feb, 21:44, shorti <lbrya... (AT) juno (DOT) com> wrote:
I am running DB2 V9.5 on AIX. We have a fairly small database (2
million records max on main table). We use COUNT(*) periodically. We
also schedule runstats and online reorg periodically. At times
COUNT(*) can be very slow and although a reorg/runstats speeds it up
we can have a window when the count is needed but reorg/runstats has
not yet run. Also, I cannot run reorg/runstats at the time a count is
requested since it would take longer to do it than it would be to just
wait for the count to return.

Any suggestions on what else can be used to get the count of a table?
The cardinality isnt accurante until a runstats is done so that is not
useful. Is there a quick "rowcount" type way to get this info? I
just need a speedy way to get number of records.


If you want exact numbers I don't think theres much you can do ( I
assume mqt's is out of the question for this ). However, if you accept
approximative numbers you can use table samples as in

select 100*count(*) from T tablesample system(1)

There is also a Bernoulli sample which is more accurate, but slower.
See:

http://publib.boulder.ibm.com/infoce...74%65%6d%22%20

/Lennart


No silver bullet for exact numbers.

for approximate size of tables, check output of db2pd -tcbstat.

Reply With Quote
  #8  
Old   
Ian
 
Posts: n/a

Default Re: Alternative to COUNT(*) - 02-18-2010 , 03:29 PM



On 2/17/10 7:29 AM, Tonkuma wrote:
Quote:
Yes we have indexes. If we have a somewhat fragmented database it can
take 45 secondsto get the count. After the reorg/runstats it takes
only a second or two.

I don't think that scanning fragmented table/index take more than 10
times slower than reorged table/index.
Why not? If FPAGES > NPAGES * 10 (or 30 or 45) it's certainly possible.

The OP should do a runstats when they're getting the problem and post
the output of REORGCHK for that table.

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.