-
Notifications
You must be signed in to change notification settings - Fork 27
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
Very slow query with large media library #747
Comments
Both of that queries could be optimized by filtering on the post table then join. SELECT p.ID FROM wp_posts AS p LEFT JOIN wp_postmeta AS imrwpmt1 ON ( p.ID = imrwpmt1.post_id AND imrwpmt1.meta_key = '_wp_attached_file' ) AND p.post_mime_type IN ( 'image/jpeg','image/png','image/gif','image/webp','application/pdf' ) AND p.post_type = 'attachment' AND p.post_status IN ( 'inherit','private' ) LEFT JOIN wp_postmeta AS imrwpmt2 ON ( p.ID = imrwpmt2.post_id AND imrwpmt2.meta_key = '_wp_attachment_metadata' ) WHERE imrwpmt2.meta_value IS NULL OR imrwpmt1.meta_value IS NULL OR imrwpmt1.meta_value LIKE '%://%' OR imrwpmt1.meta_value LIKE '_:\\%' OR ( LOWER( imrwpmt1.meta_value ) NOT LIKE '%.jpg' AND LOWER( imrwpmt1.meta_value ) NOT LIKE '%.jpeg' AND LOWER( imrwpmt1.meta_value ) NOT LIKE '%.jpe' AND LOWER( imrwpmt1.meta_value ) NOT LIKE '%.png' AND LOWER( imrwpmt1.meta_value ) NOT LIKE '%.gif' AND LOWER( imrwpmt1.meta_value ) NOT LIKE '%.webp' AND LOWER( imrwpmt1.meta_value ) NOT LIKE '%.pdf' ) LIMIT 1 To achieve that we could remove the conditions on post here. |
Root causeThe root cause is that we have first making the join with the full tables with will try to match all rows from both tables one by one. Scope a solutionThe solution for that is to filter the table before the join. To achieve that we could remove the conditions on post here. $join = $matching ? 'INNER' : 'LEFT';
$first = true;
foreach ( self::get_required_wp_metadata_aliases() as $meta_name => $alias ) {
if($first ) {
$clause .= "
$join JOIN $wpdb->postmeta AS $alias
ON ( $id_field = $alias.post_id AND $alias.meta_key = '$meta_name' $conditions)";
continue;
}
$clause .= "
$join JOIN $wpdb->postmeta AS $alias
ON ( $id_field = $alias.post_id AND $alias.meta_key = '$meta_name' )";
} Estimate effortEffort |
Well, as we discussed about it, I agree with this solution. Looks good to me :) |
Before submitting an issue please check that you’ve completed the following steps:
Describe the bug
Some queries are extremely slow when being used on the website with bug media library.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Queries should be optimised
Screenshots
Acceptance Criteria (for WP Media team use only)
The text was updated successfully, but these errors were encountered: