After frequent record deletion from your MySQL database tables, your tables can acquire overhead. Overhead is empty space left inside the database table due to the deletions. A great way to speed up your MySQL database, not to mention keep it compact, is to use a simple PHP function to optimize your database tables:
从MySQL数据库表中频繁删除记录后,您的表可能会增加开销。 开销是由于删除而在数据库表内留下的空白空间。 加速MySQL数据库(更不用说使其紧凑)的一种好方法是使用一个简单PHP函数来优化数据库表:
/* OPTIMIZE ALL TABLES */
function optimize_database($DATABASE_LINK) {
$result = mysql_query('SHOW TABLES', $DATABASE_LINK) or die('Cannot get tables');
while($table = mysql_fetch_row($result)) {
mysql_query('OPTIMIZE TABLE '.$table[0], $DATABASE_LINK) or die('Cannot optimize '.$table[0]);
}
}
I use the $DATABASE_LINK variable to keep my connection throughout my PHP script. Obviously it's not required, so you can modify the above function if you don't keep that variable.
我使用$ DATABASE_LINK变量来保持整个PHP脚本的连接。 显然,这不是必需的,因此,如果不保留该变量,则可以修改上述函数。
翻译自: https://davidwalsh.name/php-mysql-database-optimization-function