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

Docs: add documentation for WordPress.DB.SlowDBQuery #2464

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading