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

SQL question - Count or not to Count - Efficiency


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

New User


Joined: 15 Dec 2005
Posts: 72
Location: RALEIGH NC, USA

PostPosted: Thu Nov 06, 2008 11:09 pm
Reply with quote

In some programs here I read that to test to see if a records already exists they code
Code:
select count(*)
into host-variable-counter
from table
where key = ...


Then they check host-variable-counter being > 0

My question is ~ Is it more efficient to code ?

Code:
select key-column
into host-variable-counter
from table
where key  = ...



Then check for SQLCODE = 0
Back to top
View user's profile Send private message
Suresh Ponnusamy

Active User


Joined: 22 Feb 2008
Posts: 107
Location: New York

PostPosted: Thu Nov 06, 2008 11:30 pm
Reply with quote

Hi

The First one is better since we are using Key column in the where clause.
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Fri Nov 07, 2008 12:46 am
Reply with quote

Suresh,

How does the WHERE clauses differ in this context? Both the SQLs use the key column in the where clause.
Back to top
View user's profile Send private message
Suresh Ponnusamy

Active User


Joined: 22 Feb 2008
Posts: 107
Location: New York

PostPosted: Fri Nov 07, 2008 12:57 am
Reply with quote

True.. Both the queries are using the same WHERE Clause. So WHERE Clause doesn't matter here.

Thanks for correcting me.
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: Fri Nov 07, 2008 2:47 am
Reply with quote

Hello,

I'd suggest using the first format with count. . .

If there is more than one row that satisfies the where, a non-zero (which does not indicate "row not found") will be returned (-811 iirc) if a column is named rather than count.

The check for sqlcode = zero would not work correctly.
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Fri Nov 07, 2008 6:52 am
Reply with quote

Hi Dick,

If he meant the "key column" to be having only unique rows, I guess it'll not make any difference... icon_smile.gif
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: Fri Nov 07, 2008 7:32 am
Reply with quote

Hi Arun,

Yup, if the key-column represents a single-column, unique key icon_smile.gif

The "count" format should work for all cases and would not potentially cause additonal work or problems later if the key were expanded to include a second column because the first column could no longer guarantee uniqueness (say some major business change or merger).
Back to top
View user's profile Send private message
Arun Raj

Moderator


Joined: 17 Oct 2006
Posts: 2481
Location: @my desk

PostPosted: Fri Nov 07, 2008 8:21 am
Reply with quote

Hi Dick,

In similar situations we have used something like this.
Code:
select DISTINCT 1
into :host-variable
from table
where key  = ...


and then check for SQLCODE = 0
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: Fri Nov 07, 2008 8:32 am
Reply with quote

Hi Arun,

That would surely work as well icon_smile.gif

d
Back to top
View user's profile Send private message
srj1957

New User


Joined: 15 Dec 2005
Posts: 72
Location: RALEIGH NC, USA

PostPosted: Mon Nov 10, 2008 7:21 pm
Reply with quote

There was no didfference in the where clause

So the better of the two is ..

Code:
Select count(*)
into ws-counter
from table
where key-cloumn = host-var


then check the counter > 0


Thanx for your help...
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Mon Nov 10, 2008 7:52 pm
Reply with quote

IF you are using DB2 Vsn7 or above:

you only care if the count is 0 or >0

then select literal or select keyfield
with a
FETCH FIRST ROW ONLY
clause,
which will stop the search.
A count will continue to the end of the table ,
or index if your WHERE clause makes it an index scan instead of a table scan.

if you only want 0, 1 or >1
select a field or literal and let the -811 stop the search when it finds the second.

If you need the COUNT, then do count,
Back to top
View user's profile Send private message
srj1957

New User


Joined: 15 Dec 2005
Posts: 72
Location: RALEIGH NC, USA

PostPosted: Thu Nov 13, 2008 7:07 pm
Reply with quote

Thanks for all the suggestions
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 Validating record count of a file is ... DFSORT/ICETOOL 13
No new posts Question for file manager IBM Tools 7
No new posts Insert header record with record coun... DFSORT/ICETOOL 14
Search our Forums:

Back to Top