Last active
August 21, 2018 13:19
-
-
Save fedek6/fedafb842f4c1680e9902b84fe828f59 to your computer and use it in GitHub Desktop.
Log rotation in MySQL table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE FROM logs WHERE id IN (select id from (select id FROM log ORDER BY id DESC LIMIT 1000, 500) x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `log` ( | |
`id` INT(11) NOT NULL AUTO_INCREMENT, | |
`level` TINYINT(4) NOT NULL, | |
`content` TEXT NOT NULL, | |
`date` DATETIME NOT NULL, | |
PRIMARY KEY (`id`) | |
) | |
ENGINE=InnoDB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$limit = 1000; | |
$remove = 500; | |
$sth = Flight::db()->prepare( 'DELETE FROM log WHERE id IN (select id from (select id FROM log ORDER BY id DESC LIMIT :limit, :remove) x)' ); | |
$sth->bindParam( ':limit', $limit, PDO::PARAM_INT ); | |
$sth->bindParam( ':remove', $remove, PDO::PARAM_INT ); | |
$sth->execute(); | |
$count = $sth->rowCount(); | |
$sth->closeCursor(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment