SQLites stores table records in a B+ tree, sorted by rowid
.
When you are inserting with an automatically generated rowid
, all records are just appended at the end of the table.
However, when deleting, SQLite has to search for the record first. This is slow if the id
column is not indexed; either create an explicit index (as proposed by John), or declare the column as INTEGER PRIMARY KEY
to make it the rowid.
Inserting with an index becomes faster if you don't use the index, i.e., if you create the index only after bulk inserts.
Your last delete command deletes all records at once. If you know that you're deleting all records in the table, you could speed it up even further by using just DELETE FROM testing
, which doesn't need to look at any records at all.