搜索产品woocommerce时查询缓慢

Posted

技术标签:

【中文标题】搜索产品woocommerce时查询缓慢【英文标题】:Slow query while searching a product woocommerce 【发布时间】:2018-11-21 12:43:10 【问题描述】:

最近几天,当我尝试在管理仪表板搜索输入中搜索产品时,我的查询速度很慢(4 秒以上的 sql 答案)。

查询监视器 4,782 秒(也在 mysql cli 中直接执行查询,我得到了相同的时间)wp_psotmeta 表是“大”200.000 行

SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id
    FROM wp_posts posts 
         LEFT JOIN wp_postmeta postmeta ON posts.ID = postmeta.post_id 
    WHERE ( posts.post_title LIKE '%INVMCB278A2W%' 
            OR posts.post_excerpt LIKE '%INVMCB278A2W%' 
            OR posts.post_content LIKE '%INVMCB278A2W%' 
            OR ( postmeta.meta_key = '_sku'
                 AND postmeta.meta_value LIKE '%INVMCB278A2W%' ) ) 
          AND posts.post_type IN ('product','product_variation') 
    ORDER BY posts.post_parent ASC, posts.post_title ASC

如何减少响应时间(不使用缓存)

+-------------------------------------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+------------------------+----------+----------------+---------+
| Name                                            | Engine | Version | Row_format | Rows   | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation              | Checksum | Create_options | Comment |
+-------------------------------------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+------------------------+----------+----------------+---------+
| wp_aryo_activity_log                            | InnoDB |      10 | Compact    |     26 |            630 |       16384 |               0 |            0 |         0 |            143 | 2018-06-11 21:31:04 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_aws_cache                                    | InnoDB |      10 | Compact    |      0 |              0 |       49152 |               0 |            0 |   6291456 |           NULL | 2018-06-11 21:31:04 | NULL        | NULL       | utf8mb4_unicode_520_ci |     NULL |                |         |
| wp_aws_index                                    | InnoDB |      10 | Compact    | 365747 |            104 |    38338560 |               0 |            0 |   7340032 |           NULL | 2018-06-11 21:31:05 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_commentmeta                                  | InnoDB |      10 | Compact    |    101 |            162 |       16384 |               0 |        32768 |         0 |            115 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_comments                                     | InnoDB |      10 | Compact    |    866 |            359 |      311296 |               0 |       294912 |         0 |            975 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_layerslider                                  | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_layerslider_revisions                        | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_links                                        | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        16384 |         0 |              1 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_masterslider_options                         | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        16384 |         0 |              2 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_masterslider_sliders                         | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        16384 |         0 |              2 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_newsletter                                   | InnoDB |      10 | Compact    |      5 |           3276 |       16384 |               0 |        32768 |         0 |              6 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_newsletter_emails                            | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_newsletter_sent                              | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        32768 |         0 |           NULL | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_newsletter_stats                             | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        32768 |         0 |              1 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_newsletter_user_logs                         | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_omega_sync_status                            | InnoDB |      10 | Compact    |      9 |           1820 |       16384 |               0 |            0 |         0 |           NULL | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_options                                      | InnoDB |      10 | Compact    |   7556 |            873 |     6602752 |               0 |      1507328 |   4194304 |         409398 | 2018-06-11 21:31:12 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_piraeus_bank_transactions                    | InnoDB |      10 | Compact    |      9 |           1820 |       16384 |               0 |            0 |         0 |             10 | 2018-06-11 21:31:13 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_piraeusbank_transactions                     | InnoDB |      10 | Compact    |    153 |            107 |       16384 |               0 |            0 |         0 |            188 | 2018-06-11 21:31:13 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_postmeta                                     | InnoDB |      10 | Compact    | 212550 |            175 |    37306368 |               0 |     24215552 |   4194304 |         245092 | 2018-06-11 21:31:13 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_posts                                        | InnoDB |      10 | Compact    |  17042 |           1016 |    17317888 |               0 |      4128768 |   4194304 |          25216 | 2018-06-11 21:31:20 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_revslider_css                                | InnoDB |      10 | Compact    |    109 |           1202 |      131072 |               0 |            0 |         0 |            110 | 2018-06-11 21:31:22 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_revslider_layer_animations                   | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:22 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_revslider_navigations                        | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              2 | 2018-06-11 21:31:22 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_revslider_sliders                            | InnoDB |      10 | Compact    |      2 |          24576 |       49152 |               0 |            0 |         0 |              3 | 2018-06-11 21:31:22 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_revslider_slides                             | InnoDB |      10 | Compact    |      6 |          57344 |      344064 |               0 |            0 |         0 |             22 | 2018-06-11 21:31:22 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_revslider_static_slides                      | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:22 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_slp_extendo_meta                             | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        49152 |         0 |              1 | 2018-06-11 21:31:23 | NULL        | NULL       | utf8mb4_unicode_520_ci |     NULL |                |         |
| wp_store_locator                                | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        49152 |         0 |              1 | 2018-06-11 21:31:23 | NULL        | NULL       | utf8mb4_unicode_520_ci |     NULL |                |         |
| wp_term_relationships                           | InnoDB |      10 | Compact    |  32541 |             48 |     1589248 |               0 |      1589248 |   4194304 |           NULL | 2018-06-11 21:31:23 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_term_taxonomy                                | InnoDB |      10 | Compact    |   2862 |             85 |      245760 |               0 |       294912 |         0 |           3120 | 2018-06-11 21:31:23 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_termmeta                                     | InnoDB |      10 | Compact    |   3510 |             70 |      245760 |               0 |       376832 |         0 |           3756 | 2018-06-11 21:31:23 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_terms                                        | InnoDB |      10 | Compact    |   2862 |             85 |      245760 |               0 |       278528 |         0 |           3120 | 2018-06-11 21:31:23 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_usermeta                                     | InnoDB |      10 | Compact    |   2380 |             96 |      229376 |               0 |       245760 |         0 |           4391 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_users                                        | InnoDB |      10 | Compact    |     56 |            292 |       16384 |               0 |        49152 |         0 |             59 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_wc_download_log                              | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        32768 |         0 |              1 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_520_ci |     NULL |                |         |
| wp_wc_webhooks                                  | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        16384 |         0 |              1 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_520_ci |     NULL |                |         |
| wp_woocommerce_api_keys                         | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        32768 |         0 |              1 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_attribute_taxonomies             | InnoDB |      10 | Compact    |    181 |            271 |       49152 |               0 |        16384 |         0 |            197 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_downloadable_product_permissions | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        49152 |         0 |              1 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_log                              | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        16384 |         0 |              1 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_order_itemmeta                   | InnoDB |      10 | Compact    |   6854 |             69 |      475136 |               0 |       442368 |         0 |           9572 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_order_items                      | InnoDB |      10 | Compact    |   1037 |             94 |       98304 |               0 |        49152 |         0 |           1478 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_payment_tokenmeta                | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        32768 |         0 |              1 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_payment_tokens                   | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        16384 |         0 |              1 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_sessions                         | InnoDB |      10 | Compact    |   4842 |           5755 |    27869184 |               0 |       491520 |   5242880 |         105968 | 2018-06-11 21:31:24 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_shipping_table_rates             | InnoDB |      10 | Compact    |    774 |            127 |       98304 |               0 |            0 |         0 |            781 | 2018-06-11 21:31:26 | NULL        | NULL       | utf8mb4_unicode_520_ci |     NULL |                |         |
| wp_woocommerce_shipping_zone_locations          | InnoDB |      10 | Compact    |   1317 |             74 |       98304 |               0 |       147456 |         0 |           3500 | 2018-06-11 21:31:26 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_shipping_zone_methods            | InnoDB |      10 | Compact    |    195 |             84 |       16384 |               0 |            0 |         0 |            207 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_shipping_zones                   | InnoDB |      10 | Compact    |     67 |            244 |       16384 |               0 |            0 |         0 |             71 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_tax_rate_locations               | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        32768 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woocommerce_tax_rates                        | InnoDB |      10 | Compact    |      2 |           8192 |       16384 |               0 |        65536 |         0 |              4 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woof_query_cache                             | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |        16384 |         0 |           NULL | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woof_stat_buffer                             | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_woof_stat_tmp                                | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_ci     |     NULL |                |         |
| wp_wpgmza                                       | InnoDB |      10 | Compact    |      3 |           5461 |       16384 |               0 |            0 |         0 |              5 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_wpgmza_categories                            | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_wpgmza_category_maps                         | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_wpgmza_circles                               | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_wpgmza_maps                                  | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              2 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_wpgmza_polygon                               | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_wpgmza_polylines                             | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_wpgmza_rectangles                            | InnoDB |      10 | Compact    |      0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8_general_ci        |     NULL |                |         |
| wp_yoast_seo_links                              | InnoDB |      10 | Compact    |     99 |            165 |       16384 |               0 |        16384 |         0 |            995 | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_520_ci |     NULL |                |         |
| wp_yoast_seo_meta                               | InnoDB |      10 | Compact    |   2089 |             54 |      114688 |               0 |            0 |         0 |           NULL | 2018-06-11 21:31:27 | NULL        | NULL       | utf8mb4_unicode_520_ci |     NULL |                |         |
+-------------------------------------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+------------------------+----------+----------------+---------+

