Android SQL Lite查询速度很慢

时间:2016-01-29 16:40:23

标签: android sqlite

我开发了一个这个函数,用于搜索单个元素在20 km范围内的所有元素进入

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记录whitout索引。 结果显示在列表中 我写了错误的代码?

由于

1 个答案:

答案 0 :(得分:0)

您应该在C中创建一个SQLite距离函数(这样距离计算会更快)并将其用作查询中的过滤器来限制结果。 查看示例(适用于iOS):http://www.thismuchiknow.co.uk/?p=71