Open moraleida opened 3 years ago
@moraleida Hi 👋, you guys are doing awesome work with Engineering Practices! 🙌 Thank you for making these awesome docs.
I was recently checking this issue and would like to share my thoughts, not sure if this is a good idea for bigger picture but let me share:
NOT EXISTS
check: if the user makes a meta_key with appending _1
after the actual meta_key then it will be straight forward query to just check meta_key
= _publicize_pending_1
, so the end query will be as below:$query = new \WP_Query(
array(
'suppress_filters' => true,
'meta_key' => '_publicize_pending_1',
)
);
The above query will fetch all the posts which are pending for publicizing, and I guess will be somewhat faster than checking with the value. As we all know, in the normal WP environment index is available on meta_key
only, not on the meta_value
, however the WordPress VIP platform indexing meta_value
in the combination of meta_key but in some limit of characters. So I guess we can think of this way as well for this issue, still correct me if I have interrupted in wrong discussions.
Thanks! 🙂
@vishalkakadiya I think your solution should work fine. It taps into the same idea that is, always look for something that exists, instead of for something that doesn't. I suggest adding different values for a meta key, but having different meta-keys will most likely work just fine too.
I don't think indexing the meta_value column makes a lot of difference in this case, because the first thing the query will do is join the tables to contain only that meta_key. But then again, if we step into the millions of records within the join, then that might be a good replacement.
Is your enhancement related to a problem? Please describe.
It is very common that developers introduce new
meta_keys
without setting default values for posts that already existed in the database, or that they deletemeta_keys
when the value is not needed instead of setting it to a default or false value.This becomes an issue on larger database tables when users need to find some information by filtering not only by the meta_key and value but also sometimes by checking if that
meta_key
does not exist at all. This use case is covered inWP_Meta_Query
, allowing for the use ofcompare => 'NOT EXISTS'
This solution should, however, be discouraged, as it generates very expensive queries that can really hammer a database performance.
Example:
_publicize_pending
meta_key in wp_postmetaGenerates:
In contrast, this query checking for a specific value in the declared
meta_key
only needs to read 89,970 rows, or roughly 70% fewer rows.Generates: