我在WordPress WooCommerce管理中按名称或名称搜索产品时遇到了重大性能问题。大约有40 k产品,搜索需要16到50秒,这是相当慢的。此外,有时这种延迟会导致504错误。
我在网上寻找解决方案,浏览论坛,甚至尝试使用ChatGPT等聊天机器人,但还没有找到一个明确的解决方案。
使用Query Monitor插件,我发现了一个似乎是罪魁祸首的特定查询:
SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id
FROM wpprefix_posts posts
LEFT JOIN wpprefix_wc_product_meta_lookup wc_product_meta_lookup
ON posts.ID = wc_product_meta_lookup.product_id
LEFT JOIN wpprefix_wc_product_meta_lookup parent_wc_product_meta_lookup
ON posts.post_type = 'product_variation'
AND parent_wc_product_meta_lookup.product_id = posts.post_parent
WHERE posts.post_type IN ('product','product_variation')
AND ( ( ( posts.post_title LIKE '%DELETEME%')
OR ( posts.post_excerpt LIKE '%DELETEME%')
OR ( posts.post_content LIKE '%DELETEME%' )
OR ( wc_product_meta_lookup.sku LIKE '%DELETEME%' )
OR ( wc_product_meta_lookup.sku = ''
AND parent_wc_product_meta_lookup.sku LIKE '%DELETEME%' ) ))
ORDER BY posts.post_parent ASC, posts.post_title ASC
字符串
对于我们的用例,我们只需要搜索产品的基础上他们的名字(标题)和名称.我希望优化此搜索,特别是在WooCommerce管理,鉴于我们处理的产品数量庞大.
此外,我在服务器日志中观察到错误,例如:
upstream timed out unknown error while reading response header from upstream "/wp-admin/edit.php?s=DELETEME..."
型
我还注意到其他一些端点(如GET /wp-json/)的“从上游阅读响应头时出现未知错误”。
此外,当用户尝试在前端搜索产品时,它依赖于admin-ajax.php,但这也不能按预期运行。
我们在每个产品都有很大的描述,这是HTML代码meybe,导致搜索速度慢?
尽管在我的VPS设置上增加了资源,这些问题仍然存在。有人能指导我如何编写或调整我的主题的functions.php代码,以提高此搜索查询的性能并解决这些错误吗?任何帮助或指导都将不胜感激!
3条答案
按热度按时间cyvaqqii1#
多个带有
OR
运算符的LIKE
可能会降低查询的性能,特别是当列包含长文本内容时。字符串
假设你的数据库是MySQL,你可以在
wpprefix_posts
表上使用这样的FULLTEXT
索引:型
然后使用函数
型
用于代替
%LIKE
s和OR
s进行搜索。(see Natural Language Full-Text Searches)
表
wpprefix_wc_product_meta_lookup
也是如此。让我们知道这是否有帮助!
jgzswidk2#
选择DISTINCT p.ID as product_id,p.post_parent as parent_id从wpprefix_posts p左加入wpprefix_wc_product_Meta_lookup sku_lookup ON p.ID = sku_lookup.product_id其中p.post_type IN('product ','product_variation')AND(p.post_title LIKE '%DELETEME%' OR sku_lookup.sku LIKE '%DELETEME%')ORDER BY p.post_parent ASC,p.post_title ASC;
你能尝试一下,我已经使用OR运算符组合了搜索产品名称(post_title)和SKU(sku)的条件,我还维护了DISTINCT关键字,以确保只返回唯一的产品ID。并保留了ORDERBY子句,用于对结果进行排序
j5fpnvbx3#
核心WordPress中的搜索功能使用了臭名昭著的数据库性能反模式
some_content_column LIKE '%searchterm%'
。再多的MySQL / MariaDB调优或增加服务器资源也无法解决这个问题。这是由WordPress核心搜索算法的选择造成的。
尝试使用搜索插件来改变算法。我用relevanssi很成功。还有其他几个。
(In postgresql可以设置索引来加速那些LIKE搜索,但WordPress不支持该数据库。