8

サーバーに送信されるのを待っている間、データベースにイベントを保存するために外部ストレージを使用しています。

レコードを挿入すると、パフォーマンスが非常に悪くなります。外部メモリが遅くなる可能性があることは知っていますが、いくつかの数値を確認したかったので、それをテストする小さなアプリを作成しました。

コードは次のとおりです。

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、内部メモリ

      方法| レコード| 時間(ミリ秒)| 1秒あたりのレコード
------------- + --------- + ----------- + -------------- ------
      通常| 100 | 2072 | 48.26
InsertHelper | 100 | 1662 | 60.17


Nexus One、外部メモリ:

      方法| レコード| 時間(ミリ秒)| 1秒あたりのレコード
------------- + --------- + ----------- + -------------- ------
      通常| 100 | 7390 | 13.53
InsertHelper | 100 | 7152 | 13.98


エミュレータ、内部メモリ:

      方法| レコード| 時間(ミリ秒)| 1秒あたりのレコード
------------- + --------- + ----------- + -------------- ------
      通常| 100 | 1803 | 55.46
InsertHelper | 100 | 3075 | 32.52


エミュレータ、外部メモリ:

      方法| レコード| 時間(ミリ秒)| 1秒あたりのレコード
------------- + --------- + ----------- + -------------- ------
      通常| 100 | 5742 | 17.42
InsertHelper | 100 | 7164 | 13.96

ご覧のとおり、エミュレータは信頼できないので、InsertHelperどちらかといえば高速である必要があります。
もちろん、これは当然のことですが、テストはほとんど好奇心から行われました。

しかし、私が心配しているのは、外部メモリを使用しているときの電話のパフォーマンスの悪さです。私はいくつかの重要な側面を見逃しましたか、SQLiteDatabaseそれとも単にSDカードが遅くなるためですか?

実際のアプリでロックを無効にしたことを追加できますが、ほとんど違いはありません。

4

2 に答える 2

14

CommonsWareのコメントは正しいです。dbのパフォーマンスに大きな違いをもたらすのは、トランザクションの使用です。挿入ループをトランザクションでラップします。InsertHelperで機能するかどうかは100%わかりませんが、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();
}
于 2011-06-15T01:10:30.830 に答える
2

dbのパフォーマンスに問題があるため、コードを使用してシステムの1秒あたりの挿入数を測定しました。ただし、{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

そしてサムスンギャラクシーノート:

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
于 2012-03-21T20:36:14.563 に答える