最后几天,当我尝试在管理仪表板搜索输入中搜索产品时,我的查询速度很慢(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 Explain命令
+------+-------------+----------+------+------------------+---------+---------+-----------------------+-------+----------------------------------------------+
| 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 |
+------+-------------+----------+------+------------------+---------+---------+-----------------------+-------+----------------------------------------------+
答案 0 :(得分:2)
优化方式太多了
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"
技术上是"字"?解决你在这里和其他人给你的提示,然后回来看看它是否足够让我们帮助完成任务。
答案 1 :(得分:1)
您可以在表的相关列上创建索引,例如;
ID, post_title, post_parent in wp_posts table,
post_id, wp_postmeta table
同时为varchar字段添加索引, 在左侧重写不具有wildchard(%)的where条件 。例如:
posts.post_title LIKE '%INVMCB278A2W%'
不会在列post_title上使用索引 但是
posts.post_title LIKE 'INVMCB278A2W%'
将使用索引
答案 2 :(得分:1)
我遇到了同样的问题,没有任何索引或优化帮助(63,000个产品)。
我将查询拆开,发现是SKU搜索使速度变慢。
经过数小时的研究,我终于找到了查询的来源,并删除了SKU搜索。
要亲自编辑 /wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php 文件,请查找 search_products 函数并从“ $ term_group_query。=” (WC版本3.4.4中的1397行)开始的行中删除SKU搜索。
如果需要,还可以将连接完全删除到wp_postmeta上,位于第1423行的更远一点。
如果您想实际搜索SKU,显然这不是合适的解决方案。
答案 3 :(得分:0)
我实际上在过去一周内开始遇到完全相同的问题。
background-repeat: no-repeat
答案 4 :(得分:0)
改为尝试-
要添加的索引:
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