Skip to content

Commit

Permalink
docs: add documentation for WordPress.DB.SlowDBQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
petitphp committed Jun 24, 2024
1 parent d77b020 commit d7a99a9
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions WordPress/Docs/DB/SlowDBQueryStandard.xml
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>

0 comments on commit d7a99a9

Please sign in to comment.