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

在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个插入,在4个设备上重复这5次.

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

表2(AI列)是使用_id INTEGER PRIMARY KEY AUTOINCREMENT以及两个TEXT列创建的.

除了两个TEXT列之外,还使用_id INTEGER PRIMARY KEY创建了表3(无AI列).

因此,表2使用稍微不同的ROWID选择算法进行插入.

使用的四种设备是: –

>(1)Genymotion仿真设备(Custom Tablet – 5.1.0 – API 22 – 1536×2048
)
>(2)Onix 10“平板电脑(AT101-1116
)
>(3)HTC 1 M8(HTC_0PKV1
)
>(4)Lenevo A10-30平板电脑(联想TB2-X30F
)

我得到的结果是: –

enter image description here

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

enter image description here

这两个表的比较强调了使用事务可以对性能产生的好处.

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...