android - 在 Android 上使用 SQLite 的 AUTOINCREMENT 的开销是多少?

标签 android android-sqlite auto-increment

在 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 )

    我得到的结果是:-

enter image description here

当所有内容仅在 1 个事务中运行时(即在任何插入之前使用 setTransactionSuccessful();endTransaction 运行 beginTransaction(); ),结果会更有利(); 在所有插入之后(对于所有表,即全部 150,000 个插入),例如:-

enter image description here

比较两个表,突出显示使用事务对性能带来的好处。

关于android - 在 Android 上使用 SQLite 的 AUTOINCREMENT 的开销是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384919/

相关文章:

java - H2数据库自动递增id在第一次输入后具有随机值

android - Android 中动态更新启动器图标

java - 在 Sqlite 中编写

java - 如何更改主键自动递增,以便在删除行时不会跳过值

java - 我的应用程序不断崩溃,SQLite 数据库泄漏错误

android - 如何根据另一行的更改更新一行?

mysql - 在Mysql中设置不同的自动增量值

android - PreferenceActivity 中的启用和禁用选项

使用媒体播放器播放音频文件的 Android 问题

android - 如何在 Android 中查找来自相机的输入 fps