WordPress.org

Make WordPress Core

Opened 16 months ago

Last modified 16 months ago

#23061 new enhancement

get_post_meta() on post with large amount of meta keys uses too much memory

Reported by: joehoyle Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Database Keywords:
Focuses: Cc:

Description

I think it's likely there has been a ticket on this in the past, but I couldn't find one so here goes!

If I have a post that have say 25k post meta entries, and I call get_post_meta( $id, 'foo', true ) then update_meta_cache is going to cause all post metas to be loaded into memory.

In most cases I can see this makes sense, however I recently had an issue where I was storing contest votes as meta keys, a post had 30k votes, and would take about 70MB of memory to get any meta key of that post (even if I didn't want votes). This is fairly edgecase, but the idea that all meta for a post is always loaded even if you don't want it is perhaps over presumptuous as a dev may be storing a large amount of data in meta.

With persistant object caching this is somewhat worse, as the object cache store gets very large.

I am not proposing this functionality is removed, maybe just a filter or something in http://core.trac.wordpress.org/browser/trunk/wp-includes/meta.php#L524 so one could either adjust the query or perhaps pass an array of meta keys to exclude from the sql query

Change History (3)

comment:1 follow-up: wonderboymusic16 months ago

get_post_meta is a bad choice here, you should probably be using a custom SQL SELECT using COUNT and GROUP BY. Votes and ratings aren't what those EAV tables are designed to store. I would suggest creating a join table

comment:2 in reply to: ↑ 1 joehoyle16 months ago

Replying to wonderboymusic:

get_post_meta is a bad choice here, you should probably be using a custom SQL SELECT using COUNT and GROUP BY. Votes and ratings aren't what those EAV tables are designed to store. I would suggest creating a join table

Hey, could you explain why the meta tables are not meant for something like vote tracking on a post? A vote seems to fit the scheme quite well, post | vote | vote_info and doing a count for total votes?

How would you see an ideal scheme for that?

Also, the voting was an example of large amounts of data -- of course it's not limited to that. With update_meta_cache it's not viable to put any amount of data you don't want permanently in memory, as of course it's not garbage collection due to the object cache.

comment:3 scribu16 months ago

There have been similar requests for user meta: http://core.trac.wordpress.org/ticket/15458#comment:1

Note: See TracTickets for help on using tickets.