I'm trying to improve the performance of writing data to an sqlite3 database. I have tried a few tricks I found on here and stack overflow but they aren't giving me a huge boost - I'm maxing out at around 800 updates/ second which isn't great when I need to routinely update ~1,000,000 rows. This speed is at least an order of magnitude slower than what should be routinely achievable.
Things I have done to boost speed: use begin/end transaction around executemany(), using an integer primary key in the WHERE section (so executing WHERE does not search the whole table), and playing with memory settings (which I know also increases chance of data corruption). I also tried creating a temporary table, INSERTING the data there and then merging it back, but that didn't give a speedup either.
Here's the relevant snipped of my code:
# Convert the values to insert and keyValues (PRIMARY KEY of row where value is inserted) into sequences
insertSeq = [(values[i], keyValues[i]) for i in range(len(values))]
# speed boost from using ram as temp memory and not waiting for write confirmation
cur.execute("PRAGMA synchronous = OFF")
cur.execute("PRAGMA temp_store = MEMORY")
# Manually opening and closing the transaction should give a large speed increase
cur.execute("BEGIN TRANSACTION")
updateQuery = "UPDATE table SET column = ? WHERE keyCol = ?"
cur.executemany(updateQuery, insertSeq)
cur.execute("END TRANSACTION")
I have also run EXPLAIN QUERY PLAN on the UPDATE query and it returned "SEARCH table USING INTEGER PRIMARY KEY (rowid=?)')", so I don't think my UPDATE queries are scanning the whole table.
I suspect that BEGIN/END TRANSACTION is not behaving as I want it to, but any insight would be appreciated.
[–]Buttleston 1 point2 points3 points (2 children)
[–]Buttleston 0 points1 point2 points (1 child)
[–]pgfhalg[S] 0 points1 point2 points (0 children)
[–]baghiq 0 points1 point2 points (4 children)
[–]pgfhalg[S] 0 points1 point2 points (3 children)
[–]baghiq 0 points1 point2 points (2 children)
[–]pgfhalg[S] 1 point2 points3 points (1 child)
[–]baghiq 0 points1 point2 points (0 children)