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 回答
0

在 while 循环之外获取检索列索引:

Cursor cursor = dbHelper.getFollowedValuesForCalendar();

if (cursor !=null) {

    if (!cursor.moveToFirst()) { cursor.close(); return; }

    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));

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

    cursor.close();

}

作为一般经验法则,您应该始终尝试删除在循环之外创建的变量/对象。这提高了性能并减少了内存使用量。

于 2017-06-24T01:49:22.290 回答