gpt4 book ai didi

iphone - 在 iPhone 上将多行插入 sqlite 数据库的最快方法

转载 作者:行者123 更新时间:2023-12-03 18:25:20 25 4
gpt4 key购买 nike

有人可以解释一下使用 FMDB 在 iPhone 上插入大量数据的最佳方法是什么吗?我看到类似使用 beginTransaction 命令之类的东西。老实说,我不确定 this 或 setShouldCacheStatements 的作用。我按照我的同事到目前为止所做的代码进行操作,如下所示:

BOOL oldshouldcachestatements = _db.shouldCacheStatements;
[_db setShouldCacheStatements:YES];
[_db beginTransaction];
NSString *insertQuery = [[NSString alloc] initWithFormat:@"INSERT INTO %@ values(null, ?, ?, ?, ?, ?, ?, ?);", tableName];
[tableName release];
BOOL success;

bPtr += 2;
int *iPtr = (int *)bPtr;
int numRecords = *iPtr++;

for (NSInteger record = 0; record < numRecords; record++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Some business logic to read the binary stream

NSNumber *freq = aFreq > 0 ? [NSNumber numberWithDouble:100 * aFreq / 32768]: [NSNumber numberWithDouble:-1.0];

// these fields were calculated in the business logic section
success = [_db executeUpdate:insertQuery,
cID,
[NSNumber numberWithInt:position],
[NSString stringWithFormat:@"%@%@", [self stringForTypeA:typeA], [self stringForTypeB:typeB]], // methods are switch statements that look up the decimal number and return a string
[NSString stringWithFormat:@"r%i", rID],
[self stringForOriginal:original],
[self stringForModified:modified],
freq];

[pool drain];
}
[outerPool drain];

[_db commit];
[_db setShouldCacheStatements:oldshouldcachestatements];

这是我能做到的最快速度吗?写法是sqlite的限制吗?我看到了这个:http://www.sqlite.org/faq.html#q19并且不确定这个实现对于 fmdb 是否是最好的,或者是否还有其他我可以做的事情。其他一些同事提到了一些关于批量插入和优化的事情,但我不确定这意味着什么,因为这是我第一次遇到 sqlite。我可以研究什么想法或方向吗?谢谢!

最佳答案

首先,大多数情况下你不必担心sqlite3的性能,如果你没有完全错误地使用它。

以下因素可提高 INSERT 语句的性能:

交易

正如您已经提到的,交易是最重要的功能。特别是如果您有大量查询,事务将将您的 INSERT 速度提高约 10 倍

PRAGMA 配置

Sqlite3 提供了多种机制来避免在最坏的情况下数据库损坏。在某些场景下,这是不需要的。在其他方面,这是绝对必要的。以下 sqlite3 命令可能会加快 INSERT 语句的速度。应用程序的正常崩溃不会损坏数据库,但操作系统崩溃可能会损坏数据库。

PRAGMA synchronous=OFF -- may cause corruption if the OS fails
PRAGMA journal_mode=MEMORY -- Insert statements will be written to the disk at the end of the transaction
PRAGMA cache_size = 4000 -- If your SELECTs are really big, you may need to increase the cache
PRAGMA temp_store=MEMORY -- Attention: increases RAM use

停用索引

任何 SQL 索引都会减慢 INSERT 语句的速度。检查您的表是否有一些索引:

.indices <table_name>

如果是,则在事务后删除INDEXCREATE它。

单选

在生成新数据时,我没有看到使用批量插入的方法。不过,您可以收集数据并仅执行一个 INSERT 语句。这可能会显着提高您的性能,但也会增加失败的可能性(例如语法)。一种黑客遇到另一种黑客:由于 sqlite3 不直接支持这一点,因此您必须使用 UNION 命令来相应地收集所有插入语句。

INSERT INTO 'tablename'
      SELECT 'data1' AS 'column1', 'data2' AS 'column2'
UNION SELECT 'data3', 'data4'
UNION SELECT 'data5', 'data6'
UNION SELECT 'data7', 'data8'

语句缓存

我建议避免使用语句缓存,因为有 unfixed issue有了这个功能(据我所知,它不会显着影响性能)。

内存管理

我想提的最后一点是关于 ObjC 的。与基本操作相比,内存管理需要非常非常多的时间。也许您可以通过在循环外准备这些变量来避免一些 stringWithFormat:numberWithDouble:

摘要

总而言之,如果你只是使用事务,我认为sqlite3的速度不会有问题。

关于iphone - 在 iPhone 上将多行插入 sqlite 数据库的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11784143/

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