My customer is running MySQL 5.7.16 on Ubuntu16.04 as backend for WordPress site.
A lot of heavy, bad written SQLs.
I was able to improve situation significant by enabling MySQL cache, slow query log is almost empty now.
But – certain query doesn't use cache and I cannot understand why.
SQL uses two tables, which are pretty static, so it's not cache invalidation.
Any idea how I can debug that?
TIA, Vitaly
1) SHOW STATUS LIKE "qcache%";
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1700 |
| Qcache_free_memory | 49555584 |
| Qcache_hits | 17960465 |
| Qcache_inserts | 6928571 |
| Qcache_lowmem_prunes | 1850967 |
| Qcache_not_cached | 924283 |
| Qcache_queries_in_cache | 13303 |
| Qcache_total_blocks | 28441 |
+-------------------------+----------+
2)Example of SQL:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
LEFT JOIN wp_postmeta AS mt1
ON (wp_posts.ID = mt1.post_id
AND mt1.meta_key = '_pagefrog_fbia_status' )
WHERE 1=1
AND ( ( wp_postmeta.meta_key = '_pagefrog_fbia_status'
AND wp_postmeta.meta_value = '1' )
OR mt1.post_id IS NULL
OR ( wp_postmeta.meta_key = '_pagefrog_fbia_status'
AND wp_postmeta.meta_value = '' ) )
AND wp_posts.post_type = 'post'
AND ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10
Addenda
SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 83886080 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+-----------------------------+----------+
Best Answer
SHOW VARIABLES LIKE 'query_cache*';
Do you have this on
wp_postmeta
:PRIMARY KEY(post_ID, meta_key)
? If you can switch to that, performance should improve when the QC is not used. If you have trouble, let's seeSHOW CREATE TABLE wp_postmeta
; some WP versions are really lame when it comes to optimizing that schema.Do you need the
GROUP BY
?JOIN...GROUP BY
often suffers from the explosion-implosion syndrome that makes such queries slow. There may be a workaround.Will you be "paginating"? If so, use of
OFFSET
is inefficient.Getting rid of
SQL_CALC_FOUND_ROWS
will speed up the query.That is a confusing query, what is it trying to do?
Index
Change to this
Notes:
SELECT MAX(CHAR_LENGTH(meta_key)) FROM ..
to see what the current max is.(I have been ranting about WP for years. I would be happy to advise them.)