Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SQLite Select query speed
    primarykey
    data
    text
    <p>I would like help speeding up a fairly simple select query in my <code>SQLite</code> database. </p> <p>The query is searching a table of approx 6500 rows and currently returns in about 600ms. I don't know if that is acceptable or not, but I had hoped that it would be faster than that. I will be putting this into an <code>AsyncTask</code> eventually, but wanted to optimize as much as possible. In the code snippet below you can see what I have tried.<br> KEY_songPath is indexed.</p> <pre><code>public ArrayList&lt;Song&gt; getCurrentDirSongs(String currentDir) { long startTime = System.currentTimeMillis(); ArrayList&lt;Song&gt; songsList = new ArrayList&lt;Song&gt;(); String selectQuery = ""; int strLength = currentDir.length(); //selectQuery = "SELECT * FROM " + TABLE_SONGS + " WHERE (" + KEY_songPath + " &gt;= '" + currentDir + "')"; //selectQuery = "SELECT * FROM " + TABLE_SONGS + " WHERE (" + KEY_songPath + " GLOB '" + currentDir + "*')"; //selectQuery = "SELECT * FROM " + TABLE_SONGS + " WHERE (" + KEY_songPath + " LIKE '" + currentDir + "%')"; selectQuery = "SELECT * FROM " + TABLE_SONGS + " WHERE " + KEY_ID + " IN (SELECT " + KEY_ID +" FROM "+ TABLE_SONGS + " WHERE length(" + KEY_songPath + ")&gt;= "+ strLength +") AND " + "(" + KEY_songPath + " LIKE '" + currentDir + "%')"; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { Song song = new Song(); song.setSongTitle(cursor.getString(1)); song.setSongPath(cursor.getString(2)); song.setSongArtist(cursor.getString(3)); song.setSongAlbum(cursor.getString(4)); song.setTrackNumber(cursor.getString(5)); song.setID(Integer.parseInt(cursor.getString(6))); song.setAlbumID(Integer.parseInt(cursor.getString(7))); song.setArtistID(Integer.parseInt(cursor.getString(8))); song.setSongCheckedStatus(Boolean.parseBoolean(cursor.getString(9))); song.setSongPosition(Integer.parseInt(cursor.getString(10))); song.setSongDuration(Integer.parseInt(cursor.getString(11))); // Adding song to list songsList.add(song); } while (cursor.moveToNext()); } long estimatedTime = System.currentTimeMillis() - startTime; Log.d("AMCDatabase Database Handler: ", "SongsList.size(): "+ getSongsCount("SongsList") + " Time: " + estimatedTime + "ms"); // return contact list return songsList; </code></pre> <p>}</p> <p>This has returned with the following result: SongsList.size(): 6390 Time: 588ms</p> <p>Any help gratefully received! </p> <p>P.S This may all be a non-issue - is that time of a second acceptable to query and 6000 values to an <code>ArrayList</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload