Orphaned postmeta:
SELECT COUNT(pm.meta_id) as row_count FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;
DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;
Orphaned comment meta
SELECT COUNT(*) as row_count FROM wp_commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM wp_comments);
DELETE FROM wp_commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM wp_comments);
Session Data:
SELECT * FROM `wp_options` WHERE `option_name` LIKE '_wp_session_%'
DELETE FROM `wp_options` WHERE `option_name` LIKE '_wp_session_%'
Delete post revisions:
DELETE FROM wp_posts WHERE post_type = "revision";
Delete transients:
DELETE FROM `wp_options` WHERE `option_name` LIKE ('%_transient_%')
Delete unused tags:
DELETE FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE count = 0 );
DELETE FROM wp_term_taxonomy WHERE term_id not IN (SELECT term_id FROM wp_terms);
DELETE FROM wp_term_relationships WHERE term_taxonomy_id not IN (SELECT term_taxonomy_id FROM wp_term_taxonomy);