Unusual result

gaurav.shetti
Posts: 119
Joined: Fri Feb 27, 2009 9:09 am

Unusual result

Post by gaurav.shetti »

Hi,
Can you please help me explain this behaviour.
I had written the following script
<$cnt = 0>
<sql ROW "select id , forms_text, case_id from tre">
<$forms_text = ( $forms_text + " " + $case_id )>
<sql "update tre set forms_text = $forms_text where id = $id ">
</sql>
<$cnt = (1 + $cnt)>
<!--<WRITE $file_case1>$cnt</WRITE>-->
Record processed for case with case_id : $case_id
$cnt records processed
</sql>

where id is counter, formstext is varchar(4000) and case_id is long.


Now the table tre contains the following 2 records
id forms_text case_id
------------+------------+------------+
4a0155b40 abc 527174
4a0155b60 pqr 527174

.
If i run the above script, instead of showing 2 records processed, it is going into an infinite loop. Can you explain the behaviour of it.
thanks
gaurav.shetti
Posts: 119
Joined: Fri Feb 27, 2009 9:09 am

Unusual result

Post by gaurav.shetti »

in case the table is not clear ...
form_text is abc and pqr respectively
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Unusual result

Post by mark »

It's not particularly safe to change the table you're serching within the search. In this case you're scanning the table begin to end. For each record you're expanding the storage required so it moves to the end and eventually gets selected again.

Perhaps you want something more like
<sql novars "update tre set forms_text=forms_text+' '+case_id"></sql>
$loop records processed
gaurav.shetti
Posts: 119
Joined: Fri Feb 27, 2009 9:09 am

Unusual result

Post by gaurav.shetti »

I did not get what you are trying to say.
This updation works fine if all the case_ids are unique. But it goes haywire once it receives duplicate entries
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Unusual result

Post by mark »

You were "lucky" I guess. I'd expect it go haywire regardless. You should not nest an update of a table inside a select from the same table unless the update will only affect fixed size data like ints and floats etc.
gaurav.shetti
Posts: 119
Joined: Fri Feb 27, 2009 9:09 am

Unusual result

Post by gaurav.shetti »

Oh is there a logical explaination to it. I mean, i did not get the reasoning behind this happening. How is the same data being fetched again when it has already been processed. I saw it this way. The outer sql loop acts as a for loop . It picks a data and updates the same data but then theres i++, a new data is being fetched. How can it fetch the same data again ??
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Unusual result

Post by mark »

The outer loop doesn't prefetch everything. It fetches one row processes the loop and moves to the next row and there's no index involved in your query so it's a linear scan over the table. Imagine a compact table with 2 records a and b and empty space x.

a
b

select first row
-> a
b

update expanding a so it won't fit there any more.

-> x
b
aa

get next next row

x
-> b
aa

update expanding b

x
-> x
aa
bb

get next row

x
x
-> aa
bb

etc.
gaurav.shetti
Posts: 119
Joined: Fri Feb 27, 2009 9:09 am

Unusual result

Post by gaurav.shetti »

thats a superb explaination ... What do you mean by expanding a. Does that mean, a with the newly updated data and how would that move 2 records of a below ?
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Unusual result

Post by mark »

By expanding I mean adding to the text length of the record. In this example "a" became "aa". I guess I wasn't entirely clear. "aa" is the modified record "a" not 2 "a" records. In the example above each row is 1 record.
gaurav.shetti
Posts: 119
Joined: Fri Feb 27, 2009 9:09 am

Unusual result

Post by gaurav.shetti »

Oh so you mean to say, once the row is modified, it again gets fetched at the end. So it this what you called as weird behavior ? :)
Post Reply