Sometimes the wp_term_relationships table becomes bloated with many orphaned relationships. This happens particularly often if you’re using your site not as a blog but as some other type of content site where posts are deleted periodically. I recently worked on a site that had 18,000 term relationships for posts that no longer exist, and it was slowing the site down. In my case it was the way a real estate plugin from Placester IDX manages real estate listings, but you may find similar problems caused by other custom functionality.
Fortunately, MySQL can help you find these orphaned relationships with a pretty simple query:
SELECT * FROM wp_term_relationships LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID WHERE wp_posts.ID is NULL;
It can even automatically delete the orphaned entries for you! Make sure you back up your database before running the following query:
DELETE wp_term_relationships FROM wp_term_relationships LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID WHERE wp_posts.ID is NULL;
The number of rows deleted should match the number of rows returned in the SELECT
query above.
Once you’ve deleted the unnecessary items, make sure to optimize the database table.
OPTIMIZE wp_term_relationships;
The post Clean up a bloated wp_term_relationships table appeared first on Scott Nelle.