-
-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add documentation for
WordPress.DB.SlowDBQuery
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?xml version="1.0"?> | ||
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd" | ||
title="Slow DB Query" | ||
> | ||
<standard> | ||
<![CDATA[ | ||
For performance reason, it's recommended to avoid doing lots of meta queries. | ||
The underlying SQL request generated by those queries can be extremely slow queries especially for complexe meta query. | ||
]]> | ||
</standard> | ||
<code_comparison> | ||
<code title="Valid: Using tax_query to retrieve a list of posts"> | ||
<![CDATA[ | ||
$query = new WP_Query( | ||
array( | ||
'tax_query' => array( | ||
array( | ||
'taxonomy' => 'color', | ||
'field' => 'slug', | ||
'terms' => array( | ||
'blue', | ||
'red', | ||
) | ||
) | ||
) | ||
) | ||
); | ||
]]> | ||
</code> | ||
<code title="Invalid: Using meta_query to retrieve a list of posts"> | ||
<![CDATA[ | ||
$query = new WP_Query( | ||
array( | ||
'meta_query' => array( | ||
array( | ||
'key' => 'color', | ||
'compare' => 'IN', | ||
'value' => array( | ||
'blue', | ||
'red', | ||
) | ||
) | ||
) | ||
) | ||
); | ||
]]> | ||
</code> | ||
</code_comparison> | ||
<code_comparison> | ||
<code title="Valid: in case of binary metadata (true/false) check if meta_key exist and remove it otherwise"> | ||
<![CDATA[ | ||
// mark post 123 as "featured" | ||
update_post_meta( 123, 'is_featured', true ); | ||
// remove "featured" state for post 456 | ||
delete_post_meta( 456, 'is_featured' ); | ||
$query = new WP_Query( | ||
array( | ||
'meta_key' => 'is_featured' | ||
), | ||
); | ||
]]> | ||
</code> | ||
<code title="Invalid: check for the value of a binary metadata, which is slower"> | ||
<![CDATA[ | ||
// mark post 123 as "featured" | ||
update_post_meta( 123, 'is_featured', true ); | ||
// mark post 456 as not "featured" | ||
update_post_meta( 456, 'is_featured', false ); | ||
$query = new WP_Query( | ||
array( | ||
'meta_key' => 'is_featured', | ||
'meta_value' => true | ||
), | ||
); | ||
]]> | ||
</code> | ||
</code_comparison> | ||
</documentation> |