MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dynamic SQL versus Static SQL

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
geetha001
Beginner


Joined: 22 Jun 2005
Posts: 41
Topics: 14

PostPosted: Tue Oct 26, 2010 9:33 pm    Post subject: Dynamic SQL versus Static SQL Reply with quote

I have a requirement where I need to use multiple cursors in a DB2 stored procedure because of the different sort orders columns on the front end.

The numbers of cursors to be defined can go upto 20 as there may be upto 10 columns on the page for which the user has ability to sort on.

The options to do this would be
1. Static SQL defining all the 20 cursors (at any point of time only one cursor will execute)
2. Static SQL defining one cursor with a big CASE statement in the 'ORDER BY' clause
3. One more option would be dynamic SQL

I am trying to find an answer as to what would be the best approach in this case, whether static SQL or dynamic SQL?

Also, the table I would be accessing to get the data has millions (200+ millions) of rows and that concerns me about using dynamic SQL.

Please let me know if you have opinions/answers about what would be the better of the two approaches static/dynamic SQL.
Back to top
View user's profile Send private message
CZerfas
Intermediate


Joined: 31 Jan 2003
Posts: 211
Topics: 8

PostPosted: Wed Oct 27, 2010 6:48 am    Post subject: Reply with quote

First of all: The number of rows in the table to be read is not decreasing the performance figures you could get. In fact will the elapse time of your query benefit from beeing "optimized" for every run of your program compared to the static version.

You just have to pay the price for this optimizing of the query CPU- and elapse-wise. Therefor you should be worried, if the number of executions of your program is high. Start it a couple of times a day is nothing to be considered, but start it a couple of times per second really would speak for the static version of this query.

Besides: I don't think you can use the CASE statement in the ORDER BY clause.

regards
Christian
Back to top
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Wed Oct 27, 2010 8:35 am    Post subject: Reply with quote

I have supported custom and packaged applications with high volumes of database activity that use dynamic sql exclusively. I have never experienced any performance issues associated with the prepare of the sql statements. For your situation, dynamic sql is the way to go. Think of the long term impacts of maintaining 10 or 20 statements in one or more stored procedures rather than just one statement. Think about the increased development times. Think about the app that calls the stored proc that returns up to 20 results sets, but only one of them has data. Think about what happens when someone requests a slightly different sort order. Choose the right tool for the job. There are some challenges unique to dynamic sql, but these are more than offset by the advantages.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Wed Oct 27, 2010 12:16 pm    Post subject: Reply with quote

extracting the data without a sort (order by) would not be as resource intensive as with an order by.

why not have the front-end do the sort?
you are returning more than a screenful of rows and the front-end could do the sort easier than the multiple variations on the mainframe.

yeah, yeah, I know- this is the way it was designed.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Wed Oct 27, 2010 1:07 pm    Post subject: Reply with quote

CZerfas wrote:

Besides: I don't think you can use the CASE statement in the ORDER BY clause.


I just ran a test DB2V9, it works.
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12369
Topics: 75
Location: San Jose

PostPosted: Wed Oct 27, 2010 3:21 pm    Post subject: Reply with quote

geetha001,

Untested sql , but something like this might work.


Code:

SELECT COL_01                                       
      ,COL_02                                       
      ....
      ,COL_10
  FROM table
  ORDER BY LOCATE('2','1234567890')
  ;   


The result will be sorted on the col2 as the locate results into returning the position of the string 2 is found in the string '1234567890'.


In the program define a host variable and move the column number you want to perform the order by.

Code:

HOST-ORD     PIC X.

Move '2' to Host-ord


SELECT COL_01                                       
      ,COL_02                                       
      ....
      ,COL_10
  FROM table
  ORDER BY LOCATE(:Host-ord,'1234567890')
  ;   


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
geetha001
Beginner


Joined: 22 Jun 2005
Posts: 41
Topics: 14

PostPosted: Wed Oct 27, 2010 9:17 pm    Post subject: Reply with quote

Thank you for all your inputs.

I am still looking at the static and dynamic SQL options, since it will be something new to my application and response times are also of a great concern.

However, I would like to try the LOCATE option mentioned by Kolusu.

Kolusu,

One question though, what If there are more than 10 columns would there be something that I can specify so the search string can look for a two digit number?

Thanks
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12369
Topics: 75
Location: San Jose

PostPosted: Wed Oct 27, 2010 10:23 pm    Post subject: Reply with quote

geetha001 wrote:
Kolusu,

One question though, what If there are more than 10 columns would there be something that I can specify so the search string can look for a two digit number?

Thanks


geetha001,

You can use alphabets (abc...z) and number (0 - 9) and use it in locate

ex:
Code:

