If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > Foreach - best and most effective rewrite in SQL?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-28-11, 04:50
Musil David Musil David is offline
Registered User
 
Join Date: Sep 2006
Location: Czech Republic
Posts: 82
Foreach - best and most effective rewrite in SQL?

I am coding application according design and many time there is need to rewrite commands like these:

foreach ( select @x = col ... from ... )
update, insert, exec...etc

endforeach

I d have some philosophical questions...

1) what do you use to rewrite this ? E.g. you automative, without thinking, write cursor according your template - it is fastest, easy and effective way how to re-write this

2) you use other way cos you concern about amount of written code and his simply fast re-reading in the future by other programmers ? E.g. cursor are heavy, lot of code etc. so we use simply temp table with indexed column and counter...

3) what concerns of effectivity and resource cost? You mostly use cursors/ temporary tables/db work tables cos ...... .

Conlusion first we use .... and if we see during testing problems we rewrite and use this ...

Thanks for your hints and points of view...
Reply With Quote
  #2 (permalink)  
Old 03-28-11, 10:29
trvishi trvishi is offline
Registered User
 
Join Date: Sep 2003
Location: Switzerland
Posts: 443
The basic thumb rule for databases is the more you batch, the better your performance is. Which translates to

a) Temp tables are more batch based.
b) Cursors are more row based, so less throughput. Wherever you can avoid cursors, it would be prefereble.
Reply With Quote
  #3 (permalink)  
Old 03-28-11, 12:03
Musil David Musil David is offline
Registered User
 
Join Date: Sep 2006
Location: Czech Republic
Posts: 82
is temp tables based solution better solution even if I am going through temp table row-by-row with counter variable holding index column...?
Reply With Quote
  #4 (permalink)  
Old 03-28-11, 13:21
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Cursors are always going to be slow and difficult to read but if the application works and it's fast enough then why bother changing it? At best it will be wasted effort and at worst you'll just be introducing new errors.

If the application too slow then only change those cursors that affect the performance most.

If money is no object then sure - get rid of all the cursors
__________________
Mike
Reply With Quote
  #5 (permalink)  
Old 03-29-11, 06:00
Musil David Musil David is offline
Registered User
 
Join Date: Sep 2006
Location: Czech Republic
Posts: 82
Thanks for tips and hints

Problem is that I am almost ever in transaction (where DDL is prohibited, so create table is disabled in tran). Moreover working on just part of large application in deep level on one called stored procedure.

So, if I implement new code with foreach... it seems for me most simple/fast to change just one procedure and implement cursor... otherwise I d have to look for all callings of my procedure and spoting where transaction starts in these procedures and implement create temp table before begin tran in every calling...

And instead one stored proc simple test all these changed procedures I d have to test subseqently...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On