2013-10-07 59 views
2

我正在使用以下代碼從SQlite獲取數據,我非常快速地在光標中獲得了完美的數據。
當我迭代遊標時,需要10秒鐘才能從遊標中獲取31條記錄。
我的查詢需要0.016150秒的時間才能執行。

如何將此時間減少到< 1秒?如何減少從SQLite光標讀取數據的時間?

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

您應該先優化SQL查詢 –

+0

您好帕雷希感謝您的回覆...我的查詢需要時間0.016150秒執行。 –

+0

@ParmarS:不完全。查詢方法快速返回,因爲它僅計算查詢的一部分,而其餘部分在遊標操作期間完成。請發佈您的查詢和您的數據庫創建查詢。 – njzk2

回答

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進一步優化。

+0

此代碼可以幫助但不會減少我所假設的時間。 –

0

以檢索列索引的,而循環之外:

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

} 

作爲一個一般的經驗法則,你應該總是嘗試刪除變量/對象創建循環外。這可以提高性能並減少內存使用量。

相關問題