WordPress数据库 优化清理,数据库表用途说明,删除wp无用数据库信息代码

赛高部落

  1. wp_commentmeta: 用于保存评论的元信息,在将评论放入回收站等操作时会将数据放入此表,Akismet等插件也会生成此表的数据。此表不太重要。
  2. wp_comments: 用于保存评论信息的表。
  3. wp_links: 用于保存用户输入到Wordpress中的链接(通过Link Manager)的表。
  4. wp_options: 用于保存Wordpress相关设置、参数的表,里面包括了大量的重要信息。
  5. wp_postmeta: 用于保存文章的元信息(meta)的表。此表不太重要。
  6. wp_posts: 用于保存你所有的文章相关信息的表,非常的重要。一般它存储的数据是最多的。
  7. wp_terms: 文章和链接分类以及文章的tag分类可以在表里找到。
  8. wp_term_relationships: 日志与wp_terms中的类别与标签联合起来共同存储在wp_terms_relationships表中。类别相关链接也存储在wp_terms_relationships中。
  9. wp_term_taxonomy: 该表格对wp_terms表中的条目分类(类别、链接以及标签)进行说明。
  10. wp_usermeta : 用于保存用户元信息(meta)的表。
  11. wp_users:用于保存Wordpress使用者的相关信息的表。

 

  • 清除wordpress中的基本配置(wp_options表)

造成wp_options数据 多余的主要途径有两个,一是频繁的安装卸载插件。二是大量的RSS缓存。清理的方法,首先在根目录下wp_config.php文件中加入define(‘MAGPIE_CACHE_ON’, ‘0’);代码来阻止RSS缓存的生成。其次是使用代码手动清理。

 

删除RSS缓存数据:

DELETE FROM wp_options WHERE option_name LIKE (‘%_transient_%’);

DELETE FROM wp_options WHERE option_name REGEXP ‘_transient_’;

 

  • 清理wp_posts表

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 表

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’;

 

删除重复的 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

)

 

删除附件记录

DELETE FROM wp_postmeta WHERE meta_key = ‘_wp_attached_file’;

DELETE FROM wp_postmeta WHERE meta_key = ‘_wp_attachment_metadata’;

 

  • 清理 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

);

 

  • 清理wp_users和wp_usermeta两个表

删除最后登录时间为2022年的用户

DELETE FROM `wp_users` WHERE `wp_users`.`ID` = any(SELECT `user_id` from `wp_usermeta` WHERE (`wp_usermeta`.`meta_value` LIKE ‘%2023%’) AND(`wp_usermeta`.`meta_key` LIKE ‘last_login’))

 

选择月份删除的代码:

DELETE FROM `wp_users` WHERE `wp_users`.`ID` = any(SELECT `user_id` from `wp_usermeta` WHERE (`wp_usermeta`.`meta_value` LIKE ‘%2022%’) AND(`wp_usermeta`.`meta_key` LIKE ‘last_login’));

DELETE FROM `wp_users` WHERE `wp_users`.`ID` = any(SELECT `user_id` from `wp_usermeta` WHERE (`wp_usermeta`.`meta_value` LIKE ‘%2023-01%’) AND(`wp_usermeta`.`meta_key` LIKE ‘last_login’));

DELETE FROM `wp_users` WHERE `wp_users`.`ID` = any(SELECT `user_id` from `wp_usermeta` WHERE (`wp_usermeta`.`meta_value` LIKE ‘%2023-02%’) AND(`wp_usermeta`.`meta_key` LIKE ‘last_login’));

DELETE FROM `wp_users` WHERE `wp_users`.`ID` = any(SELECT `user_id` from `wp_usermeta` WHERE (`wp_usermeta`.`meta_value` LIKE ‘%2023-03%’) AND(`wp_usermeta`.`meta_key` LIKE ‘last_login’));

DELETE FROM `wp_users` WHERE `wp_users`.`ID` = any(SELECT `user_id` from `wp_usermeta` WHERE (`wp_usermeta`.`meta_value` LIKE ‘%2023-04%’) AND(`wp_usermeta`.`meta_key` LIKE ‘last_login’));

DELETE FROM `wp_users` WHERE `wp_users`.`ID` = any(SELECT `user_id` from `wp_usermeta` WHERE (`wp_usermeta`.`meta_value` LIKE ‘%2023-05%’) AND(`wp_usermeta`.`meta_key` LIKE ‘last_login’));

发表评论