Skip to content

Commit baea92e

Browse files
committed
docs: add documentation for WordPress.DB.SlowDBQuery
1 parent 29488fe commit baea92e

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0"?>
2+
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
4+
title="Slow DB Query"
5+
>
6+
<standard>
7+
<![CDATA[
8+
For performance reason, it's recommended to avoid doing lots of meta queries.
9+
The underlying SQL request generated by those queries can be extremely slow queries especially for complexe meta query.
10+
]]>
11+
</standard>
12+
<code_comparison>
13+
<code title="Valid: Using tax_query to retrieve a list of posts">
14+
<![CDATA[
15+
$query = new WP_Query(
16+
array(
17+
'tax_query' => array(
18+
array(
19+
'taxonomy' => 'color',
20+
'field' => 'slug',
21+
'terms' => array(
22+
'blue',
23+
'red',
24+
)
25+
)
26+
)
27+
)
28+
);
29+
]]>
30+
</code>
31+
<code title="Invalid: Using meta_query to retrieve a list of posts">
32+
<![CDATA[
33+
$query = new WP_Query(
34+
array(
35+
'meta_query' => array(
36+
array(
37+
'key' => 'color',
38+
'compare' => 'IN',
39+
'value' => array(
40+
'blue',
41+
'red',
42+
)
43+
)
44+
)
45+
)
46+
);
47+
]]>
48+
</code>
49+
</code_comparison>
50+
<code_comparison>
51+
<code title="Valid: in case of binary metadata (true/false) check if meta_key exist and remove it otherwise">
52+
<![CDATA[
53+
// mark post 123 as "featured"
54+
update_post_meta( 123, 'is_featured', true );
55+
56+
// remove "featured" state for post 456
57+
delete_post_meta( 456, 'is_featured' );
58+
59+
$query = new WP_Query(
60+
array(
61+
'meta_key' => 'is_featured'
62+
),
63+
);
64+
]]>
65+
</code>
66+
<code title="Invalid: check for the value of a binary metadata, which is slower">
67+
<![CDATA[
68+
// mark post 123 as "featured"
69+
update_post_meta( 123, 'is_featured', true );
70+
71+
// mark post 456 as not "featured"
72+
update_post_meta( 456, 'is_featured', false );
73+
74+
$query = new WP_Query(
75+
array(
76+
'meta_key' => 'is_featured',
77+
'meta_value' => true
78+
),
79+
);
80+
]]>
81+
</code>
82+
</code_comparison>
83+
</documentation>

0 commit comments

Comments
 (0)