Title: [Plugin: WP-Optimize] wp_term_relationships
Last modified: August 19, 2016

---

# [Plugin: WP-Optimize] wp_term_relationships

 *  [nm2net](https://wordpress.org/support/users/nm2net/)
 * (@nm2net)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/)
 * I cleaned my database with the plugin, but this table “wp_term_relationships”
   still has the same amount of records (very large).
 * Any ideas how to clean it? I am pretty sure it contains a lot of unused info..
 * [http://wordpress.org/extend/plugins/wp-optimize/](http://wordpress.org/extend/plugins/wp-optimize/)

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Thread Starter [nm2net](https://wordpress.org/support/users/nm2net/)
 * (@nm2net)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1962794)
 * btw, I dont know of this is actual for the current version of WP, but you should
   check it out : [http://www.ambrosite.com/blog/clean-up-wordpress-revisions-using-a-mysql-multi-table-delete](http://www.ambrosite.com/blog/clean-up-wordpress-revisions-using-a-mysql-multi-table-delete)
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1962899)
 * I haven’t tested it recently but if you’re feeling brave you can try this query.
   It cleans wp_term_relationships by removing all “orphaned” records — that is,
   records that don’t have a corresponding object ID in the post or link tables.
   As with all delete queries, I recommend backing up your database before running
   it, just in case.
 *     ```
       DELETE tr FROM wp_term_relationships tr
       INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
       WHERE tt.taxonomy != 'link_category'
       AND tr.object_id NOT IN (SELECT ID FROM wp_posts);
       ```
   
 *  Plugin Contributor [Ruhani Rabin](https://wordpress.org/support/users/ruhanirabin/)
 * (@ruhanirabin)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1962922)
 * wp_term_relationships is a complicated table.. it might effect some other part
   of the db that is why I didn’t plan to put it in.. you can try what [@ambrosite](https://wordpress.org/support/users/ambrosite/)
   said but do backup and let me know.. how that went..
 *  Thread Starter [nm2net](https://wordpress.org/support/users/nm2net/)
 * (@nm2net)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1962988)
 * They just didn’t made the database design well. InnoDB tables (except the most
   important one! – posts) and not a single relation.. however.
 * Thanks for your suggestion ambrosite. I ran the query with a SELECT just to check
   what it covers, and I see there are some records
    WHERE tt.taxonomy **=** ‘category’.
   Would that be a problem, and should I include it into the where clause? /I am
   not very familiar with the WP details/
 *     ```
       WHERE tt.taxonomy != 'link_category'
       AND tt.taxonomy != 'category'
       AND tr.object_id NOT IN (SELECT ID FROM wp_posts);
       ```
   
 *  Thread Starter [nm2net](https://wordpress.org/support/users/nm2net/)
 * (@nm2net)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1962989)
 * Anyways, it worked fine for me. Thanks ambrosite 😉
 * Additionally I used
    `DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT ID
   FROM wp_posts);` to clear the wp_postmeta table as well
 *  [pete_398](https://wordpress.org/support/users/pete_398/)
 * (@pete_398)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1963021)
 * Seems the author of this plugin has no website up now. Does that mean there is
   no support ?
 *  [graffics](https://wordpress.org/support/users/graffics/)
 * (@graffics)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1963031)
 * > Anyways, it worked fine for me. Thanks ambrosite 😉
   > Additionally I used
   >  DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT ID
   > FROM wp_posts); to clear the wp_postmeta table as well
 * How many records did it clean out for you? I also have a large wp_term_relationships
   but only five we thrown out after that query
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1963032)
 * How many records were you expecting would be deleted? There is a many-to-many
   relationship between posts and taxonomy terms. If you have a lot of posts, you
   will have a lot of term relationships, especially if you have posts assigned 
   to multiple categories. In general, you can expect your wp_term_relationships
   table to have at least as many records as your wp_posts table. In most cases,
   it will have more.
 *  [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * (@ambrosite)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1963033)
 * And don’t forget that post tags are taxonomy terms as well. If you tend to put
   a lot of tags on your posts, you could easily have a wp_term_relationships table
   with 5-10 times as many records as wp_posts.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘[Plugin: WP-Optimize] wp_term_relationships’ is closed to new replies.

 * ![](https://ps.w.org/wp-optimize/assets/icon-256x256.png?rev=1552899)
 * [WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance](https://wordpress.org/plugins/wp-optimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-optimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-optimize/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-optimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-optimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-optimize/reviews/)

## Tags

 * [wp_term_relationships](https://wordpress.org/support/topic-tag/wp_term_relationships/)

 * 9 replies
 * 5 participants
 * Last reply from: [ambrosite](https://wordpress.org/support/users/ambrosite/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/plugin-wp-optimize-wp_term_relationships/#post-1963033)
 * Status: not resolved