gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:33:21 24 4
gpt4 key购买 nike

在 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

那么预计会有什么样的影响,AUTOINCREMENT 会慢多少?

最佳答案

我的估计是开销大约慢了 8-12%

我使用 3 结构相似且简单的 获得结果,其中包含两个 TEXT 列,每个表运行 10,000 插入,重复这 5 4 台设备上的次数。

表 1(Dflt 列) 仅使用两个 TEXT 列创建(因此使用默认 ROWID)。

表 2(AI 列) 除了两个 TEXT 列之外,还使用 ​​_id INTEGER PRIMARY KEY AUTOINCREMENT 创建。

表 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) 联想 A10-30 平板电脑一台(联想 TB2-X30F)

    我得到的结果是:-

enter image description here

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

enter image description here

两个表的比较,突出了使用事务对性能的好处。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com