Search code examples
androidperformancesqliteandroid-cursormemory-efficient

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


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.


Solution

  • 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?