0

Ok, I hope I won't get downvoted for this being not a coding question, but I think this will benefit all who want to make their Wordpress projects more CPU efficient.

Basically on the other questions that I asked, I got a response that it's better to use Custom Taxonomy (same as category or tags), rather than using post meta data.

My question is, is it really better for server CPU and if yes, that means Custom Taxonomy is working completely different than Post meta data query?

I'm building a quite serious project and I want to make it as efficient as possible, so that I wouldn't need to redo all over again.

To clarify, is custom taxonomy more efficient than post meta data when it comes to querying the posts. Thanks.

2

Yes and no.

The post meta table is optimised for retrieving a posts keys/meta values when you already know the post ID. It is not optimised for searching and filtering.

The taxonomy tables are optimised for grouping and filtering, searching.

If you want to find all posts that have X, or Y, X and Y should be terms in a taxonomy.

If you know the data will be retrieved when you already know the post ID, use post meta. It is fast to retrieve meta belonging to a post. It is slow to do the reverse. The slowness also depends on your servers traffic as well as how much meta you've stored. As your site gets bigger and more popular, the cost rises quickly.

Other things to watch out for:

  • avoid not or exclusions, they're extremely slow/heavy/expensive on the database, always ask the database for what you want, don't ask it to remove/exclude/hide things. Filter or X Y Z, don't filter out A B C, it's expensive/slow! If necessary, add extra data that says the opposite, e.g. show_on_homepage instead of hide_on_homepage etc
  • avoid ordering by RAND, it is one of the worst things you can do for query performance
  • don't ask for more than 100 posts per page, you can always paginate or do multiple queries
  • don't store everything as post meta, or everything ass terms/taxonomy, the point is they are good for different things, choose which for each piece of data you need to store.
    • you can even store data in both, e.g. detailed data in post meta, and an approximation in a term for easy filtering. For example, storing $5.96 in meta, and a $5-$10 term will avoid expensive query math and provide a trivial UI for users that's fast while still storing the exact price
1
  • The TLDR: store data in post meta, but if you need to filter/search/group by that data store it as a taxonomy/term. Storing it as both is also an option, and so is storing an approximation as a term for performance. Don't store everything in post meta, and don't store everything in taxonomies, they're good at different things.
    – Tom J Nowell
    Sep 29 '21 at 9:59
1

Yes, an SQL query that is for a single taxonomy will generally be faster than an SQL query that is for a post meta value. If you just need to check whether the post meta key exists, then that is performant. However, if you need to query for its value, that is a slower query. This is because by default there is no index on the meta_value in the postmeta table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.