WordPress数据库优化之垃圾清理系列

WordPress的文章、评论等很多数据都是存放在数据库的,每当在WordPress编辑文章的时候会生成一些文章修订版本信息、自动保存文章草稿等无用数据,还有更换主题,删除插件也会将数据留在数据库中,在卸载后无法被清理。会造成WordPress数据库中增加很多冗余的数据信息,占用比较大的数据库缓存。而随着WordPress网站使用的时间越久,网站数据库中堆积的冗余数据信息越来越多,数据查询越来越慢,从而导致WordPress有大量无用的垃圾数据使数据库负担沉重,数据库运行起来越来越慢,并且也会造成网站访问速度缓慢,网页打开的速度变慢,丧失用户的良好体验度。所以,需要定期清理和优化WordPress数据库中的冗余数据,从而保证WordPress数据库运行的效率和网站的访问速度。

提示:清理优化之前请先备份数据库,以防因失误带来的意外情况!只有做好备份工作才可以有备无患。

PS:主要涉及到的几张表:wp_options,wp_posts,wp_postmeta,wp_commentmeta

清理 wp_options 表的处理方法

-- wordpress数据库手动清除Transients(下面两个SQL语句,任选一条执行)
DELETE FROM wp_options WHERE option_name LIKE ('%\_transient\_%');
DELETE FROM wp_options WHERE option_name REGEXP '_transient_';

清理 wp_posts 表(包括删除修订版本、自动草稿的文章数据)

-- 删除所有非发布状态帖子信息(只保留已经发布的状态的文章、菜单、页面)
DELETE FROM wp_posts WHERE NOT(post_status = 'publish' AND post_type IN('post','nav_menu_item','page'));
-- 删除全部文章修订版本及所对应的关联数据
DELETE a,b,c FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision';
-- 删除自动保存草稿以及修订版本的文章
delete from wp_posts where (post_status='auto-draft' or post_status='inherit') and post_type='post';

清理 wp_postmeta 表

--规矩删除(删除文章中不存在文章的元信息)
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT post_id FROM wp_posts);
-- 安全删除(删除_edit_lock和_edit_last条目是安全的)
DELETE FROM wp_postmeta WHERE meta_key = '_edit_lock';
DELETE FROM wp_postmeta WHERE meta_key = '_edit_last';
-- 风险删除(除了这两条还执行了一些其他语句由于有些风险:自己酌情考虑)
DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';
DELETE FROM wp_postmeta WHERE meta_key = '_revision-control';
DELETE FROM wp_postmeta WHERE meta_value = 'unknown';
--特殊插件删除(postnav插件会记录每个文章的访问数,如果不需要,可以删除)
DELETE FROM wp_postmeta WHERE meta_key = 'views';
-- 删除孤立的文章元信息(当文章的记录删除后,还有日志扩展表postmeta的数据还没有删除,也要人工清理下)
DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL
-- 删除重复的 meta key 和 value 记录,仅保留最新的一个
DELETE FROM wp_postmeta WHERE meta_id IN (
select * from (select meta_id FROM wp_postmeta pm WHERE
meta_id NOT IN (SELECT max(meta_id) FROM wp_postmeta pm2 where pm2.post_id=pm.post_id and pm2.meta_key=pm.meta_key)
) as g1
)

在WordPress的后台上传图片或者附件后会在wp_postmeta中生成_wp_attached_file和_wp_attachment_metadata两个项,wp_posts也会记录附件的信息。如果使用FTP工具上传文件,表中就不会有这些信息。

-- 特殊操作删除
DELETE FROM wp_postmeta WHERE meta_key = '_wp_attached_file';
DELETE FROM wp_postmeta WHERE meta_key = '_wp_attachment_metadata';

以下的这些可以考虑清,或者不清,其实很多人或许没这个项目,因为这些大都由插件产生的。

