2016-01-29 49 views
0

我已經開發了這一功能搜索在單個元素的20公里範圍內尊重所有位置元素插入Android的SQL精簡版查詢速度很慢

ArrayList<GeoJsonResponse> result = new ArrayList<GeoJsonResponse>(); 
    Cursor cursor = mDb.query(database_event_schema.TABLE_NAME, null, null, null, null, null, null); 
    String[] colums = new String[]{database_event_schema._EVENT_ID,database_event_schema._AUTHOR, database_event_schema._LAT,database_event_schema._LON,database_event_schema._PLACE,database_event_schema._MAG,database_event_schema._DEPTH,database_event_schema._TIME}; 

    Double lat,lon,magnitude = null,distance = null; 

    if (cursor != null) { 
     while (cursor.moveToNext()) { 
      try{lat = Double.valueOf(cursor.getString(cursor.getColumnIndex(database_event_schema._LAT)));}catch(Exception e){continue;} 
      try{lon = Double.valueOf(cursor.getString(cursor.getColumnIndex(database_event_schema._LON)));}catch(Exception e){continue;} 
      String place = cursor.getString(cursor.getColumnIndex(database_event_schema._PLACE)); 
      double dist_km = Utills.distance(curPos.latitude,curPos.longitude,lat,lon,'K'); 
      if(dist_km>=0 && dist_km<=10) { 
       String id = cursor.getString(cursor.getColumnIndex(database_event_schema._EVENT_ID)); 
       String author = cursor.getString(cursor.getColumnIndex(database_event_schema._AUTHOR)); 
       try{magnitude = Double.valueOf(cursor.getString(cursor.getColumnIndex(database_event_schema._MAG)));}catch(Exception e){} 
       try{distance = Double.valueOf(cursor.getString(cursor.getColumnIndex(database_event_schema._DEPTH)));}catch(Exception e){} 
       String date = cursor.getString(cursor.getColumnIndex(database_event_schema._TIME)); 
       Date dateNew = null; 
       if(!date.equals("")) 
       { 
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
        try { 
         dateNew = format.parse(date); 
        } catch (ParseException e) { 
         // TODO Auto-generated catch block 
        e.printStackTrace(); 
         Log.e("test", e.getLocalizedMessage()); 
        } 
       } 
       result.add(new GeoJsonResponse(id,author,place,magnitude,distance,date,dateNew,lat,lon)); 
      } 
     } 
    } 
    return result; 

但查詢是很慢的。我如何增加查詢速度? 通過此表,查詢選擇所有具有20km位置的元素並選擇事件。該表擁有500.000條記錄的黑白指數。 結果顯示爲列表 我寫了一個錯誤的代碼?

感謝

+0

您可以添加更多關於您試圖從表格和表格結構中獲得的詳細信息 –

回答

0

您應該創建一個在C SQLite的距離函數(所以距離計算會更快),並把它作爲一個過濾器在查詢中限制的結果。 查看示例(適用於iOS):http://www.thismuchiknow.co.uk/?p=71