简体   繁体   English

从SQLite DB创建ArrayList非常慢

[英]Creating an ArrayList from SQLite DB is very slow

Alright, so I'm trying to create a little and lightweight ArrayList from my SQLite DB. 好吧,所以我试图从我的SQLite数据库中创建一个小巧轻便的ArrayList

Here's the code: 这是代码:

public ArrayList<Album> getAlbums()
{
    Cursor cursor = null;
    Cursor cursor2 = null;
    try{
        Log.d(Global.TAG, "STARTING");
        ArrayList<Album> albums = new ArrayList<Album>();
        Album album = new Album();
        db = getOpenDatabase(DBADS);
        String albumQuery = "SELECT * FROM " +albumsTableName + " ORDER BY " +albumIsMain + " DESC"; 
        cursor = db.rawQuery(albumQuery, null);
        if (cursor.moveToFirst()){
            do {
                Log.d(Global.TAG, "IN LOOP");
                album = new Album();
                album.id = cursor.getInt(0);
                album.albumName = cursor.getString(1);
                album.passcode = cursor.getString(2);
                album.isMainAlbum = cursor.getInt(3) == 1 ? true : false;
                albumQuery = "SELECT COUNT(*) FROM " + picturesTableName+" WHERE "+ pictureAlbumId +"="+album.id;
                cursor2 = db.rawQuery(albumQuery, null);
                cursor2.moveToFirst();
                album.photosCount = cursor2.getInt(0);
                cursor2.close();

                albums.add(album);
            }
            while (cursor.moveToNext());
        }
        Log.d(Global.TAG, "RETURNING");
        return albums;
    }
    catch(Exception ex)
    {
        Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
    }
    finally
    {
        if(cursor != null){
            cursor.close();
        }
        if(cursor2 != null){
            cursor2.close();
        }
        if(cursor3 != null){
            cursor3.close();
        }
    }
    return null;

}

The following code executes in like 8 seconds for only 6 items total! 下面的代码仅在8秒内执行,总共只有6个项目! This is just exteremly slow. 这只是极慢。
There are not like 1000 rows here, but only 6. After some debugging, I see that if I remove the other second inner cursor, it becomes very fast. 这里没有1000行,只有6行。经过一些调试,我发现如果删除另一个内部游标,它将变得非常快。

What could be the reason for that? 可能是什么原因呢?

Thanks alot. 非常感谢。

Save the result of first select statement in a list. 将first select语句的结果保存在列表中。 Then pass them simultaneously to second select call. 然后将它们同时传递给第二个选择呼叫。 Rightnow you are making a lot of db calls 现在,您正在进行很多数据库调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM