简体   繁体   English

mysql 同表内查询优化

[英]mysql query optimization within the same table

This following query is returning as a slow query under query monitor.以下查询在查询监视器下作为慢查询返回。 It takes approx 0.4sec.大约需要 0.4 秒。 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同表下查询(wp_postmeta),取2组meta_key和meta_value作为其条件

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.我尝试过使用gift_price 的meta_value > 1 并且它实际上减慢了它的速度。

The "meta" tables in WP have an inefficient schema specification. WP 中的“元”表具有低效的架构规范。 Recommendations: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta建议: http : //mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta

Index the wp_postmeta table索引 wp_postmeta 表

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 并检查索引是否正常工作

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') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM