[wp-trac] [WordPress Trac] #3273: Post Meta caching mechanism leads to unnecessary queries

WordPress Trac wp-trac at lists.automattic.com
Sun Oct 22 07:07:32 GMT 2006


#3273: Post Meta caching mechanism leads to unnecessary queries
----------------------------+-----------------------------------------------
 Reporter:  markjaquith     |       Owner:  markjaquith
     Type:  defect          |      Status:  new        
 Priority:  normal          |   Milestone:  2.1        
Component:  Administration  |     Version:  2.1        
 Severity:  normal          |    Keywords:  post_meta  
----------------------------+-----------------------------------------------
 If  you do {{{ get_post_meta($post->ID, 'yourkey', true); }}} on each post
 in a loop, you'll get:

  * One query that fetches all of the postmeta fields (done automatically
 by WP's {{{update_post_caches()}}})
  * One query for each post that does not have 'yourkey'

 This can lead to a lot of unnecessary queries.

 The problem is that {{{get_post_meta()}}} checks:

 {{{ isset($post_meta_cache[$post_id][$key]) }}}

 This returns false, because the key doesn't exist for that post, and a SQL
 query is made.

 We need to make it check {{{ $post_meta_cache[$post_id] }}}.  If THAT
 exists, and the key doesn't, return false.

 This would require another behavior change in {{{get_post_meta()}}} to
 protect against this scenario:

  1. Query a postmeta key for a post not in the cache
  1. Query a different postmeta key for that same post (returns false, even
 though the key may exist)

 To remedy this, we could do the following in {{{get_post_meta()}}}

 If a request is made for a post's key, and that post isn't in the cache at
 all, freshen the cache for ALL of that post's meta keys (would require
 modifying other functions slightly)

 I'll work on a patch.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/3273>
WordPress Trac <http://wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list