Running a WordPress site for a long time probably means you’ve tried out lots of different plugins handling your data. This usually means you have a lot orphans in your database. So very sad.
Of course you want an orphan free database!
But you can easily clean up your database by running a few SQL queries.
First you should probably look if you have any orphan wp_postmeta rows in your database:
SELECT *
FROM wp_postmeta AS pm
LEFT JOIN wp_posts AS p ON p.ID = pm.post_id
WHERE p.ID IS NULL
Doing a LEFT JOIN means that SQL will look for posts, but still return a row if no parent post was found. And we only want to get post meta rows with no post parent; therefor we check where posts are NULL.
To be sure you won’t delete all your data, I strongly recommend to always backup your database and compare the row count from the above query with the total count of rows in your wp_postmeta table.
When you are sure, you can delete the rows with following query:
DELETE pm
FROM wp_postmeta AS pm
LEFT JOIN wp_posts AS p ON p.ID = pm.post_id
WHERE p.ID IS NULL
Happy orphan hunting!