[Solved] Does sqlite support reading certain number of rows for each query?

EverSQL Database Performance Knowledge Base

Does sqlite support reading certain number of rows for each query?

My Android application has an activity to present data from SQLite database. The db table might contain huge number of rows. For performance reasons, I want to load 20 rows from db at a time, and when user scrolls down listview to the end, read next 20 rows.

So I want to use SQL statement like this:

select * from mytable where id > N and count = 20;

I just wonder if SQLite supports this kind of "count=20" feature to read at maximum 20 rows for the query. If it is supported, what is exact syntax?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `mytable` ADD INDEX `mytable_idx_count_id` (`count`,`id`);
The optimized query:
SELECT
        * 
    FROM
        mytable 
    WHERE
        mytable.id > mytable.N 
        AND mytable.count = 20

Related Articles



* original question posted on StackOverflow here.