This following query is returning as a slow query under query monitor. It takes approx 0.4sec. Is there any way to improve its query speed?
Query under the same table (wp_postmeta), fetching 2 sets of meta_key and meta_value for its condition
SELECT p.post_id as id
FROM `wp_postmeta`as p, `wp_postmeta`as b
WHERE p.post_id = b.post_id
AND (b.meta_key = 'gift_price' and b.meta_value != '')
AND (p.meta_key = '_stock_status' and p.meta_value = 'instock')
I have tried to play with gift_price's meta_value > 1 and it actually slows it down even more.
The "meta" tables in WP have an inefficient schema specification. Recommendations: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta
Index the wp_postmeta table
ALTER TABLE `wp_postmeta`
ADD INDEX `IndexName` (`post_id` ASC, `meta_key` ASC, `meta_value` ASC);
Use EXPLAIN and check if the index is working
EXPLAIN SELECT p.post_id as id
FROM `wp_postmeta`as p, `wp_postmeta`as b
WHERE p.post_id = b.post_id
AND (b.meta_key = 'gift_price' and b.meta_value != '')
AND (p.meta_key = '_stock_status' and p.meta_value = 'instock')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.