Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
DB2 Query for performance

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: DB2 Query for performance
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    Post subject:
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    Post subject: Reply to: DB2 Query for performance
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    Post subject: Reply to: DB2 Query for performance
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    Post subject:
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    Post subject:
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    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 HEX value search in a DB2 query maxsubrat DB2 2 Wed Oct 04, 2017 3:04 pm
No new posts Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts Can we limit length in concatenation ... balaji81_k DB2 7 Tue Aug 22, 2017 2:50 am
No new posts CONTIG for performance? JPVRoff JCL & VSAM 8 Fri Jun 09, 2017 8:39 am
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us