我正在尝试使用 SQLite Swift 进行批量插入。但是,我不能 100% 确定我的代码是否正确。
我选择的 Swift 代码(因为它在时间上提供了最好的性能)是:
do {
try DB.transaction { () -> Void in
for index in 0...num_docs {
table.insert(data <- "test", data_num <- index)
}
}
} catch _ {
throw DataAccessError.Transaction_Error
}
编辑 - -
如果我在 swift 中使用以下代码,插入 10000 个文档会从 +/- 12 秒下降到 0.8 秒。听起来好得令人难以置信。
let docsTrans = DB.prepare("INSERT INTO TEST_DB (data, data_num) VALUES (?,?)")
do {
try DB.transaction(.Deferred) { () -> Void in
for index in 0...num_docs {
try docsTrans.run("test", index)
}
}
} catch _ {
throw DataAccessError.Insert_Error
}