Skip to content

Commit a875348

Browse files
committed
Merge branch '5.0/rt-importer-split-batch-for-large-query-2' into 5.0-trunk
2 parents aadf50a + ab28366 commit a875348

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

lib/RT/Migrate/Importer.pm

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,33 @@ sub _BatchCreate {
842842
$values_paren = $1;
843843
}
844844

845-
# DBs have placeholder limitations(64k for Pg), here we replace
846-
# placeholders to support bigger batch sizes. The performance is similar.
847-
my $batch_sql
848-
= $RT::Handle->FillIn( $sql . ( ", $values_paren" x ( $count - 1 ) ), [ map @$_, @{ $query{$sql} } ] );
849-
$self->RunSQL($batch_sql);
845+
# Enforce a 256MB limit on query strings to get around database limitation.
846+
my @query_bind_vals;
847+
my ( $vals_size, $bind_vals_batch ) = ( 0, [] );
848+
foreach my $bind_vals ( @{ $query{$sql} } ) {
849+
my $length;
850+
$length += length $_ for grep { defined $_ } @$bind_vals;
851+
852+
# check if over 250 MB to leave some extra room for $sql, commas, and quotes
853+
if ( $vals_size + $length > 250 * 1024**2 ) {
854+
push @query_bind_vals, $bind_vals_batch;
855+
$vals_size = 0;
856+
$bind_vals_batch = [];
857+
}
858+
push @$bind_vals_batch, $bind_vals;
859+
$vals_size += $length;
860+
}
861+
push @query_bind_vals, $bind_vals_batch if @$bind_vals_batch;
862+
863+
foreach my $bind_vals (@query_bind_vals) {
864+
$count = @$bind_vals;
865+
866+
# DBs have placeholder limitations(64k for Pg), here we replace
867+
# placeholders to support bigger batch sizes. The performance is similar.
868+
my $batch_sql
869+
= $RT::Handle->FillIn( $sql . ( ", $values_paren" x ( $count - 1 ) ), [ map @$_, @$bind_vals ] );
870+
$self->RunSQL($batch_sql);
871+
}
850872
}
851873

852874
# Clone doesn't need to return anything

0 commit comments

Comments
 (0)