Created
May 8, 2012 21:30
-
-
Save kjoconnor/2639495 to your computer and use it in GitHub Desktop.
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 | |
// Clear django_sessions table | |
date_default_timezone_set("UTC"); | |
$link = mysql_connect("localhost", "username", "password") | |
or die("Couldn't connect to MySQL server.\n"); | |
mysql_select_db("database") | |
or die("Couldn't select DB.\n"); | |
$affected_rows = -1; | |
$rows = 100; | |
$multi = 1.1; | |
$limit = 1; | |
while ($affected_rows !== 0) { | |
$start_time = microtime(true); | |
mysql_query("DELETE FROM django_session WHERE expire_date < NOW() LIMIT " . $rows) | |
or die("Failed to delete! " . mysql_error()); | |
$end_time = microtime(true); | |
$run_time = abs($end_time - $start_time); | |
$affected_rows = mysql_affected_rows($link); | |
print "[" . date("m-d-Y H:i:s") . "] - Deleted " . $affected_rows . " rows in " . $run_time . "s\n"; | |
if($run_time < $limit) { | |
$rows = ceil($rows * $multi); | |
print "[" . date("m-d-Y H:i:s") . "] - Increasing row count to " . $rows . "\n"; | |
} else { | |
$rows = floor($rows / $multi); | |
print "[" . date("m-d-Y H:i:s") . "] - Decreasing row count to " . $rows . "\n"; | |
} | |
sleep(.1); | |
} | |
print "Completed!\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment