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

Closes #747 fix slow queries #749

Merged
merged 7 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions inc/classes/class-imagify-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,20 @@ public static function get_post_statuses() {
* It returns an empty string if the database has no attachments without the required metadada.
* It also triggers Imagify_DB::unlimit_joins().
*
* @since 1.7
* @access public
* @author Grégory Viguier
*
* @param string $id_field An ID field to match the metadata ID against in the JOIN clause.
* @param string $id_field An ID field to match the metadata ID against in the JOIN clause.
* Default is the posts table `ID` field, using the `p` alias: `p.ID`.
* In case of "false" value or PEBKAC, fallback to the same field without alias.
* @param bool $matching Set to false to get a query to fetch metas NOT matching the file extensions.
* @param bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway.
* @param bool $matching Set to false to get a query to fetch metas NOT matching the file extensions.
* @param bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway.
* @param string $special_join_conditions Special conditions to apply on the join.
*
* @return string
* @author Grégory Viguier
*
* @since 1.7
* @access public
*/
public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $matching = true, $test = true ) {
public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $matching = true, $test = true, $special_join_conditions = '' ) {
global $wpdb;

if ( $test && ! imagify_has_attachments_without_required_metadata() ) {
Expand All @@ -186,7 +188,16 @@ public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID',

$join = $matching ? 'INNER' : 'LEFT';

$first = true;

foreach ( self::get_required_wp_metadata_aliases() as $meta_name => $alias ) {
if ( $first ) {
$first = false;
$clause .= "
$join JOIN $wpdb->postmeta AS $alias
ON ( $id_field = $alias.post_id AND $alias.meta_key = '$meta_name' $special_join_conditions )";
continue;
}
$clause .= "
$join JOIN $wpdb->postmeta AS $alias
ON ( $id_field = $alias.post_id AND $alias.meta_key = '$meta_name' )";
Expand Down Expand Up @@ -299,6 +310,9 @@ public static function get_extensions_where_clause( $args = false ) {
$extensions = array_keys( imagify_get_mime_types() );
$extensions = implode( '|', $extensions );
$extensions = explode( '|', $extensions );
$extensions = array_map(function ( $ex ) {
return strrev( $ex );
}, $extensions);
}

if ( ! $alias ) {
Expand All @@ -312,10 +326,12 @@ public static function get_extensions_where_clause( $args = false ) {
return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ];
}

$regex = '^' . implode( '\..*|^', $extensions ) . '\..*';

if ( $matching ) {
$query[ $key ] = "AND ( LOWER( $alias.meta_value ) LIKE '%." . implode( "' OR LOWER( $alias.meta_value ) LIKE '%.", $extensions ) . "' )";
$query[ $key ] = "AND REVERSE (LOWER( $alias.meta_value )) REGEXP '$regex'";
} else {
$query[ $key ] = "OR ( LOWER( $alias.meta_value ) NOT LIKE '%." . implode( "' AND LOWER( $alias.meta_value ) NOT LIKE '%.", $extensions ) . "' )";
$query[ $key ] = "AND REVERSE (LOWER( $alias.meta_value )) NOT REGEXP '$regex'";
}

return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ];
Expand Down
5 changes: 4 additions & 1 deletion inc/functions/admin-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ function imagify_count_attachments() {

$mime_types = Imagify_DB::get_mime_types();
$statuses = Imagify_DB::get_post_statuses();
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause();
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause('p.ID', true, true,
"AND p.post_mime_type IN ( $mime_types )
AND p.post_type = 'attachment'
AND p.post_status IN ( $statuses )");
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause();
$count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok.
"
Expand Down
9 changes: 8 additions & 1 deletion inc/functions/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ function imagify_has_attachments_without_required_metadata() {

$mime_types = Imagify_DB::get_mime_types();
$statuses = Imagify_DB::get_post_statuses();
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause( 'p.ID', false, false );
$nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(
'p.ID',
false,
false,
"AND p.post_mime_type IN ( $mime_types )
AND p.post_type = 'attachment'
AND p.post_status IN ( $statuses )"
);
$nodata_where = Imagify_DB::get_required_wp_metadata_where_clause( array(
'matching' => false,
'test' => false,
Expand Down
Loading