Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize SQL Queries for image counting and statistics #937

Closed
jeawhanlee opened this issue Dec 10, 2024 · 4 comments · Fixed by #957
Closed

Optimize SQL Queries for image counting and statistics #937

jeawhanlee opened this issue Dec 10, 2024 · 4 comments · Fixed by #957
Assignees
Labels
effort [S] 1-2 days of estimated development time
Milestone

Comments

@jeawhanlee
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Slow queries can some times cause time out on affected pages. see here

Describe the solution you'd like
Optimize the SQL Queries for the image counting and statistics

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@jeawhanlee jeawhanlee added the effort [S] 1-2 days of estimated development time label Dec 10, 2024
@piotrbak piotrbak added this to the 2.2.5 milestone Jan 9, 2025
@Khadreal
Copy link
Contributor

Khadreal commented Jan 13, 2025

Scope a solution ✅

For imagify_count_attachments, imagify_count_error_attachments , imagify_count_optimized_attachments which queries are a bit similar we should use EXISTS instead of Inner join as we currently have, this means creating a new function in here

For imagify_count_saving_data we should modify this query

SELECT p.ID
FROM $wpdb->posts AS p
$nodata_join
INNER JOIN $wpdb->postmeta AS mt1
ON ( p.ID = mt1.post_id AND mt1.meta_key = '_imagify_status' )
WHERE p.post_mime_type IN ( $mime_types )
AND p.post_type = 'attachment'
AND p.post_status IN ( $statuses )
AND mt1.meta_value = 'success'
$nodata_where
ORDER BY CAST( p.ID AS UNSIGNED )"
to something like this
Changed the Order by Cast and inner join to exist.

$wpdb->prepare(
        "SELECT p.ID
        FROM {$wpdb->posts} AS p
            %1s 
        WHERE p.post_mime_type IN (%s)
            AND p.post_type = %s
            AND p.post_status IN (%s)
            AND EXISTS (
                SELECT 1 
                FROM {$wpdb->postmeta} AS mt1
                WHERE mt1.post_id = p.ID 
                    AND mt1.meta_key = %s
                    AND mt1.meta_value = %s
            )
            %2s 
        ORDER BY p.ID + 0",
        [
            $nodata_join,
            implode(',', array_fill(0, count($mime_types), '%s')),
            'attachment',
            implode(',', array_fill(0, count($statuses), '%s')),
            '_imagify_status',
            'success',
            $nodata_where
        ]
    )

[Update]
Did a quick test with the new suggested query with the available data, the new queries response are faster
imagify_count_saving_data new query was about 12ms compared to the current 19ms
For others the different wasn't much, this could be due to the amount of images I have on my test site.

@wordpressfan
Copy link
Contributor

Thanks @Khadreal,

May I ask, Do u think this change will enhance the query time? did u do any tests to make sure that this will help? if yes, plz share them so we all can discuss about the best approach here.

@Khadreal
Copy link
Contributor

@wordpressfan I have updated the grooming with that

@Khadreal Khadreal self-assigned this Jan 17, 2025
@jeawhanlee
Copy link
Contributor Author

LGTM for a quick fix for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort [S] 1-2 days of estimated development time
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants