Every time a page view occurs, wordpress is sending a complex non-indexed query below. The exact same query is sent over and over and it seems that it is being passed through some "caching" code. But why doesn't the cache actually prevent re-executing the query over and over?
The query being caught, which is asked repeatedly on every page view, is:
# Query_time: 0 Lock_time: 0 Rows_sent: 320 Rows_examined: 1608 SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (2366,2363,4066,...etc, about 30 entries);
I noticed this using log-queries-not-using-indexes in mysql.
The pertinent call stack is:
- wp_get_nav_menu_items(3) in nav-menu.php (around line 495) calls
- get_posts in post.php (around line 1461) calls
- WP-Query->query in query.php (around line 2941) calls
- WP-Query->get_posts in query.php (around line 2767) calls
- update_post_caches in post.php (around line 4454) calls
- update_postmeta_cache in post.php (around line 4474) calls
- update_meta_cache in meta.php (around 560)
- which makes the nasty SELECT statement
The SELECT statement is basically getting all the info from wp_postmeta for menu items whose post_id there has term_taxonomy_id = 3 in wp_term_relationships.
In other words, it's getting some metadata corresponding to the menu bar.
But, why is the cache updated, and db re-queried, on every single page view? I have very little clue how the cache works (I'm just starting to read about it) and that's why I'm writing here. All I can say is that the "cache_results" is automatically turned on inside of the call to WP-Query->get_posts around line 1970. That's great, but why might the cache get refreshed on every single page view? If the cache is operating as I would normally expect a cache to operate, I don't get why it keeps making this query over and over.
How can I best fix the lack of effective caching? Or is this the expected behaviour?