如何减少从SQLite游标中获取数据的时间?

时间:2013-10-07 09:55:39

标签: android sqlite

我正在使用以下代码从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();

2 个答案:

答案 0 :(得分:1)

你不应该不必要地在你的光标上调用getCount(),因为这是一个昂贵的电话。阅读this

相反,我建议你改变你的代码如下:

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以进一步优化。

答案 1 :(得分: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();

}

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