gpt4 book ai didi

ios - sqlite优化读取性能

转载 作者:行者123 更新时间:2023-11-29 04:45:12 26 4
gpt4 key购买 nike

我在 iPhone 应用程序中使用 sqlite 作为只读数据库。一个用例涉及发出大量 select 语句,每个语句返回大约 3 行。减少查询次数是不可能的,因为下一个查询的参数取决于上一个查询的结果。查询本身非常简单:

SELECT int1, int2, int3, int4 , int5, int6, int7 FROM sometable WHERE (int1 = ? AND int2 = ?) or (int3 = ? and int4 = ?) ORDER BY ROWID

该表有一个索引 (int1, int2) 和一个索引 (int3, int4)。所有 int 的数据类型均为 INTEGER

查询是通过C-API完成的。语句使用 sqlite3_prepare_v2() 编译并用于所有查询。每次查询后,在绑定(bind)新参数之前,都会执行语句上的 sqlite3_reset()。

数据库文件使用标志 SQLITE_OPEN_READONLY 和 SQLITE_OPEN_NOMUTEX 打开。

iPhone 上的分析显示大部分时间花费在 sqlite3_step() -> sqlite3VdbeExec->sqlite3BtreeBeginTrans->sqlite3PagerSharedLock->line pVfs->xAccess()

我不是 sqlite 专家,但对我来说,看起来时间浪费在不必要的锁定上。不需要,因为可以保证在完成此查询时不会对数据库进行其他访问。我还想知道 sqlite3BtreeBeginTrans。是否为 select 语句创建事务?

谁能告诉我如何进一步优化它?

最佳答案

正确答案来自 sqlite-user mailing list就是使用EXCLUSIVE锁定模式:

There are three reasons to set the locking-mode to EXCLUSIVE: [...] 2) The number of system calls for filesystem operations is reduced, possibly resulting in a small performance increase.

加速约 40%...

关于ios - sqlite优化读取性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9760919/

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