2

次のコードを使用して SQlite からデータを取得しています。カーソルで完全なデータをすばやく取得しました。
カーソルを繰り返しているとき、カーソルから 31 レコードを取得するのに 10 秒以上かかります。
私のクエリの実行には 0.016150 秒かかります。

この時間を 1 秒未満に短縮するにはどうすればよいですか?

Cursor cursor = dbHelper.getFollowedValuesForCalendar();

    if(cursor!=null && cursor.getCount()>0)
    {
        cursor.moveToFirst();

        int followedDateIndex   = cursor.getColumnIndex(Info.FOLLOWED_DATE);
        int followedCountIndex  = cursor.getColumnIndex(Info.FOLLOWED_COUNT);
        int successValueIndex   = cursor.getColumnIndex(Info.SUCCESS_VALUE);
        int isEnableIndex       = cursor.getColumnIndex(Info.IS_ENABLE);
        do
        {       
            Info.previousFollowedDaysList.add(cursor.getString(followedDateIndex)); 
            Info.previousFollowedValuesList.add(cursor.getInt(followedCountIndex));
            Info.successValuesList.add(cursor.getInt(successValueIndex));
            Info.isEnableList.add(cursor.getInt(isEnableIndex));
        }while(cursor.moveToNext());
    }
    else
    {
        //System.out.println(" Records between dates ==============>>>> 0");
    }

    if(cursor!=null)
        cursor.close();
4

2 に答える 2

1

getCount()高価な呼び出しであるため、不必要にカーソルを呼び出すべきではありません。これを読んでください。

代わりに、以下のようにコードを変更することをお勧めします。

Cursor cursor = dbHelper.getFollowedValuesForCalendar();

while(cursor != null && cursor.moveToNext()) {
    int followedDateIndex   = cursor.getColumnIndex(Info.FOLLOWED_DATE);
    int followedCountIndex  = cursor.getColumnIndex(Info.FOLLOWED_COUNT);
    int successValueIndex   = cursor.getColumnIndex(Info.SUCCESS_VALUE);
    int isEnableIndex       = cursor.getColumnIndex(Info.IS_ENABLE);

    Info.previousFollowedDaysList.add(cursor.getString(followedDateIndex)); 
    Info.previousFollowedValuesList.add(cursor.getInt(followedCountIndex));
    Info.successValuesList.add(cursor.getInt(successValueIndex));
    Info.isEnableList.add(cursor.getInt(isEnableIndex));
}

if(cursor!=null)
    cursor.close();

さらに、列のインデックスが既にわかっている場合は、さらに最適化cursor.getColumnIndexするために単に取り出します。while

于 2013-10-07T10:17:02.650 に答える