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

DB2 Query for performance


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

New User


Joined: 05 Oct 2005
Posts: 94

PostPosted: Mon Jun 16, 2008 7:45 pm
Reply with quote

Please let me know which will be the most appropriate query to be used in program and why?

Code:
1) SELECT  COUNT(*)
       FROM  TPAPUAG
    WHERE CON_CODE          = 'CON01'
        AND  PRAG_NO            = 1201
        AND  PUAG_SEQ_NO    = 1;

Code:
2) SELECT  PRAG_NO
       FROM  TPAPUAG
     WHERE CON_CODE       = 'CON01'
         AND  PRAG_NO        = 1201
         AND  PUAG_SEQ_NO    = 1;


Code:
3) SELECT  1
       FROM  TPAPUAG
      WHERE CON_CODE       = 'CON01'
          AND  PRAG_NO        = 1201
          AND  PUAG_SEQ_NO    = 1;

I feel the 3rd option is the best.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Mon Jun 16, 2008 7:53 pm
Reply with quote

Since the three querys are going to return three different results the most appropriate 1 would be the 1 that returns what you need!

If there are 1,000 rows in the table that satisfy the where clause then option 1 will return 1,000 and option 2 will return 1201 1,000 times and option 3 will return 1 1,000 times!
Back to top
View user's profile Send private message
ascelepius

New User


Joined: 16 Jun 2008
Posts: 35
Location: bangalore

PostPosted: Mon Jun 16, 2008 8:36 pm
Reply with quote

Hi nikyojin,

To add what to Carl said, if its only the existence of a particular record that you are interested in, I would think using the clause,

Code:

OPTIMIZE FOR 1 ROW ONLY FETCH FIRST 1 ROW ONLY


along with the SQL.

regards,
ASCI(I)
Back to top
View user's profile Send private message
nikyojin

New User


Joined: 05 Oct 2005
Posts: 94

PostPosted: Mon Jun 16, 2008 9:14 pm
Reply with quote

I'm sorry since all the fields making the primary key is included in the where condition.

So the no of rows returned will always be 1.

Sorry for the confusion once again. Please reply
Back to top
View user's profile Send private message
Suresh Ponnusamy

Active User


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

PostPosted: Mon Jun 16, 2008 9:24 pm
Reply with quote

If WHERE Clause has only Primary Keys, then 3rd query will take less time when compared to other two queries. Since it needs to check only the existence of the record.
1)
Primary Keys being used, will go via Index path and if value is exist then it will return 1.

2)
Then 2nd Query. It needs to find out the existence and select the column value.

3)
Then 1st Query. It needs to find out the matching records (In this case it is 1) and apply the scalar function COUNT on that.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Jun 17, 2008 3:42 pm
Reply with quote

I think there is no difference in the 3 queries as the index is always the same and in the same order ... EXPLAIN on these 3 queries will give the same o/p in PLAN_TABLE and DSN_STATEMNT_TABLE ...

Correct me if im wrong !!!
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 RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts exploiting Z16 performance PL/I & Assembler 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top