Get count with cursor.getCount() or to execute a rawQuery with a COUNT on a SQL clause?
Asked Answered
P

1

6

What would be better in terms of memory efficiency or would have the best overall performance on Android and SQLite, getting a record count with cursor.getCount() or to execute a rawQuery with a COUNT on a normal SQL clause (and use cursor.getInt(0) later to get the count returned)?

Note: I'm not using the results, I just want the count.

Patten answered 21/12, 2011 at 16:4 Comment(0)
B
6

If you are using the result of the query afterwards, then of course the best way would be to do cursor.getCount() This is faster than doing 2 queries one to get the count and one for the result

EDIT :

If you are not using the results, then a rawQuery is faster because you are gettting only one column from the database instead of many. Plus why would you waste resources to execute a query and not use its result?

Betthel answered 21/12, 2011 at 16:8 Comment(2)
Oh, no. I forgot to say that I'm not using the results, in this case I'm just interested on the count itself. I'll fix my question.Patten
Ok, thanks. The reason I just want the count is that I'm displaying the user's related records as a badge over a customized Tab on the App. Think about the badge on the Email Apps icons. So I'm getting count of favorites, or other records related to the current logged in user and I do not need the records, just the count.Patten

© 2022 - 2025 — McMap. All rights reserved.