我在下面所描述的情景,我的问题是,Android的SQLite的有效补充大量的数据:如果不存在
- 我怎样才能让这个更有效?
- 我可以改进表结构吗?
- 我可以改进查询/数据访问和写作吗?
目前它可以解析以每秒大约100个对象。
编辑:通过在“情况1:”上添加InsertHelper,现有值的速度增加了,主要是这种情况。但是,我无法找到如何当你需要“last_insert_rowid()”
数据库结构,使用它的任何资源:
data {
id integer primary key autoincrement
name text not null
...
}
reference {
id integer (same id as in table1, not unique here tho)
source text not null
source_id text not null
}
表“参考”不断从源和ID的引用我的数据库中的id的来源。 'data'表中的每个条目在'reference'表中至少有一个条目。
我有一个JSON流,我一次解析和构建一个对象。流量可以高达几MB,超过8000个对象。构建对象时,它将包含一个引用。
对于每一个对象建我想:
if(reference for this object does not exist){
if(we find the object name in data)
add to reference with the found id then break
else
add to data, get new id, add to reference then break
}
代码看起来(有点伪代码,使其更易于阅读):
beginTransaction();
try {
while(parsing next object is successful){
if("SELECT 1 FROM reference WHERE source = ? and source_id = ?" == null){
Object[] objects = "SELECT * FROM data WHERE name = ?"
switch(objects.length){
case 0:
"INSERT INTO data (name) VALUES(?)"
"INSERT INTO reference (id, source, source_id) VALUES (last_insert_rowid(), ?, ?)"
break;
case 1:
"INSERT INTO reference (id, source, source_id) VALUES (?, ?, ?)"
break;
}
}
}
setTransactionSuccessful();
} finally {
endTransaction();
}