Make WordPress Core

Opened 6 years ago

Closed 2 months ago

#48408 closed enhancement (worksforme)

Introduce eagerloading of meta data

Reported by: mattiasf's profile mattiasf Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords:
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 (4)

#1 follow-up: @ocean90
6 years 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
6 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 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!

This ticket was mentioned in Slack in #core-performance by swissspidy. View the logs.


2 months ago

#4 @swissspidy
2 months ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Closing as worksforme as per the above.

Note: See TracTickets for help on using tickets.