简体   繁体   中英

Best way to efficiently delete SQL rows in Android

I have a database table in my app in which I would only like to store the top ten highest numeric values. The columns are id(unique), name, score.

What is the best method for this? should I manually pull all records out and delete by id? or can I use a SQL Statement?

Thanks,

Ben

Then what you could do is put a Trigger on you insert for the table. Basically the trigger would check to see if there were now more than 10 rows in the table and delete the smallest row(s). The trigger would look like this:

CREATE TRIGGER clamp_trigger AFTER INSERT ON table_name
BEGIN
  DELETE FROM table_name
  WHERE column_name NOT IN (
  SELECT column_name 
  FROM table_name 
  ORDER BY column_name DESC
  LIMIT 10);
END;

I'm not 100% sure about SQLite, but in later versions of MySql, something like this works

DELETE FROM table_name
WHERE column_name NOT IN (
SELECT column_name 
FROM table_name 
ORDER BY column_name DESC
LIMIT 10);

Basically, select everything that isn't in the top 10 and delete it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM