| @@ -320,13 +320,15 @@ | ||
| 320 | 320 | } |
| 321 | 321 | } else { |
| 322 | 322 | /** |
| 323 | 323 | * Filter the chunk size of the requests fetching the data. |
| 324 | - * 15,000 seems to be a good balance between memory used, speed, and number of DB hits. | |
| 324 | + * 2,000 seems to be a good balance between memory used, speed, and number of DB hits, | |
| 325 | + * while keeping the resulting `IN ()` clause well under hosts' SQL query size limits | |
| 326 | + * (e.g. WP Engine's query governor). | |
| 325 | 327 | * |
| 326 | 328 | * @param int $limit The maximum number of elements per chunk. |
| 327 | 329 | */ |
| 328 | - $limit = apply_filters( 'imagify_count_saving_data_limit', 15000 ); | |
| 330 | + $limit = apply_filters( 'imagify_count_saving_data_limit', 2000 ); | |
| 329 | 331 | $limit = absint( $limit ); |
| 330 | 332 | |
| 331 | 333 | $mime_types = Imagify_DB::get_mime_types(); |
| 332 | 334 | $statuses = Imagify_DB::get_post_statuses(); |
| @@ -358,17 +360,27 @@ | ||
| 358 | 360 | $attachment_ids = array_chunk( $attachment_ids, $limit ); |
| 359 | 361 | |
| 360 | 362 | while ( $attachment_ids ) { |
| 361 | 363 | $limit_ids = array_shift( $attachment_ids ); |
| 362 | - $limit_ids = implode( ',', $limit_ids ); | |
| 364 | + // Safety net: further split the chunk if its rendered `IN ()` list would still be too long. | |
| 365 | + $id_chunks = Imagify_DB::chunk_in_values( $limit_ids ); | |
| 366 | + $attachments = []; | |
| 363 | 367 | |
| 364 | - $attachments = $wpdb->get_col( // WPCS: unprepared SQL ok. | |
| 365 | - " | |
| 366 | - SELECT meta_value | |
| 367 | - FROM $wpdb->postmeta | |
| 368 | - WHERE post_id IN ( $limit_ids ) | |
| 369 | - AND meta_key = '_imagify_data'" | |
| 370 | - ); | |
| 368 | + foreach ( $id_chunks as $id_chunk ) { | |
| 369 | + $id_chunk_list = implode( ',', $id_chunk ); | |
| 370 | + | |
| 371 | + $chunk_attachments = $wpdb->get_col( // WPCS: unprepared SQL ok. | |
| 372 | + " | |
| 373 | + SELECT meta_value | |
| 374 | + FROM $wpdb->postmeta | |
| 375 | + WHERE post_id IN ( $id_chunk_list ) | |
| 376 | + AND meta_key = '_imagify_data'" | |
| 377 | + ); | |
| 378 | + | |
| 379 | + if ( $chunk_attachments ) { | |
| 380 | + $attachments = array_merge( $attachments, $chunk_attachments ); | |
| 381 | + } | |
| 382 | + } | |
| 371 | 383 | $wpdb->flush(); |
| 372 | 384 | |
| 373 | 385 | unset( $limit_ids ); |
| 374 | 386 | |