I came across some strange behaviour in SQLite - when trying to insert a row to a table that already has a certain value (and adding another one would abuse the unique constraint), gaps in autoincremented ids appear. I then started to read the documentation and here is the excerpt that I found:
Note that "monotonically increasing" does not imply that the ROWID always increases by exactly one. One is the usual increment. However, if an insert fails due to (for example) a uniqueness constraint, the ROWID of the failed insertion attempt might not be reused on subsequent inserts, resulting in gaps in the ROWID sequence. AUTOINCREMENT guarantees that automatically chosen ROWIDs will be increasing but not that they will be sequential. source: http://ift.tt/1xuL9jh
My problem: after a lot of failed additions I get the ROWID higher and higher. After some time I will end up with really huge numbers, although the total number of rows will be relatively small. Thus I will have to use lots of longs to fit. This on the other hand will result in a worse performance - I will generally use more memory - both on the stack and on the heap - which could negatively impact performance.
My question: is there a way to insert rows to the table so that they always take the smallest available id?
Note: If this is helpful, I never delete any rows.
Potential solution: It works, but I don't really feel it is right. The idea is to execute this from time to time:
UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='table_name';
Aucun commentaire:
Enregistrer un commentaire