Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add paragraph about meta_queries and NOT_EXISTS #356

Closed
wants to merge 3 commits into from

Conversation

moraleida
Copy link

Description of the Change

Fixes #355

Adds a paragraph discouraging the use of "EXISTS" or "NOT_EXISTS" as comparison methods to query for post meta. Please see full description as examples in the Issue.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Applicable Issues

#355

Changelog Entry

@moraleida moraleida changed the title add paragraph about meta_queries and EXISTS/NOT_EXISTS add paragraph about meta_queries and NOT_EXISTS Feb 24, 2021
@jeffpaul jeffpaul requested a review from bengreeley March 3, 2021 20:03
@jeffpaul
Copy link
Member

jeffpaul commented Mar 3, 2021

@bengreeley would appreciate review from you on this, thanks!

[Post meta](https://wordpress.org/support/article/custom-fields/) lets us store unique information about specific posts. As such the way post meta is stored does not facilitate efficient post lookups. Generally, looking up posts by post meta should be avoided (sometimes it can't). If you have to use one, make sure that it's not the main query and that it's cached.
* [Post meta](https://wordpress.org/support/article/custom-fields/) lets us store unique information about specific posts. As such the way post meta is stored does not facilitate efficient post lookups. Generally, looking up posts by post meta should be avoided (sometimes it can't). If you have to use one, make sure that it's not the main query and that it's cached.

* Always set default values for post_meta keys, especially if they are used to filter other queries. When writing queries filtering by post meta, never use the compare type ```NOT EXISTS```.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moraleida Could you put an example of when we'd set default values for post_meta keys? Is that when using update_post_meta or add_post_meta? I feel like we need a bit more information on why default values (instead of empty strings, I assume) are a best practice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend we lead with 'Never use compare type NOT EXISTS' as the leading part of this (or on its own line) since it tees up the majority of the rest of the section


Queries that need to check if a given meta_key does not exist need to use `LEFT JOIN`, gathering a lot more data. In practice, this type of query destroys any other performance improvements because it requires the MySQL engine to run through all the rows in the selected set to make sure it catches any rows that meet the criteria.

If all posts have a specific meta key, or if you can test for the presence of a given key, the engine can use a simple `INNER JOIN` and only touch the rows that already meet the criteria, saving a lot of time and processing on large tables.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend rewording If all posts have a specific meta key, or if you can test for the presence of a given key to To search for the presence of a given key. The 'if all posts have a specific meta key' doesn't make a ton of sense to me, but I could be missing something.

Copy link
Contributor

@bengreeley bengreeley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moraleida Thank you for putting this together! I made a few suggestions in wording that will help clarify the intent and really focus on the performance recommendations. Let me know if you have any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add paragraph on the performance of meta_queries using EXISTS/NOT_EXISTS
5 participants