将以下代码复制到记事本中然后保存为 db_best.php 文件,修改好相应数据库信息后上传至网站然后使用浏览器直接访问该脚本即可一键优化数据库,不过由于一次性执行多个 sql 查询,所以该脚本访问时响应速度可能会比较长,优化成功后会显示 Done 。 (PS:优化前记得备份数据库以防万一。)
相关文章:WordPress 数据库结构说明
PHP
//ps 记得修改数据库前缀~
//使用时将脚本上传至网站任意目录后并且通过浏览器访问即可一键优化 wordprsss 数据库。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?php
//WordPress 数据库优化脚本 by weixiaoduo.com
$hostname_blog = "localhost";//设定数据库主机,同 wp-config.php
$database_blog = "WordPress";//设定数据库名,同 wp-config.php
$username_blog = "root";//设定数据库用户名,同 wp-config.php
$password_blog = "";//设定数据库密码,同 wp-config.php
$blog = mysql_pconnect($hostname_blog, $username_blog, $password_blog) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_blog, $blog);
mysql_query('DELETE FROM wp_posts WHERE post_type = "revision"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = "_edit_lock"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = "_edit_last"');
mysql_query('DELETE FROM wp_commentmeta WHERE meta_key LIKE "%trash%"');
mysql_query('DELETE FROM wp_comments WHERE comment_approved = "trash"');
mysql_query('DELETE FROM wp_options WHERE option_name REGEXP "_transient_"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = 『_wp_attached_file』');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = 『_wp_attachment_metadata』');
mysql_query("delete from wp_posts
where (post_status='auto-draft' or post_status='inherit')
and post_type='post'");
$tablelist = mysql_query("SHOW TABLES");
while($checklist = mysql_fetch_array($tablelist)) {
$optimization=mysql_query("OPTIMIZE TABLE `$checklist[0]`");
}echo 'Done';
//ps 记得修改数据库前缀~
//使用时将脚本上传至网站任意目录后并且通过浏览器访问即可一键优化 wordprsss 数据库。 |