开发者

sqlite filtering by sum

开发者 https://www.devze.com 2023-01-15 04:44 出处:网络
I have a database with 1000 reco开发者_运维问答rds containing a file name and a file size on each row.

I have a database with 1000 reco开发者_运维问答rds containing a file name and a file size on each row. If the SUM of all file sizes exceeds a specific limit, then: - I need to create a sql query to delete all the remaining rows, from oldest to newest

Any suggestions? Thanks


Supposing a table created thus:

CREATE TABLE Files (Id INTEGER PRIMARY KEY, FileName TEXT, CreationDate DATE, Size INTEGER);

To get the running sum, use the following query:

SELECT f1.id AS FileId, sum(f2.size) AS RunningSumSize
FROM file f1 INNER JOIN file f2
ON f1.createdDate<=f2.createdDate
GROUP BY FileId
ORDER BY RunningSumSize DESC;

To delete the file ID's above the threshold:

DELETE FROM File WHERE Id IN
 (SELECT FileId FROM 
  (SELECT f1.id AS FileId, sum(f2.size) AS RunningSumSize
  FROM file f1 INNER JOIN file f2
  ON f1.createdDate<=f2.createdDate
  GROUP by FileId
  ORDER by RunningSumSize DESC)
  WHERE RunningSumSize > :ThresholdSize:);

Note: The order by is optional.


Probably the easiest solution (that is still fast even with many rows) is to calculate the running total in the application:

select createdDate, size from files order by createdDate desc

Now read the result set, and in the loop use total += size. Once total is larger, delete everything older than the current createdDate:

delete from files where createdDate < ?

Some other databases (for example MySQL and H2) support efficient running totals, but not SQLite.


Do binary search. Check if sum of 500 newest files is less then limit, if so, if sum of 750 is below limit etc. Until you get the row from which you should start deleting. Then just DELETE WHERE file_date > date_of_your_last_row_below_limit.

0

精彩评论

暂无评论...
验证码 换一张
取 消