wordpress自定义查询搜索速度慢

erhoui1w  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(386)

我有一个wordpress网站包含大量的帖子,我写了一个自定义的查询并将其传递给 WP_Query() ,这是数组的结构

Array
(
    [post_type] => Array
        (
            [0] => post
            [1] => innovations
        )

    [tag] => 
    [posts_per_page] => 10
    [paged] => 1
    [s] => 
    [tax_query] => Array
        (
            [0] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [taxonomy] => sector
                            [field] => id
                            [terms] => Array
                                (
                                    [0] => 15394
                                    [1] => 15399
                                    [2] => 15436
                                )

                            [include_children] => 1
                            [operator] => IN
                        )

                )

            [1] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [taxonomy] => business-model
                            [field] => id
                            [terms] => Array
                                (
                                    [0] => 15448
                                )

                            [include_children] => 1
                            [operator] => IN
                        )

                )

            [2] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [taxonomy] => technology
                            [field] => id
                            [terms] => Array
                                (
                                    [0] => 15470
                                    [1] => 15471
                                )

                            [include_children] => 1
                            [operator] => IN
                        )

                )

            [3] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [taxonomy] => topic
                            [field] => id
                            [terms] => Array
                                (
                                    [0] => 15456
                                )

                            [include_children] => 1
                            [operator] => IN
                        )

                )

        )

    [orderby] => Array
        (
            [post_date] => DESC
        )

)

它按预期工作,但执行此查询需要时间,速度非常慢。是否有其他方法可以提高其性能

knpiaxh1

knpiaxh11#

这是很难告诉不能够看到您的数据结构,能够基准各种参数等。
你能做的就是把它缓存起来。如果您不需要实时运行这个查询,那么您可以使用wordpress transients系统将查询结果存储到所需的时间,比如秒、分钟甚至小时(这取决于用例)。https://codex.wordpress.org/transients_api
之后,此查询将只在每个特定时间集运行一次,而不会影响每个用户的每个页面加载。
缺点是仍然会有一个用户点击页面,加载速度会很慢——在这种情况下,您可以设置wordpress cron task,以便每隔一天重新生成该查询的结果 n seconds .
或者,您可以使用各种wordpress缓存插件,但这对于您的场景来说可能太多了(例如->https://wordpress.org/plugins/wp-super-cache/)

相关问题