WordPress自定义查询搜索速度慢

时间:2018-06-27 06:48:52

标签: mysql wordpress

我有一个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
        )

)

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

1 个答案:

答案 0 :(得分:0)

在无法看到您的数据结构,无法对各种参数进行基准测试等情况下很难做到。

您绝对可以做的就是将其缓存。如果您不需要此查询实时运行,则可以使用WordPress瞬态系统将查询结果存储所需的时间-例如秒,分钟或什至小时(取决于用例)。 https://codex.wordpress.org/Transients_API

此后,此查询将仅在每个特定时间运行一次,而不影响每个用户的每个页面加载。

不利的一面是,仍然会有用户点击该页面,并且负载对于他来说很慢-在这种情况下,您可以设置WordPress Cron任务来重新生成该查询的结果n seconds

或者,您可以使用各种WordPress缓存插件,但这对于您的情况而言可能太多了。 (例如-> https://wordpress.org/plugins/wp-super-cache/