-- 可选项目
DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';
DELETE FROM wp_postmeta WHERE meta_key = 'jd_tweet_this';
DELETE FROM wp_postmeta WHERE meta_key = 'wp_jd_clig';
DELETE FROM wp_postmeta WHERE meta_key = 'wp_jd_target';
DELETE FROM wp_postmeta WHERE meta_key = 'nofollow4post';
DELETE FROM wp_postmeta WHERE meta_key = 'ratings_score';
DELETE FROM wp_postmeta WHERE meta_key = 'ratings_users';
DELETE FROM wp_postmeta WHERE meta_key = 'ratings_average';
DELETE FROM wp_postmeta WHERE meta_key = 'wp_noextrenallinks_mask_links';
DELETE FROM wp_postmeta WHERE meta_key = '_wp_page_template';
DELETE FROM wp_postmeta WHERE meta_key = '_sexybookmarks_permaHash';
DELETE FROM wp_postmeta WHERE meta_key = '_sexybookmarks_shortUrl';

以上几条语句执行完毕能够删除掉95%以上的数据,算的上是极限优化了,最后考虑到这个数据表并不是很重要,有洁净癖的人可以尝试清空这个表,当然测试清空表会让一些原本的数据丢失。

-- 洁癖删除
TRUNCATE TABLE wp_postmeta;

清理 wp_commentmeta 表

--删除孤立的评论元信息(评论删除后残留在wp_commentmeta表中的信息)
DELETE FROM wp_commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM wp_comments);
DELETE FROM wp_commentmeta WHERE meta_key REGEXP 'akismet';
-- 删除记录评论被删除的时间,这些信息用处不是很大
DELETE FROM wp_commentmeta WHERE meta_key LIKE '%trash%';
-- 直接全部删除wp_commentmeta数据表内容,影响不会太大,这里面不会涉及重要的数据
TRUNCATE TABLE wp_commentmeta;

清理其他数据表

-- 删除所有垃圾留言(包括待审、垃圾评论、回收站评论)
DELETE FROM wp_comments WHERE comment_approved != '1';
-- 删除待审评论
DELETE FROM wp_comments WHERE comment_approved = '0';
-- 删除垃圾评论
DELETE FROM wp_comments WHERE comment_approved = 'spam';
-- 删除回收站评论
DELETE FROM wp_comments WHERE comment_approved = 'trash';
--孤立的关系信息(文章、评论等删除后残留在wp_term_relationships表中的信息)
DELETE FROM wp_term_relationships WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM wp_posts);
-- 删除没有的标签(有些文章删除了,但标签还在,WordPress不会自动删除的)
DELETE a,b,c FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id
WHERE (
c.taxonomy = 'post_tag' AND
c.count = 0
);

优化数据库表

OPTIMIZE TABLE wp_commentmeta;
OPTIMIZE TABLE wp_comments;
OPTIMIZE TABLE wp_links;
OPTIMIZE TABLE wp_options;
OPTIMIZE TABLE wp_postmeta;
OPTIMIZE TABLE wp_posts;
OPTIMIZE TABLE wp_terms;
OPTIMIZE TABLE wp_term_relationships;
OPTIMIZE TABLE wp_term_taxonomy;
OPTIMIZE TABLE wp_usermeta;
OPTIMIZE TABLE wp_users;

汇总以上数据库的执行语句到PHP文件中一键优化清理WordPress数据库

将上面所有清理数据库的语句汇总到一个php文件(SEOClear.php),打开PHP文件后就可以直接优化清理数据了,并且保证是安全的。 粘贴以下代码执行PHP文件即可:

<?php
//wordpress数据库优化清理脚本
$hostname_blog = "localhost";//设定数据库主机,同wp-config.php
$database_blog = "s9h.cn";//设定数据库名,同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 a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type = "revision"');
mysql_query('DELETE FROM wp_posts WHERE NOT(post_status = "publish" AND post_type IN("post","nav_menu_item","page")');
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 != "1"');
mysql_query('DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL');
mysql_query('DELETE FROM wp_commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM wp_comments)');
mysql_query('DELETE FROM wp_term_relationships WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM wp_posts)');
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数据库。
?>

执行后将会批量删除WordPress产生的冗余文件,修改好相应数据库信息后上传至网站然后使用浏览器直接访问该脚本文件(SEOClear.php)即可优化清理数据库,不过由于一次性执行多个sql查询,所以该脚本访问时响应速度可能会比较长,优化成功后会显示Done。

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容