ORDER BY LOCATE('A','ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890')
  ;   


The above gives you the option of ordering on 36 columns.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Thu Oct 28, 2010 2:49 am    Post subject: Reply with quote

Quote:
The above gives you the option of ordering on 36 columns


Kolusu,

am i being thick headed here?

does not your solution only provide a one (1) column order by?

what if I wanted to order by 3 columns
and the next execution order by 5 columns?
would I not have to code more than one LOCATE phrase?
then we run into the problem of a variable number/possibilities of ORDER BY
columns
I realize that the TS has not really indicated such a requirement, but can I?
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Thu Oct 28, 2010 7:46 am    Post subject: Reply with quote

I understand the need to get fancy with solutions at times, but getting fancy to avoid dynamic sql is unnecessary. Dynamic sql is an extremely valuable often underutilized feature. Why mix in trickery when a straightforward solution is available? The dynamic sql will also provide capabilities to optionally include/exclude predicates and table joins. btw, if you use SPUFI or DSNTIAUL, you're already using dynamic sql.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12369
Topics: 75
Location: San Jose

PostPosted: Thu Oct 28, 2010 10:27 am    Post subject: Reply with quote

dbzTHEdinosauer wrote:
Quote:
The above gives you the option of ordering on 36 columns


Kolusu,

am i being thick headed here?

does not your solution only provide a one (1) column order by?

what if I wanted to order by 3 columns
and the next execution order by 5 columns?
would I not have to code more than one LOCATE phrase?
then we run into the problem of a variable number/possibilities of ORDER BY
columns
I realize that the TS has not really indicated such a requirement, but can I?


dbzTHEdinosauer,

From what I understood , OP at any time has option of sorting just 1 column depending on the user requirement. The requirement may be to show the information related to customer on a screen (ex: his medical records) and then he has the option of sorting it on the date of service , or the doctor or amount.

If you really want to have a variable number of columns in the order by, then you need to define all the max possible values and then manipulate them dynamically. However I am not sure if it would work or not and I have no way of testing it.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
geetha001
Beginner


Joined: 22 Jun 2005
Posts: 41
Topics: 14

PostPosted: Sun Nov 07, 2010 6:53 pm    Post subject: Reply with quote

Thanks Kolusu for your suggestion of LOCATE. It worked, when I use it on one column. However, I did not try yet for more than one column.
For now, my secondary sort columns are defined, therefore LOCATE would work for me.

I thought LOCATE clause would help me resolve my issues. However, I am encountering another situation.

Since my application requires paging concept. I am trying to use ROWNUMBER OVER ORDER BY (ColA), so that I can get to the rows I need exactly and help me with my paging concept.

If my SQL is

Code:

SELECT
a.Col1
a.Col2
CASE
(WHEN a.col3 = '9999-12-31'
 THEN a.col4
 ELSE
        a.col3
END) AS testcol
a.Col5
ROWNUMBER OVER ORDER BY (testcol, a.Col2, a.Col5)
FROM tab1 A
WHERE a.col1 = 123


I am getting a SQLCODE = -204 for 'testcol'.
When I have a.Col2 and/or a.Col5 the SQL runs fine, but it is only for 'testcol' because it is like an alias for the actual column, I am having an issue.

Please let me know if there is another right way to do this.

I am also trying to fit in the LOCATE clause, if this SQL works fine first. I am hoping I will be able to LOCATE clause also.
Back to top
View user's profile Send private message
geetha001
Beginner


Joined: 22 Jun 2005
Posts: 41
Topics: 14

PostPosted: Mon Nov 08, 2010 7:16 pm    Post subject: Reply with quote

I would like make one correction of my last post

Quote:

I am getting a SQLCODE = -204 for 'testcol'.
When I have a.Col2 and/or a.Col5 the SQL runs fine, but it is only for 'testcol' because it is like an alias for the actual column, I am having an issue.


I am actually getting a SQLCODE = -206.

I tried to use the case statements in the ORDER BY clause as follows and it works fine

Code:

SELECT
a.Col1
a.Col2
CASE
(WHEN a.col3 = '9999-12-31'
 THEN a.col4
 ELSE
        a.col3
END) AS testcol
a.Col5
ROWNUMBER OVER ORDER BY (
                             CASE (WHEN a.col3 = '9999-12-31'
                                       THEN a.col4
                                       ELSE
                                       a.col3
                             END))
FROM tab1 A
WHERE a.col1 = 123


This has resolved my ORDER BY issue, However I was wondering if there is any way I can use the LOCATE clause also.
Basically for my sorting and paging logic, if I can use the LOCATE verb declaring a number of cursors can be eliminated.

If any of you think this can be done otherwise, please let me know.

Thanks
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group