WordPress.org

Make WordPress Core

Opened 10 months ago

Last modified 10 months 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: @ocean90
10 months ago

  • Focuses coding-standards removed
  • Keywords reporter-feedback added
  • Severity changed from major to normal

Hello @mattiasf, welcome to WordPress Trac!

I think what you're looking for is the update_post_meta_cache argument of WP_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 calling update_postmeta_cache().

#2 in reply to: ↑ 1 @mattiasf
10 months ago

Replying to ocean90:

Hello @mattiasf, welcome to WordPress Trac!

I think what you're looking for is the update_post_meta_cache argument of WP_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 calling update_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!

Note: See TracTickets for help on using tickets.