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

Dynamic query - Access path - for changing where clause


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

Active User


Joined: 31 Jul 2007
Posts: 136
Location: india

PostPosted: Wed Jun 13, 2012 12:35 pm
Reply with quote

Hi ,

I am preapring a dynamic qury with changing where clause .

Assume the sample below

Code:
  SELECT  PROJNO, PROJNAME, RESPEMP
    FROM    DSN8810.PROJ
    WHERE   PROJNO   = zzzz    AND     PRSTDATE = uuuu


The above query will be in a string , from which i PREPARE my qeryu adn do the fetch

I have a second option as below
Code:
SELECT  PROJNO, PROJNAME, RESPEMP
    FROM    DSN8810.PROJ
    WHERE   PROJNO   = ?
    AND     PRSTDATE = ?

Move the "SQL to execute" to STRING-VARIABLE

Code:
    EXEC SQL DECLARE CSR2 CURSOR FOR FLSQL;
    EXEC SQL PREPARE FLSQL FROM :STRING-VARIABLE;
    EXEC SQL OPEN CSR2 USING :TVAL1, :TVAL2;


Now the question i have is , will there be any difference in the way or instance at which access path is determined ?

Will it be determined during PREPARE in both the cases or , PREPARE in first case and OPEN in second case.



Please let me know if the question is not clear i will try to better my wordings.
Back to top
View user's profile Send private message
gylbharat

Active Member


Joined: 31 Jul 2009
Posts: 565
Location: Bangalore

PostPosted: Wed Jun 13, 2012 1:04 pm
Reply with quote

I don't think there will be any difference... as access path will be determined when we open the cursor.

Experts... please correct me if i am wrong.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Wed Jun 13, 2012 1:21 pm
Reply with quote

it is so easy when one refers to the manual,
in this case REDBOOK- Dynamic SQL

Access path is never determined at OPEN.

I suggest that you both read the above mentioned manual.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Wed Jun 13, 2012 1:41 pm
Reply with quote

cvishu,

you should pay particular attention to the discussion of tokens vs literals in your WHERE clause,
because they do affect the access path chosen.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Jun 13, 2012 8:00 pm
Reply with quote

There are several big differences between the two options.
- When using literals the access path can change depending on the statistics of the columns (=sometimes better performance)
-before DB2 v10 : Each different dynamic statement needs preparing and PREPARE is an expensive (cpu/elapsed) statement. When using parameter-markers the statement does not change so even if you do prepare, DB2 will find the same statement in the "dynamic statement cache" and reuse (=a lot faster) the previous accesspath.
So in general it is always better to use parameter markers. Some big players on the software market refuse to do so,since v10 DB2 is supposed to be smart enough to treath look-a-like (only literals are different) SQL-statements the same way as identical SQL-statements.

It is advised to use parameter-markers whenever possible.

This is the short answer, It would be wise to follow dbzTHEdinosauer advice and start reading.
Back to top
View user's profile Send private message
cvishu

Active User


Joined: 31 Jul 2007
Posts: 136
Location: india

PostPosted: Fri Jun 15, 2012 12:07 pm
Reply with quote

Thanks every one
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 Access to non cataloged VSAM file JCL & VSAM 18
No new posts How to access web services/website? Mainframe Interview Questions 4
No new posts RC query -Time column CA Products 3
No new posts Using Dynamic file handler in the Fil... COBOL Programming 2
No new posts SET PATH in View DDL DB2 2
Search our Forums:

Back to Top