在 SQLite 文档中,它包括以下内容:-
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.
和
The behavior implemented by the AUTOINCREMENT keyword is subtly different from the default behavior. With AUTOINCREMENT, rows with automatically selected ROWIDs are guaranteed to have ROWIDs that have never been used before by the same table in the same database. And the automatically generated ROWIDs are guaranteed to be monotonically increasing. These are important properties in certain applications. But if your application does not need these properties, 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.
以上引述来自SQLite Autoincrement
那么预计会产生什么样的影响?自动增量会慢多少?
最佳答案
我的估计是,我不是统计学家,开销会慢8-12%。
我使用 3 结构相似且简单的表(具有两个 TEXT 列)获得结果,每个表运行 10,000 次插入,重复这 5 次4 台设备上的时间。
表 1(Dflt 列)仅使用两个 TEXT 列创建(因此使用默认 ROWID)。
表 2(AI 列) 是使用 _id INTEGER PRIMARY KEY AUTOINCRMENT
除了两个 TEXT 列之外创建的。
表 3(无 AI 列)是除了两个 TEXT 列之外还使用 _id INTEGER PRIMARY KEY
创建的。
因此,表 2 使用稍微不同的 ROWID 选择算法进行插入。
使用的四种设备是:-
(1) Genymotion 模拟设备(自定义平板电脑 - 5.1.0 - API 22 - 1536x2048 )
(2) Onix 10 英寸平板电脑 (AT101-1116 )
(3) HTC 1 M8 (HTC_0PKV1 )
(4) 一台 Lenovo A10-30 平板电脑(Lenovo TB2-X30F )
我得到的结果是:-
当所有内容仅在 1 个事务中运行时(即在任何插入之前使用 setTransactionSuccessful();
和 endTransaction 运行
在所有插入之后(对于所有表,即全部 150,000 个插入),例如:-beginTransaction();
),结果会更有利();
比较两个表,突出显示使用事务对性能带来的好处。
关于android - 在 Android 上使用 SQLite 的 AUTOINCREMENT 的开销是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384919/