Mysql解释命令

+------+-------------+----------+------+------------------+---------+---------+-----------------------+-------+----------------------------------------------+
| id   | select_type | table    | type | possible_keys    | key     | key_len | ref                   | rows  | Extra                                        |
+------+-------------+----------+------+------------------+---------+---------+-----------------------+-------+----------------------------------------------+
|    1 | SIMPLE      | posts    | ALL  | type_status_date | NULL    | NULL    | NULL                  | 17042 | Using where; Using temporary; Using filesort |
|    1 | SIMPLE      | postmeta | ref  | post_id          | post_id | 8       | theodoulidis.posts.ID |     8 | Using where; Distinct                        |
+------+-------------+----------+------+------------------+---------+---------+-----------------------+-------+----------------------------------------------+

【问题讨论】:

请在运行查询之前使用 EXPLAIN 关键字并发布它的输出,很可能它没有使用正确的索引或缺少索引。除此之外,LIKE 查询非常昂贵。 解释说key null,我在这里复制它 当您使用带有“like”的查询时,它的开销大于相等。 “或”也很昂贵。使用全文索引可能是个好主意。 dev.mysql.com/doc/refman/5.6/en/innodb-fulltext-index.html***.com/questions/5629491/… 【参考方案1】:

优化的方式太多了

