473,387 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Cursor performance, FOR or DECLARED

People,

I've inherited a lot of SP embedded SQL code that makes substantial
use of FOR cursors rather than DECLARED, and I am wondering whether
there is a significant difference in the performance of either.

With the declared cursor, perhaps opening, and using a 'while' loop to
fetch each record, what would be the main differences in the author
using a FOR statement in lieu thereof ? The results of the FOR
statement are seemingly to perform a significant number of inserts or
updates after modifying several variables.

I know the insert /update performance will depend on many other
underlying factors, but for management of the overall record set,
which would be the preferred manner ? I could see it being done either
way to be honest.

As an aside, the author obviously never chose to use any dynamic SQL
or to use parameter variable inserts [ ? ] as they are all presently
'values' and variables either determined by function call or set from
select statements.

My recent readings lead me to believe this code is somewhat [very]
inefficient in its present state and could do with a serious overhaul.

Any insight or guidance for overall efficiency would be appreciated.

Many thanks,

Tim
Nov 12 '05 #1
1 2874
Tim,

A FOR loop is defined as a cursor internally.
There are in fact some advantages of using FOR loops, especially when
you add a NOT FOUND conditionhandler and teh needed IF THEN ELSE logic
to break out of a driving for a declared curor.
Letting DB2 set all this up (when using a FOR) is more efficient.
Also a FOR is easier to maintain since you don't need to declare the
variables to fetch into. You simply refer to the FOR loop variable.

regarding values vs. hostvariables for embedded SQL I would actually
prefer using the values since thsi way you give the optimizer the best
information to chew on, and little do you care if the procedure compiles
a tad longer or how big the package is.

The interesting quetsion is:
Is the logic smart or dumb?
Can the cursor/FOR be rewritten as a single SQL Statement (searched
update/insert from subselect)? Can insert statements be combined?
Note that DB2 supports multi row VALUES:
INSERT INTO T VALUES <row1>, <row2>, .....;
This is a lot faster than the typical single row inerts you see often.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Tran | last post by:
I built a system using mysql innodb to archive SMS messages but the innodb databases are not keeping up with the number of SMS messages coming in. I'm looking for performance of 200 msgs/sec where...
1
by: Jim | last post by:
I having a difficult time here trying to figure out what to do here. I need a way to scroll through a recordset and display the results with both forward and backward movement on a web page(PHP...
7
by: Michael Beumers | last post by:
Hello NG I've defined a cursor like the following in my COBOL Programm: DECLARE testc CURSOR FOR SELECT ... FROM ... WHERE field1 LIKE :hostvariable1 field2 LIKE :hostvariable2
7
by: P. Adhia | last post by:
Sorry for quoting an old post and probably I am reading out of context so my concern is unfounded. But I would appreciate if I can get someone or Serge to confirm. Also unlike the question asked in...
2
by: Chris Zopers | last post by:
Hello, I've created a stored procedure that loops through a cursor, with the following example code: DECLARE curPeriod CURSOR LOCAL for SELECT * FROM tblPeriods DECLARE @intYear smallint...
8
by: spider007 | last post by:
Hi all, I have a peculiar problem. I have a query which returns multiple rows. I want to perform some operations on the output row by row. For that, i declared a cursor and fetching the rows. Now...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
4
by: invy | last post by:
Hi, I'm experiencing a problem while declaring scroll cursor. I'm using Embeded SQL with C. my code is given below. #ifdef SQLSRVR_RDBMS EXEC SQL DECLARE CUR_DWNLD SCROLL CURSOR FOR SCURS;...
0
debasisdas
by: debasisdas | last post by:
This thread contains some useful tips/samples regarding some advance concepts in cursors. FEW MORE EXAMPLES =================== declare er emp%rowtype; cursor c1 is select * from emp; begin...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.