IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Alternate to Count(*) in select query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
GMnaidu

New User


Joined: 29 Jan 2009
Posts: 6
Location: Chennai

PostPosted: Mon Mar 30, 2009 5:02 pm
Reply with quote

Hi All,

I would like to know if there is any alternate way to find out no.of records in a DB2 table apart from the Count(*) in the select query.
Count(*) consumes so much CPU utilization and not preferable.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Mar 31, 2009 12:51 am
Reply with quote

Hello,

Quote:
Count(*) consumes so much CPU utilization
Yup, and it should. . .

Why do you believe you need to count all of the rows in real-time? As the numbers change from moment to moment, why not ask your dba if there are weekly/monthly stats that are available? I believe something close should suffice?
Back to top
View user's profile Send private message
GMnaidu

New User


Joined: 29 Jan 2009
Posts: 6
Location: Chennai

PostPosted: Tue Mar 31, 2009 10:16 am
Reply with quote

Hi Dick,

In my cobol program i have quey which needs the count of a DB2 table.
I am looking for an alternate method instead of using Count(*)over here.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Mar 31, 2009 10:28 am
Reply with quote

Hello,

If you truly need the actual count in your progam, what you have is most likely what you'll continue to use.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Mar 31, 2009 4:00 pm
Reply with quote

GM naidu,

just out of curiosity, why does the COBOL program need to know the count of the DB2 table?

is this a temp table?
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Wed Apr 01, 2009 6:49 am
Reply with quote

The only time I've seen COUNT(*) used in a COBOL program was to determine if an internal table was large enough to hold all the rows prior to loading the table. I never really saw much sense in that. I would simply load the table until I exceeded the maximum occurrences in which case I'd cancel with an error message. The only advantage I could see in doing the COUNT first would be to know how large the internal table needed to be in case it wasn't big enough.
Back to top
View user's profile Send private message
GMnaidu

New User


Joined: 29 Jan 2009
Posts: 6
Location: Chennai

PostPosted: Wed Apr 01, 2009 5:40 pm
Reply with quote

Hi,

It goes like this,

we have IMS screens.if user want any info regarding some person he will just type the name and press enter.
in the backend a program is triggered automatically which have a select count(*) query with where condition provided by user.
if 0 records then in the online screen 'no records in db' msg should be displayed
else it should retrive the info from the db2 tables and display on the screen.

Here i am looking for an alternate to count(*).
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Apr 01, 2009 6:25 pm
Reply with quote

normally, I have always written the logic to access the rows,
and if zero rows are returned, appropriate advisory message is displayed.

idea being, why do a COUNT, then SELECT if count is >0?
saves an SQL call (count).
saving a DECLARE CURSOR and FETCH is not worth it,
or a ROWSET SELECT which is faster than the DECLARE CURSOR/ FETCH

or do a singleton select without the FIRST Row only clause
if you are only interested in 0,1 or more than 1 type info.

0 = 1 row
100 = no rows
-811 = more than one row.

the COUNT will process until no-more rows to interrogate,
whereas a singleton select will stop after the second row is found,
even though DB2 is 'provoked' with a forced error.

if the WHERE conditions force an index read, the effect of the COUNT is not so bad,
but if we are doing a table scan, forget it - go with the -811 or create the necessary indexes.

DBA's will debate the -811 logic (because of the error handling logic),
but the table size, design, indexes are more important considerations
than some DBA's hard and fast rule.

If your vsn of DB2 supports ROWSET Select and
you know there will never be more rows than the rowset count
throw the COUNT crap out and replace with ROWSET select.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Apr 01, 2009 7:39 pm
Reply with quote

Hello,

Quote:
If you truly need the actual count in your progam
Quote:
if 0 records then in the online screen 'no records in db' msg should be displayed
else it should retrive the info from the db2 tables and display on the screen.
So, there was never any reason to use count(*). . .

Suggest you may have a simple scrolling application that has nothing to do with "count". Write the code to show up to the first full screen of data. If there is none, show none. If there is less than a full screen, show what there is. If there is more than a full screen, show the full screen and the user will decide what they want to do next.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To get the count of rows for every 1 ... DB2 3
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts RC query -Time column CA Products 3
No new posts Validating record count of a file is ... DFSORT/ICETOOL 13
No new posts Dynamically pass table name to a sele... DB2 2
Search our Forums:

Back to Top