On a very old site (7 yrs) with a lot of posts (30k) I've been trying to whittle down the database tables. The postmeta table is 1.5m rows, 750k of those rows are for a meta_key named '_thumbnail_id'. I know this sets the featured image for each post, but I'm confused as to why there are 750,000 of these set when there are only 30,000 posts.
Many post_id's appear to have 5-6 featured images set (http://cl.ly/image/0S3Z2M1V2c0M). That was generated by this
SELECT * FROM `wp_postmeta` WHERE `meta_key` = '_thumbnail_id' ORDER BY `wp_postmeta`.`post_id` ASC
They aren't duplicates (ie two rows with the same post_id and meta_value). I'm guessing the featured image was changed multiple times and the old values are not removed but am unsure.
Two questions really, is this normal behavior or something I need to track down and fix? Whether this is normal or not, if they're not being used, can they be removed?
Bonus question, what's the safe MySQL query to remove only the unused ones?