Optimize Page Load Time (Fix Slow Page Loading/Long Queries)
-
A client site was running extremely slow loading specific pages. After watching “top” on the server and seeing that it was waiting 5-8 seconds per page load time for MySQL to finish I figured it was a slow query somewhere. After enabling and checking the “slow query logs” I found that this plugin was a culprit.
SELECT ID FROM wp_posts WHERE guid = 'https://example.com/wp-content/upl oads/2017/01/example_image-210x140.jpg';
The site in question has over half a million rows in wp_posts, and the column guid isn’t indexed. These facts together would make the above query take 1.25-1.75 seconds each.
Some of the pages that are loading slowly contain anywhere from 4-20 categories that all load with images, and each time a category is loaded it runs the above query.
Looking into the plugin code, I found that the function z_taxonomy_image_url() was to blame. This function retrieves the URL stored in the wp_options table for any given term.
The problem is, the wp_options value is the full size. If you are requesting a resized image, then this won’t do. So the plugin’s function runs a sub-function z_get_attachment_id_by_url() using the URL it obtained from the wp_options lookup. z_get_attachment_id_by_url() then searches wp_posts for the ID with that URL as the guid by running the above slow query.
If you DO want the fullsize image, the plugin should never run the z_get_attachment_id_by_url() and instead just directly return the value obtained from the wp_options table.
Therefore the following code change helps:
File: categories-images.php
Line: 192
Change From:
if(!empty($taxonomy_image_url)) {
Change To:
if(!empty($taxonomy_image_url) && $size != 'full') {
This is make it ONLY run the z_get_attachment_id_by_url() function if you are requesting a size other then full.
If you are uploading the correct images you want for the category, and never use the resized versions, then this is a much needed fix.
A proper fix, hopefully for a future version of this plugin, would be to store the attachment_id in the wp_options table, and NOT the URL to the fill size image. Thereby eliminating the need to even search the wp_posts table all together – for fullsize and resized images.
- The topic ‘Optimize Page Load Time (Fix Slow Page Loading/Long Queries)’ is closed to new replies.