Audit and slim down wp_postmeta on large WordPress and WooCommerce sites
wp_postmeta is usually the biggest table of a WordPress site, and on a WooCommerce store with years of products and orders it can hold millions of rows. Every value is a LONGTEXT, the table has separate indexes on post_id and meta_key but none on the values, and queries that filter or sort by meta values get slower as it grows. Before tuning queries, it pays to see what is in the table.
Problem: A store with thousands of products has a wp_postmeta table of millions of rows, and pages with meta queries (filters, sorting by price) take seconds.
Solution: Measure which meta keys take the space, delete orphaned and leftover rows, make sure the code loads meta in batches, and move data that is queried by value into a table with proper columns.
The examples use the standard wp_ prefix. Make a backup before any DELETE.
1. What is in the table
-- Rows and size per meta key
SELECT meta_key,
COUNT(*) AS rows_count,
ROUND( SUM( LENGTH( meta_value ) ) / 1024 / 1024, 1 ) AS value_mb
FROM wp_postmeta
GROUP BY meta_key
ORDER BY rows_count DESC
LIMIT 30;
-- Rows per post type
SELECT p.post_type, COUNT(*) AS meta_rows
FROM wp_postmeta pm
JOIN wp_posts p ON p.ID = pm.post_id
GROUP BY p.post_type
ORDER BY meta_rows DESC;
The result usually shows a few surprises: keys of plugins removed long ago, page builder data on revisions, _edit_lock on every post, or, on a store without HPOS, orders that own most of the table.
2. Remove what nobody uses
-- Meta of posts that no longer exist DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts p ON p.ID = pm.post_id WHERE p.ID IS NULL; -- Keys of a plugin that has been removed (check the key first!) DELETE FROM wp_postmeta WHERE meta_key LIKE '\_oldplugin\_%';
Revisions keep their own meta too, so limiting revisions (define( 'WP_POST_REVISIONS', 10 );) and deleting old ones removes a lot. After big deletions, run ANALYZE TABLE wp_postmeta; and, if it pays, OPTIMIZE TABLE, see optimizing the database tables.
3. Load meta in batches
Most “slow meta” in PHP is not one slow query but hundreds of small ones: a loop over post IDs that calls get_post_meta() for each. A normal WP_Query loads the meta of all found posts at once; for a list of IDs, prime the cache yourself:
<?php
$product_ids = wc_get_products( array( 'limit' => 50, 'return' => 'ids' ) );
update_meta_cache( 'post', $product_ids ); // one query for the meta of all 50
foreach ( $product_ids as $id ) {
$price = get_post_meta( $id, '_price', true ); // from the cache
}
The full pattern, with terms and posts, is in avoiding N+1 queries.
4. Data queried by value: a real table
Meta works well for “give me the values of this post”. It works badly for “give me the posts where the value is between X and Y, sorted by Z”, because meta_value is text without an index. WooCommerce itself solved this for products with the wc_product_meta_lookup table (price, stock, rating as typed, indexed columns) and for orders with HPOS. For your own data that is filtered and sorted, do the same:
CREATE TABLE wp_product_stats ( post_id bigint(20) unsigned NOT NULL, views int unsigned NOT NULL DEFAULT 0, rating decimal(3,2) NOT NULL DEFAULT 0, PRIMARY KEY (post_id), KEY rating (rating) );
Then “the 20 best rated products” is one indexed query instead of a meta query over millions of rows. How to create and upgrade such a table from a plugin is described in creating a custom table with dbDelta.
NOTE: On a WooCommerce store, check two things before anything else: whether orders are already on HPOS (WooCommerce → Settings → Advanced → Features), which moves them out of wp_postmeta entirely, and whether the product lookup table is up to date (WooCommerce → Status → Tools → Regenerate the product lookup tables), because the shop filters use it instead of meta.