Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Alternate to Count(*) in select query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Alternate to Count(*) in select query
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

Site Director


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

PostPosted: Tue Mar 31, 2009 12:51 am    Post subject:
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    Post subject:
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

Site Director


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

PostPosted: Tue Mar 31, 2009 10:28 am    Post subject:
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: 6968
Location: porcelain throne

PostPosted: Tue Mar 31, 2009 4:00 pm    Post subject:
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    Post subject:
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    Post subject: Reply to: Alternate to Count(*) in select query
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: 6968
Location: porcelain throne

PostPosted: Wed Apr 01, 2009 6:25 pm    Post subject:
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

Site Director


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

PostPosted: Wed Apr 01, 2009 7:39 pm    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Select numeric portion from CHAR data... balaji81_k DB2 1 Sat Aug 19, 2017 1:51 am
No new posts SQL - select data available in index Nileshkul DB2 3 Mon Jun 26, 2017 1:30 am
No new posts CICS Transaction attach count of supp... lind sh CICS 1 Wed Jun 21, 2017 1:33 pm
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts Check if any Detail records and extra... V S Amarendra Reddy SYNCSORT 19 Mon May 08, 2017 8:54 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us