SQLite: efficient way to drop lots of rows

前端 未结 3 2053
说谎
说谎 2020-12-19 16:28

SQlite, Android, true story. I have a table, which I use as a cache:

CREATE TABLE cache(key TEXT, ts TIMESTAMP, size INTEGER, data BLOB);
CREATE UNIQUE INDEX         


        
3条回答
  •  庸人自扰
    2020-12-19 16:46

    You have two options to improve the performance, especially the first one:

    1) Using Transaction like this:

    DbTransaction trans = conn.BeginTransaction(); // <-------------------
    try 
    {
       Any code to delete the items
    }
    catch
    {
        trans.Rollback(); // <-------------------
        throw; // <-------------------
    }
    

    2) Otherwise, supposing that the items are continuous, then

    • a) Get the ID of the first item;

    • b) Get the total number of items to be deleted

    • c) Using command like this:

      DELETE FROM blobs WHERE ID > fistId LIMIT count;

    Good luck.

提交回复
热议问题