Opened 5 years ago
Last modified 5 years ago
#48408 new enhancement
Introduce eagerloading of meta data
Reported by: | mattiasf | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Query | Keywords: | reporter-feedback |
Focuses: | performance | Cc: |
Description
I believe it should be possible to introduce eagerloading to postmeta/attachments.
In "Laravel" language you would do:
<?php
$posts = Post::with('meta')->take(10)->get();
?>
Which would produce two queries: 1) for the posts table 2) for the post_meta table where post ids IN posts result.
That would produce TWO queries to fetch 10 posts and ALL of its meta data, instead of currently where it produces a lot of requests to the database.
A SO user produced a draft that illustrates how it could work, see: https://wordpress.stackexchange.com/a/225743/50042
Speed is alfaomega in these times, and I think WordPress should take it way more serious.
Change History (2)
#1
follow-up:
↓ 2
@
5 years ago
- Focuses coding-standards removed
- Keywords reporter-feedback added
- Severity changed from major to normal
#2
in reply to:
↑ 1
@
5 years ago
Replying to ocean90:
Hello @mattiasf, welcome to WordPress Trac!
I think what you're looking for is the
update_post_meta_cache
argument ofWP_Query
, see https://developer.wordpress.org/reference/classes/wp_query/#caching-parameters. When set to true it fetches all meta data in one query by callingupdate_postmeta_cache()
.
Thanks for prompt reply, and thanks for the welcome.
So in fact, what you're saying is that eager loading is already introduced in the current versions of WordPress? So when fetching posts, say through WP_Query, and then calling wp_postmeta
it makes a query to fetch all meta for all $posts in the WP_Query? If that's the case, then that's great news!
Hello @mattiasf, welcome to WordPress Trac!
I think what you're looking for is the
update_post_meta_cache
argument ofWP_Query
, see https://developer.wordpress.org/reference/classes/wp_query/#caching-parameters. When set to true it fetches all meta data in one query by callingupdate_postmeta_cache()
.