【问题标题】:Getting data from sqlite on android is really slow从android上的sqlite获取数据真的很慢
【发布时间】:2013-01-24 03:54:32
【问题描述】:

我有这个代码:

DatabaseHandler db = new DatabaseHandler(this);
System.out.println("Start - " + System.currentTimeMillis());
for (int i = 1; i < db.getChampsCount() + 1; i++) {
    String name = db.getChampInfo(i, "name");
    String title = db.getChampInfo(i, "title");
    String thumb = db.getChampInfo(i, "thumb");
    System.out.println("End - " + System.currentTimeMillis());
    [...]
}

还有这个

String getChampInfo(int id, String col) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("SELECT " + col + " FROM champions WHERE id = " + id, new String[] {});
    if (cursor != null)
        cursor.moveToFirst();

    db.close();
    return cursor.getString(0);
}

DatabaseHelper 类的一部分

它工作正常,但问题是执行时间太长(在我的安卓手机上为 2089 毫秒)。这些字符串是 UI 的一部分,所以我认为我不能把它放在另一个线程中。我该怎么做才能让这段代码运行得更快?

编辑:正好有 110 行

【问题讨论】:

  • 如果使用单个 SQL 语句,您可能会获得大约 3 倍的速度
  • 冠军表中有多少行。我假设 id 是主键?想不通为什么这么慢,主键应该被索引..嗯..
  • +1 为亚历克斯·胡安·吉特迈尔。结合你的 SQL。不需要单独查询每一列。
  • 发布你的表结构。

标签: android sql performance sqlite


【解决方案1】:

不应该使用单条sql语句来代替单独的语句吗?

只需创建一个 ArrayList 即可将所有需要的值存储在 Activity 类中。

例如:ArrayList&lt;String&gt; myData;

现在在数据库助手类中创建一个函数,如下所示:

 // TO get All Data of datanase which you want
public ArrayList<String> getAllData() {          
    ArrayList<String> subTitleList = null;         
    Cursor cursor = null;          
    try {              
    String queryString = "SELECT * FROM champions";              
        cursor =  db.rawQuery(queryString, null);              
        if (cursor != null && cursor.moveToFirst()) {                 
            subTitleList = new ArrayList<String>();                 
            do {                     
                String nextUser = new String(cursor.getString(cursor.getColumnIndex("name")));                     
                String nextUser = new String(cursor.getString(cursor.getColumnIndex("title")));                     
                String nextUser = new String(cursor.getString(cursor.getColumnIndex("thumb")));                     

                subTitleList.add(nextUser);                 
            } 
            while (cursor.moveToNext());   
            System.out.println("it comes in SubTitleList");
        }         
    } 
    catch (Exception e) {             
        e.printStackTrace();             
        subTitleList = null;         
    } 
    finally {             
        if (cursor != null && !cursor.isClosed()) {                 
            cursor.deactivate();                 
            cursor.close();                 
            cursor = null;             
        }             
        if(db != null){                 
            db.close();             
        }         
    }
    //System.out.println("SubTitleList is: "+subTitleList);
    return subTitleList;   
}

现在在您的活动类中,您可以调用此函数并从 myData ArrayList 中获取所有需要的数据。

myData = db.getAllData(); // i think there is no need of any ID if you are fetching all the data.

希望你明白我的意思。

【讨论】:

  • 感谢您的接受。只需稍作修正即可。如果要获取整个表的数据并且有更多字段,则可以将其存储在具有 geter、seter 方法的 Any Object Class 中来存储和获取数据。并制作该类的 ArrayList 而不是字符串。希望你明白我的意思。请随意使用 cmets。
  • 当你声明 String nextuser 3 次时的部分真的让我很烦。我应该将 subTitleList 更改为 2d 数组列表还是我没有得到什么?我也不能投票,因为我没有 15 个代表; /
  • @Qbee:这只是结构代码。你可以在里面做你想做的事。那么它是否解决了?如果没有,请评论您遇到了哪个问题?
  • @Qbee:欢迎朋友。评论我以获得任何其他帮助。
  • @Qbee,如果你试过了,现在速度有多快?
【解决方案2】:

您绝对可以在AsyncTask 中运行它们。您所要做的就是将数据传递给任务的参数,或者如果您无法弄清楚,只需让您的任务有一个构造函数来获取参数并像这样调用它:

MyAsync ma = new MyAsync(stuff, stuff, stuff);
ma.execute();

在任务的onPostExecute() 中,您可以从后台运行的查询中获取数据,然后您可以更新 UI。

其他人也说得对。如果您可以将最好的查询与这样的表结合起来,那么它并不会真正给您带来很大的性能提升,至少我不认为。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多