Cursor.moveToFirst() is taking up a lot of time

by Sublimity Mobile Software » Tue, 19 May 2009 01:32:24 GMT


Sponsored Links
 Hi,

Currently i'm working on a database system for some applications. I
need to do a lot of queries to load data from the database into the
application. After being amazed how much time it took to do these
queries on the database i found out that much time was consumed by
cursor.MoveToFirst(). This functions costs currently ~25% of all my
database action.

Does anyone know why this takes would take so long or does anyone know
a better solution to get to the first position of the cursor?


My code:

Cursor c = db.query(var1, var2, var3 .....);
c.CustomMoveToFirst();

int count = c.getCount();
for(int i = 0; i<count; i++){
        doSomethingWithCursor();
        c.moveToNext();
}


The same problem occurs when i place everything in a while loop and
use only the MoveToNext() function. So i think that the problem must
be in positioning the cursor to the first position.

My code:
Cursor c = db.query(var1, var2, var3 .....);
c.CustomMoveToFirst();

while(c.moveToNext()){
         doSomethingWithCursor();
}

Thanks,
Sublimity Mobile Software

--~--~---------~--~----~------------~-------~--~----~



Cursor.moveToFirst() is taking up a lot of time

by Sublimity Mobile Software » Wed, 20 May 2009 17:17:58 GMT


 Thank you for your reply.

You where right. The getCount() method takes indeed a lot of time. So
i filled another window by using fillWindow() like this:

Cursor c = db.query(var1, var2, var3 .....);
SQLiteCursor liteCursor = (SQLiteCursor) c;
CursorWindow cw = new CursorWindow(true);
liteCursor.fillWindow(0, cw);

And then looping the CursorWindow to get all rows and columns.

I presumed that this would make a performance boost because i thought
that by doing this i will cut back on overhead. But this actually
takes about 40% more time then simply calling getCount() that
eventually loads the data into the cursor

I already optimized the database and the queries so there is nothing
to gain here.

But would you there be another way the get values from a database in a
faster way?




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Cursor.moveToFirst() is taking up a lot of time

by Streets Of Boston » Thu, 21 May 2009 01:59:10 GMT


 In your example, it seems you don't need the getCount() or moveToFirst
() at all.

The snippet below works well without using getCount or moveToFirst().
[code]
Cursor c = db.query(var1, var2, var3 .....);
while (c.moveToNext()) {
   // do something with cursor c
   ...
}
[/code]

On May 18, 10:47am, Sublimity Mobile Software


--~--~---------~--~----~------------~-------~--~----~



Cursor.moveToFirst() is taking up a lot of time

by Streets Of Boston » Thu, 21 May 2009 02:27:29 GMT


 really...? That's surprising.
I thought the moveToNext just fetches the rows lazily, fetches them
when necessary.





--~--~---------~--~----~------------~-------~--~----~



Cursor.moveToFirst() is taking up a lot of time

by Sublimity Mobile Software » Thu, 21 May 2009 05:50:28 GMT


 y current database layer is used to convert query results into bean
objects. I did a test and the results where:

Total time loading queries = 15273ms
Queries executed = 561
Executing All Queries = 3781 ( ~25% of the total time )
GetCount() method uses = 7291ms ( ~48% of the total time )
Loaded Objects = 1120

It uses 7291ms of the total 15273ms loading the data into the cursor
through using the getCount() method. I can not remove any columns from
the query as you suggested because i need those values in my bean
objects.

Any suggestions?


On 20 mei, 17:06, Marco Nelissen <marc...@android.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Help me: Build Android SDK for eclipse on win XP, and customize android source code!

Hi you,

I have 2 problem that I can't solute. Could you help me, please?

1. I built SDK Android on Ubuntu OS completely. But this SDK only for
eclipse on Ubuntu OS.


I want to create SDK for Window XP. How can I do that? Does it like
how to built SDK Android on Ubuntu OS?
2. I download Apps in Android source code such as: Email, Calendar,
Music, Setting,... but when I import them into eclipse, it have some
errors: project Calendar requires "import android.provider.Calendar;",
"import android.provider.Calendar.Events;",... So where can I download
"android.provider.calendar", "android.provider.calendar.Events"...
from? Or how can I set up environment?

Thank you very much!

-- 

2. Camera too slow to release!

I am developing a live wallpaper that shows what the camera sees. I am
stuck now where the camera is too slow to release. Say I am on the
launcher with the live wallpaper working and I go to launch the Camera
app, the camera app will be unable to connect to the camera. The trace
says it had "Fail to connect to camera service." The trace shows up
right in the middle of the release of the camera from the live
wallpaper.

Is there a way to add a pause or something to allow time for the
camera to release? Or better yet, speed up the camera release :)

Any help would be appreciated, thanks!

-- 

3. GLib - main event loop

4. Play streaming audio using Android media player.

5. Auto increament android:versionName in AndroidManifest.xml

6. How to modify the .apk file name during compilation

7. OnCompletionListener getting called for a Radio Application