I assume you are looking for this:
DELETE FROM ranking WHERE id NOT IN ( SELECT id FROM ranking ORDER BY score DESC LIMIT 100);
Here's a SQL Fiddle illustrating the concept.
This is pretty efficient (actually, it's pretty typical), since the subquery is only executed once. Actually, it depends more on whether the βscoreβ is covered by the index - or not:
(without index):
EXPLAIN QUERY PLAN DELETE FROM ranking WHERE id NOT IN ( SELECT id FROM ranking AS ranking_subquery ORDER BY score DESC LIMIT 2);
(after CREATE INDEX ts ON ranking(score); )
selectid order from detail 0 0 0 SCAN TABLE ranking (~500000 rows) 0 0 0 EXECUTE LIST SUBQUERY 0 0 0 0 SCAN TABLE ranking AS ranking_subquery USING INDEX ts (~1000000 rows)
source share