The pros and cons of SQLite using AutoIncrement

Source: Internet
Author: User



In project use we often need an auto-growing primary key, such as adding a autoincrement constraint to make a primary key grow each time the data is inserted. Since you typically create a table with a field that becomes ROWID by default (unless declared as without rowid), the integer primary key you add is actually a rowid alias. But if you add the AutoIncrement constraint, the database engine generates ROWID's algorithm that changes accordingly.



Their generation algorithm is as follows



Integer PRIMARY key:



Gets the maximum value of the current column, if the maximum value of the 64-bit signed integer is not reached, then on the basis of the maximum value of +1 as the new value, if the maximum value has been reached, then re-randomly looking for an unused, that is, you delete a row of the unused value. If it is no longer possible to insert, the Sqlite_full error is returned.



Integer PRIMARY key AutoIncrement:



Gets the current column maximum value, or 1 if the maximum value is not reached, or Sqlite_full is returned.



The integer primary key is automatically incremented by default, and using AutoIncrement guarantees that the newly inserted data will be larger than before, but it also brings a lot of extra consumption, consumes extra CPU, disk space, and the overhead of I/O disk operations. The recommendation from the official website is that it should be avoided if there is no strict need, usually it is not necessary.






Here are some of the data I tested:


insert operation
Turn off transaction processing
100W data not autoincrement 74.7088160515 s
100W data autoincrement 91.2676999569 s
Turn things on
100W data not autoincrement 69.6123759747 s
100W data autoincrement 86.1955218315 s 












The pros and cons of SQLite using AutoIncrement


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.