我想多次将记录插入表中,我可以选择使用compileStatement或使用db.insert,如下所示.
String TABLENAME = "table"; //fields in table: id, name
SQLiteStatement statement = db.compileStatement("INSERT INTO "+TABLENAME+" VALUES(?,?);");
statement.bindLong(1,666);
statement.bindString(2,"john");
statement.executeInsert();
要么
String TABLENAME = "table";
ContentValues values = new ContentValues();
values.put("id", 666);
values.put("name", "john");
db.insert(TABLENAME, null, values);
哪一个应该是最佳的?
编辑:-
我正在运行的应用程序是单线程的.
解决方法:
insert()在每次调用时编译SQL,而compileStatement()方法只编译一次SQL.当多次使用具有不同绑定参数的相同SQL时,compileStatement()方法执行的工作更少,速度更快.
此外,考虑在事务中包装插入以减少等待I / O所花费的时间.
标签:android,android-sqlite,sqlite3,query-optimization
来源: https://codeday.me/bug/20190708/1405832.html