Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
SQLite autoincrement: good, bad, or in-between?
4 points by prirun on March 29, 2022 | hide | past | favorite | 1 comment
The SQLite site gives advice to avoid autoincrement because it is slow. The first sentence of this page says:

"The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed."

https://www.sqlite.org/autoinc.html

Since HashBackup (I'm the author) uses autoincrement on a few tables, I thought it would be a good idea to find out about its performance.

I ran 3 tests, all inserting 1M rows into a table with Python.

Test 1: insert using autoincrement. Time: 2.36s, 25% slower than test 2

Test 2: insert using an increasing integer supplied on the insert. Time: 1.89s, fastest

Test 3: insert using a null value - the recommended way. Time: 2.73s, 44% slower than test 2

The SQlite autoincrement page says:

"But if your application does not need these properties [of autoincrement], you should probably stay with the default behavior since the use of AUTOINCREMENT requires additional work to be done as each row is inserted and thus causes INSERTs to run a little slower."

What is surprising is that it doesn't spell out that inserts with no primary key specified run even slower than autoincrement inserts.

So I feel better knowing autoincrement is not so bad, no primary key specified is worse, but I could get a little speed-up specifying the key myself.



A correction: Test 1 was using autoincrement but was also specifying the primary key. If modified to not specify the primary key, like test 3, it is indeed the slowest at 3.21s.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: