我正在使用外部存储器在等待发送到服务器时将事件存储在数据库中。
我在插入记录时看到了非常糟糕的表现。 我知道外部存储器可能很慢,但我想看一些数字,所以我写了一个测试它的小应用程序。
以下是代码:
public static final int INSERTS = 100;
File dbFile = new File(Environment.getExternalStorageDirectory(), "test.sqlite3");
// File dbFile = new File(getFilesDir(), "test.sqlite3");
dbFile.delete();
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
db.execSQL("CREATE TABLE events (_id integer primary key autoincrement, event_type TEXT NOT NULL, timestamp BIGINT, data TEXT);");
db.execSQL("CREATE INDEX mainIndex ON events (event_type, timestamp ASC);");
InsertHelper helper = new InsertHelper(db, "events");
final int eventTypeCol = helper.getColumnIndex("event_type");
final int timestampCol = helper.getColumnIndex("timestamp");
final int dataCol = helper.getColumnIndex("data");
long start = System.currentTimeMillis();
String eventType = "foo", data = "bar";
long timestamp = 4711;
for(int i = 0; i < INSERTS; ++i) {
helper.prepareForInsert();
helper.bind(eventTypeCol, eventType);
helper.bind(timestampCol, timestamp);
helper.bind(dataCol, data);
helper.execute();
}
long end = System.currentTimeMillis();
Log.i("Test", String.format("InsertHelper, Speed: %d ms, Records per second: %.2f", (int)(end-start), 1000*(double)INSERTS/(double)(end-start)));
db.close();
dbFile.delete();
db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
db.execSQL("CREATE TABLE events (_id integer primary key autoincrement, event_type TEXT NOT NULL, timestamp BIGINT, data TEXT);");
db.execSQL("CREATE INDEX mainIndex ON events (event_type, timestamp ASC);");
start = System.currentTimeMillis();
ContentValues cv = new ContentValues();
for(int i = 0; i < INSERTS; ++i) {
cv.put("event_type", eventType);
cv.put("timestamp", timestamp);
cv.put("data", data);
db.insert("events", null, cv);
}
end = System.currentTimeMillis();
Log.i("Test", String.format("Normal, Speed: %d ms, Records per second: %.2f", end-start, 1000*(double)INSERTS/(double)(end-start)));
db.close();
dbFile.delete();
数据库与我的真实应用程序使用的数据库完全一样,我尝试删除索引,但没有任何区别。
结果如下:
Nexus One, Internal memory Method | Records | Time (ms) | Records per second -------------+---------+-----------+-------------------- Normal | 100 | 2072 | 48.26 InsertHelper | 100 | 1662 | 60.17 Nexus One, External memory: Method | Records | Time (ms) | Records per second -------------+---------+-----------+-------------------- Normal | 100 | 7390 | 13.53 InsertHelper | 100 | 7152 | 13.98 Emulator, Internal memory: Method | Records | Time (ms) | Records per second -------------+---------+-----------+-------------------- Normal | 100 | 1803 | 55.46 InsertHelper | 100 | 3075 | 32.52 Emulator, External memory: Method | Records | Time (ms) | Records per second -------------+---------+-----------+-------------------- Normal | 100 | 5742 | 17.42 InsertHelper | 100 | 7164 | 13.96
正如您所看到的,模拟器无法信任,InsertHelper
如果有的话应该更快
当然,这是可以预料的,测试主要是出于好奇。
然而,我关注的是使用外部存储器时手机的性能不佳,我是否错过了SQLiteDatabase
的一些关键方面,还是只是为了让SD卡变慢?
我可以在我的真实应用中添加,我已禁用locking,但收效甚微。
答案 0 :(得分:13)
CommonsWare的评论是正确的。对数据库性能产生重大影响的是使用事务。在事务中包装插入循环。我不是100%确定它是否适用于InsertHelper,但你可以尝试用这个替换你的for循环:
db.beginTransaction();
try {
for(int i = 0; i < INSERTS; ++i) {
helper.prepareForInsert();
helper.bind(eventTypeCol, eventType);
helper.bind(timestampCol, timestamp);
helper.bind(dataCol, data);
helper.execute();
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
答案 1 :(得分:2)
我有一些数据库性能问题因此我使用您的代码来测量我系统上的每秒插入数。但我还在{begin,end} Transaction()中添加了包装。
在模拟器中。我得到了:
InsertHelper-Internal-Trans, Speed: 67 ms, Records per second: 1492.54
InsertHelper-External-Trans, Speed: 70 ms, Records per second: 1428.57
Normal-Internal-Trans, Speed: 148 ms, Records per second: 675.68
Normal-External-Trans, Speed: 152 ms, Records per second: 657.89
InsertHelper-Internal-NoTrans, Speed: 514 ms, Records per second: 194.55
Normal-Internal-NoTrans, Speed: 519 ms, Records per second: 192.68
InsertHelper-External-NoTrans, Speed: 590 ms, Records per second: 169.49
Normal-External-NoTrans, Speed: 618 ms, Records per second: 161.81
在三星Galaxy Note上:
InsertHelper-External-Trans, Speed: 52 ms, Records per second: 1923.08
InsertHelper-Internal-Trans, Speed: 52 ms, Records per second: 1923.08
Normal-External-Trans, Speed: 77 ms, Records per second: 1298.70
Normal-Internal-Trans, Speed: 121 ms, Records per second: 826.45
Normal-External-NoTrans, Speed: 4562 ms, Records per second: 21.92
Normal-Internal-NoTrans, Speed: 4855 ms, Records per second: 20.60
InsertHelper-External-NoTrans, Speed: 5997 ms, Records per second: 16.68
InsertHelper-Internal-NoTrans, Speed: 8361 ms, Records per second: 11.96