Posted in
9011
4:20 am, January 18, 2022
function to delete older sqlite records when the limit is reached
I had an issue where a sqlite database was hitting over 100,000 records and this was causing quite a lot of lag and crashing on my server, the number of records was not really meant to be this high, so i can just keep a few thousand so it does not cause lag.
This is the function i added and run before or after the latest item is inserted to remove older items in the database.
PHP
// this will delete records over the specified number if run.
// it will keep the latest $records_to_keep number and delete older ones.
public function delete_over($records_to_keep = 1000) {
$db_table_name = $this->db_table_name;
$sql = "DELETE FROM $db_table_name WHERE ROWID IN (SELECT ROWID FROM $db_table_name ORDER BY ROWID DESC LIMIT -1 OFFSET $records_to_keep)";
$result = $this->db->query($sql);
}
// example run on keywords class
$keywords->delete_over($records_to_keep = 1000);
External Link for function to delete older sqlite records when the limit is reached
View Statistics
This Week
223
This Month
789
This Year
2669
Add Comment
Other Items in sqlite
Related Search Terms
Other Categories in Code
alpine js apps c css factorio font awesome images linux quick modals sqlite site bugs site updates slick slider sliders testing windows apps apache api apps asp bat bootstrap bootstrap templates charts cookies core css css filters css grid design elements docker domains emoji fancybox fonts foundation framework gimp git html icons ideas image formatting images javascript javascript functions jquery js linux mac misc modals mysql nginx node php php errors php function php functions php simple html dom pi400 python react regex sections simple_html_dom simplepie php site bugs site documentation slick slider sql sqlite ssh sublime svg svg css templates tools virtual box vscode vue webdev windows windows 11 windows commands wordpress