【问题标题】:How can i reduce time in fetching of data from SQLite cursor?如何减少从 SQLite 游标获取数据的时间?
【发布时间】:2017-06-24 09:49:22
【问题描述】:

我正在使用以下代码从 SQlite 获取数据,我很快就在光标中获得了完美的数据。
当我迭代游标时,从游标中获取 31 条记录需要 >10 秒。
我的查询需要 0.016150 秒来执行。

我怎样才能将这个时间减少到

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

【问题讨论】:

  • 你应该先优化 SQL 查询
  • 您好 Paresh 感谢您的回复...我的查询需要 0.016150 秒的时间来执行。
  • @ParmarS :不完全是。 query 方法返回很快,因为它只计算查询的一部分,而其余部分在游标操作期间完成。请发布您的查询和您的数据库创建查询。

标签: android sqlite


【解决方案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();

此外,如果您已经知道列的索引,那么您可以简单地从while 中取出cursor.getColumnIndex 以进一步优化。

【讨论】:

    【解决方案2】:

    在 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();
    
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 2016-12-21
      相关资源
      最近更新 更多