最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Database Queries Optimization with new WP_Query

programmeradmin1浏览0评论

I’m building (mostly with a self-teaching purpose) a simple plugin to query posts from another blog on a MS network via a shortcode (like [network-posts blogs=”8" layout=”grid” columns=”3"]), but I’m running into some performance issues/doubts.

Here's the issue:

To my understanding (and maybe I’m wrong), when creating and looping through a new WP_Query, only 1 new query should be executed on the database.

When I use the shortcode (without switching to another blog), 7 new database queries are added.

When I switch to another blog (just 1 switch), the additional queries become 32.

The additional queries are called by update_meta_cache() and WP_Post::get_instance()

and comes in the form of an handful of selects:

SELECT post_id, meta_key, meta_value
FROM wp_11_postmeta
WHERE post_id IN (109)
ORDER BY meta_id ASC

and

SELECT *
FROM wp_11_posts
WHERE ID = 109
LIMIT 1

When switching to another blog, this two queries are duplicated for each post that's being loaded.

Is there a way to reduce the number of database queries?

The plugin is on github and this is the link to a simplified branch that I’m using for debugging purposes: Here switch_to_blog() is commented in the php code, but you can activate it by uncommenting line 40 and 64.

EDIT: the plugin has just ~70 lines of code distributed over two files. I didn't paste the code here because I thought it would have been more readable on github.

I’m building (mostly with a self-teaching purpose) a simple plugin to query posts from another blog on a MS network via a shortcode (like [network-posts blogs=”8" layout=”grid” columns=”3"]), but I’m running into some performance issues/doubts.

Here's the issue:

To my understanding (and maybe I’m wrong), when creating and looping through a new WP_Query, only 1 new query should be executed on the database.

When I use the shortcode (without switching to another blog), 7 new database queries are added.

When I switch to another blog (just 1 switch), the additional queries become 32.

The additional queries are called by update_meta_cache() and WP_Post::get_instance()

and comes in the form of an handful of selects:

SELECT post_id, meta_key, meta_value
FROM wp_11_postmeta
WHERE post_id IN (109)
ORDER BY meta_id ASC

and

SELECT *
FROM wp_11_posts
WHERE ID = 109
LIMIT 1

When switching to another blog, this two queries are duplicated for each post that's being loaded.

Is there a way to reduce the number of database queries?

The plugin is on github and this is the link to a simplified branch that I’m using for debugging purposes: https://github/langhenet/display-network-posts/tree/debug Here switch_to_blog() is commented in the php code, but you can activate it by uncommenting line 40 and 64.

EDIT: the plugin has just ~70 lines of code distributed over two files. I didn't paste the code here because I thought it would have been more readable on github.

Share Improve this question edited Dec 30, 2016 at 17:33 Enrico Cassinelli asked Dec 30, 2016 at 17:18 Enrico CassinelliEnrico Cassinelli 239 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can disable the caching if you set update_post_meta_cache and update_post_term_cache to false while setting the WP_Query arguments.

When caching is disabled the data will be requested from the database only on demand.

You can check my post in here. If you need more info just let me know.

发布评论

评论列表(0)

  1. 暂无评论