Pazyryk
Deity
- Joined
- Jun 13, 2008
- Messages
- 3,584
This is an update and correction (a retraction?! goodness me!) for this thread. Strangely, and embarrassingly, I have not been able to reproduce my results below using BEGIN and COMMIT. Not sure why... but after many hours on this I'm pretty sure that BEGIN and COMMIT don't do anything and I was doing something wrong in my time tests.
The good news, however, is that the concept of transactions is correct, just my implementation was wrong. See post #5 to see how to do it.
I tried this with 99% certainty that it wouldn't work. But it did!
So my mod uses SavedGameDB a lot, with 60 or more inserts/updates per turn. This was taking about 10 seconds, which was making me think twice about my whole approach. However, I read that SQLite should be able to do 50000 or so inserts per second, but could only do a few dozen "transactions" per second (although my results indicated 6 transactions per second in a running civ5 game). Each insert, update or deletion is a separate transaction -- unless you wrap them with BEGIN and COMMIT, which makes them all one transaction.
So this takes 10 seconds for roughly 60 updates:
But this takes between 0 and 0.001 seconds to do about 60 updates:
The code above just writes data from a Lua table (target_table) to a DB table in exactly the same structure.
(I'll write a tutorial on SavedGameDB eventually...)
The good news, however, is that the concept of transactions is correct, just my implementation was wrong. See post #5 to see how to do it.
I tried this with 99% certainty that it wouldn't work. But it did!
So my mod uses SavedGameDB a lot, with 60 or more inserts/updates per turn. This was taking about 10 seconds, which was making me think twice about my whole approach. However, I read that SQLite should be able to do 50000 or so inserts per second, but could only do a few dozen "transactions" per second (although my results indicated 6 transactions per second in a running civ5 game). Each insert, update or deletion is a separate transaction -- unless you wrap them with BEGIN and COMMIT, which makes them all one transaction.
So this takes 10 seconds for roughly 60 updates:
Code:
local SavedDB = Modding.OpenSaveData()
...
for i = 1, countUpdates do
local id = markedForUpdate[i]
local quaryStr = "UPDATE "..table_name.." SET "
local bFirst = true
for key, value in pairs(target_table[id]) do
if bFirst then
quaryStr = quaryStr..key.."='"..value.."'"
bFirst = false
else
quaryStr = quaryStr..", "..key.."='"..value.."'"
end
end
local quaryStr = quaryStr.." WHERE ID='"..id.."'"
Dprint (quaryStr)
for row in SavedDB.Query(quaryStr) do end
end
But this takes between 0 and 0.001 seconds to do about 60 updates:
Code:
local SavedDB = Modding.OpenSaveData()
...
for row in SavedDB.Query("BEGIN") do end
for i = 1, countUpdates do
local id = markedForUpdate[i]
local quaryStr = "UPDATE "..table_name.." SET "
local bFirst = true
for key, value in pairs(target_table[id]) do
if bFirst then
quaryStr = quaryStr..key.."='"..value.."'"
bFirst = false
else
quaryStr = quaryStr..", "..key.."='"..value.."'"
end
end
local quaryStr = quaryStr.." WHERE ID='"..id.."'"
Dprint (quaryStr)
for row in SavedDB.Query(quaryStr) do end
end
for row in SavedDB.Query("COMMIT") do end
The code above just writes data from a Lua table (target_table) to a DB table in exactly the same structure.
(I'll write a tutorial on SavedGameDB eventually...)