Android SQLite Query - Getting latest 10 records

后端 未结 9 919
死守一世寂寞
死守一世寂寞 2020-12-05 04:56

I have a database saved in my Android application and want to retrieve the last 10 messages inserted into the DB.

When I use:

Select * from tblmessag         


        
9条回答
  •  感情败类
    2020-12-05 05:39

    on large databases, the ORDER BY DESC statement really might slow down the system, e.g. raspberry pi. A nice approach to avoid ORDER BY is the OFFSET command. And you even keep the stored order:

    SELECT * FROM mytable LIMIT 10 OFFSET (SELECT COUNT(*) FROM mytable)-10;
    

    see: http://www.sqlite.org/lang_select.html

    check out your performance with:

    .timer ON
    

提交回复
热议问题