Could someone please explain why is the second update block 20 times faster then the first block?
The only difference is the placement of ttsBegin and ttsCommit.
I have read that best practice is to have transactions as small as possible.
What exactly is causing the big difference in speed?
This is slower:
while select forUpdate someTable where someTable.Field1 == “12345”
{
ttsBegin;
calcTrans.Field2 = “testing”;
calcTrans.doUpdate();
ttsCommit;
}
This is much faster:
ttsBegin;
while select forUpdate someTable where someTable.Field1 == “12345”
{
calcTrans.Field2 = “testing”;
calcTrans.doUpdate();
}
ttsCommit;
Each case fetch record by record, but in the first case, for each loop, a connection to database is open and closed (ttsbegin and ttscommit), in the second case, a single connection is used, you call ttsbegin and ttscommit only once, so it’s faster
Yes, you are opening and closing your transactions every time in the first case. So that means when you update 20 rows, you will have 20 transactions instead of 1.