WHERE ( posts.post_title LIKE '%INVMCB278A2W%' 
        OR posts.post_excerpt LIKE '%INVMCB278A2W%' 
        OR posts.post_content LIKE '%INVMCB278A2W%' 
        OR ( postmeta.meta_key = '_sku'
             AND postmeta.meta_value LIKE '%INVMCB278A2W%' ) ) 
      AND posts.post_type IN ('product','product_variation') 
ORDER BY posts.post_parent ASC, posts.post_title ASC

需要索引,但是...

OR 禁止使用索引。 两个表之间的OR 更糟。 带有前导通配符的LIKE 禁止使用索引。 IN (x, y)(多个选项)阻止将其与ORDER BY 一起使用。 UNION 代替 OR 如果没有其他一些需要“修复”的东西,那就太混乱了。 FULLTEXT 索引可以跨多个列使用,但只能在单个表中使用。 但是"INVMCB278A2W" 在技术上是一个“词”吗? 有 ways to speed up postmeta,但其他问题阻止了他们。

根据此处和其他人给您的提示,尽您所能解决,然后回来看看是否足够接近我们可以帮助完成任务。

【讨论】:

是的,是产品 wordpress 的代号只返回一行(产品 INVMCB278A2W ),同样在 1-2 周之前它“更快”,现在它停滞了 4-5 秒 @JohnPapadopoulos - “更快......” - 你有多少内存?整个数据集有多大 (GB)? innodb_buffer_pool_size的设置是什么?您使用的是 InnoDB 还是 MyISAM? 16GB 内存,数据库为 75 MB,发布表 17MB postmeta 32MB(表状态)innodb 虽然我运行 redis(当然第二次是最快的,但事实是这样。它是管理员搜索最多的时间只有一次搜索并且总是用户“看到” 5 秒延迟。【参考方案2】:

我遇到了同样的问题,并且没有多少索引或优化有帮助(63,000 个产品)。

我将查询拆开,发现是 SKU 搜索拖慢了速度。

在研究 Woocommerce 的代码数小时后,我终于找到了查询的形成位置并删除了 SKU 搜索。

要自己编辑 /wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php 文件,查找 >search_products 函数并从 "$term_group_query .=" 开始的行中删除 SKU 搜索(WC 版本 3.4.4 中的第 1397 行)

如果您愿意,您还可以在第 1423 行完全移除 wp_postmeta 的连接。

如果您想实际搜索 SKU,这显然不是一个合适的解决方案。

【讨论】:

【参考方案3】:

您可以在表的相关列上创建索引,例如;

ID, post_title, post_parent in wp_posts table,
post_id, wp_postmeta table

还为 varchar 字段添加索引并重写您的 where 条件,左侧没有通配符 (%)。例如:

posts.post_title LIKE '%INVMCB278A2W%' 

不会使用列 post_title 上的索引 但是

posts.post_title LIKE 'INVMCB278A2W%' 

将使用索引

【讨论】:

但是...OR 阻止使用这些索引。【参考方案4】:

试试这个 -

要添加的索引:

ALTER TABLE `wp_postmeta` ADD INDEX `wp_postmeta_idx_key_id_value` (`meta_key`,`post_id`,`meta_value`);
ALTER TABLE `wp_posts` ADD INDEX `wp_posts_idx_type_parent_title_id` (`post_type`,`post_parent`,`post_title`,`ID`);
ALTER TABLE `wp_posts` ADD INDEX `wp_posts_idx_content_parent_title_id` (`post_content`,`post_parent`,`post_title`,`ID`);
ALTER TABLE `wp_posts` ADD INDEX `wp_posts_idx_excerpt_parent_title_id` (`post_excerpt`,`post_parent`,`post_title`,`ID`);
ALTER TABLE `wp_posts` ADD INDEX `wp_posts_idx_title_parent_id` (`post_title`,`post_parent`,`ID`);

要执行的查询:

SELECT
        * 
    FROM
        ((SELECT
            posts.ID AS product_id,
            posts.post_parent AS parent_id,
            posts.post_title 
        FROM
            wp_posts posts 
        WHERE
            (
                (
                    1 = 1 
                    AND 1 = 1
                )
            ) 
            AND posts.post_type IN (
                'product', 'product_variation'
            ) 
            AND EXISTS (
                SELECT
                    * 
                FROM
                    wp_postmeta postmeta 
                WHERE
                    posts.ID = postmeta.post_id 
                    AND postmeta.meta_key = '_sku' 
                    AND postmeta.meta_value LIKE '%INVMCB278A2W%'
            ) 
        ORDER BY
            posts.post_parent ASC,
            posts.post_title ASC) 
        UNION
        DISTINCT (SELECT
            DISTINCT posts.ID AS product_id,
            posts.post_parent AS parent_id,
            posts.post_title 
        FROM
            wp_posts posts 
        WHERE
            (posts.post_content LIKE '%INVMCB278A2W%') 
            AND posts.post_type IN ('product', 'product_variation') 
        ORDER BY
            posts.post_parent ASC,
            posts.post_title ASC) 
    UNION
    DISTINCT (SELECT
        DISTINCT posts.ID AS product_id,
        posts.post_parent AS parent_id,
        posts.post_title 
    FROM
        wp_posts posts 
    WHERE
        (posts.post_excerpt LIKE '%INVMCB278A2W%') 
        AND posts.post_type IN ('product', 'product_variation') 
    ORDER BY
        posts.post_parent ASC,
        posts.post_title ASC) 
UNION
DISTINCT (SELECT
    DISTINCT posts.ID AS product_id,
    posts.post_parent AS parent_id,
    posts.post_title 
FROM
    wp_posts posts 
WHERE
    (posts.post_title LIKE '%INVMCB278A2W%') 
    AND posts.post_type IN ('product', 'product_variation') 
ORDER BY
    posts.post_parent ASC,
    posts.post_title ASC)
) AS union1 
ORDER BY
union1.post_parent ASC,
union1.post_title ASC

【讨论】:

以上是关于搜索产品woocommerce时查询缓慢的主要内容,如果未能解决你的问题,请参考以下文章

WooCommerce 按标签或描述搜索产品查询帖子

在 WooCommerce 购物车页面中的每个产品下方添加运输类别

WooCommerce 按用户角色删除购物车

在 WooCommerce 产品搜索中启用自定义分类

使用 WooCommerce 产品搜索搜索特定 post_type 表单 post_type 列时出错

WooCommerce 产品类别计数