Support

Account

Home Forums General Issues Delete Row in repeater of 250+ very slow

Solving

Delete Row in repeater of 250+ very slow

  • I have a client I am doing work for, when I try to delete a row by index, it takes absolutely forever in order to do it, sometimes even timing out the server.

    Why is it doing this? is there a way to speed it up? Is my only other solution to remove rows fast to do it in MySQL custom queries?

  • You cannot delete a row through MySQL, this will break the repeater. An explanation of why it takes so long and why it will break things to delete.

    The database contains multiple entries. The repeater itself has a meta_key value of "{$repeater_name}" and this DB entry holds the number of rows in the repeater. Each sub field has a meta_key value of "{$repeater_name}_{$row_index}_{$sub_field_name}"

    Now lets say that you want to delete row 100 of 250 rows. All of the rows past 100 need to be re-saved. First meta key "{$repeater_name}_100_{$sub_field_name}" is deleted and then each following row is updated "{$repeater_name}_101_{$sub_field_name}" becomes "{$repeater_name}_100_{$sub_field_name}", "{$repeater_name}_102_{$sub_field_name}" becomes "{$repeater_name}_101_{$sub_field_name}", etc… When all of the sub rows are updated ACF then updates the number of rows in the repeater.

    Now lets say that your repeater has 5 sub fields, you can do this with any number of fields. The total number of DB queries that need to be done to update a repeater is

    
    $total_queries = ($number_of_sub_fields * $number_of_rows + 1) * 2;
    

    Repeaters have their uses, but they are not scalable. If you are going to have more than maybe a couple of dozen rows or you have a significant number of sub fields then a repeater is overall a bad choice to store the data. The same will happen with flexible content fields. When you have a large data set then a custom post type is generally a better idea. My personal opinion is that this should be stated in the documentation in big bold letters for both repeaters and flex fields.

    There has been many topics here over the years on the subject of the slowness of repeaters with a large number of rows.

    This will not change as long as ACF uses the 1 field = 1 query standard WP model for updating meta values.

  • I dont think if I know what I’m doing I will break anything. I am recounting rows manually with MySQL queries using counters. I’m having some trouble in some cases because I’m not excellent at MySQL. I feel these repeaters were not properly programmed in PHP. If j have 200 rows, 200+ queries should not be run.

  • Well, yes, 200+ will be run. ACF uses the core WP function update_post_meta() to update the DB. This is and always has been the way it works. update_post_meta() does one query every time it is called (sometimes 2, but that’s another story). Every ACF field has 2 DB entries.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.