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

Process DB migration 5000 rows at a time instead of all at once. #905

Merged
merged 8 commits into from Jan 15, 2017
53 changes: 31 additions & 22 deletions includes/db-updates.php
Expand Up @@ -60,31 +60,40 @@ function wp_stream_update_auto_300( $db_version, $current_version ) {
$plugin = wp_stream_get_instance();
$plugin->install->install( $current_version );

$stream_entries = $wpdb->get_results( "SELECT * FROM {$wpdb->base_prefix}stream_tmp" );
$starting_row = 0;
$rows_per_round = 5000;

foreach ( $stream_entries as $entry ) {
$context = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}stream_context_tmp WHERE record_id = %s LIMIT 1", $entry->ID )
);

$new_entry = array(
'site_id' => $entry->site_id,
'blog_id' => $entry->blog_id,
'user_id' => $entry->author,
'user_role' => $entry->author_role,
'summary' => $entry->summary,
'created' => $entry->created,
'connector' => $context->connector,
'context' => $context->context,
'action' => $context->action,
'ip' => $entry->ip,
);

if ( $entry->object_id && 0 !== $entry->object_id ) {
$new_entry['object_id'] = $entry->object_id;
$stream_entries = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}stream_tmp LIMIT %d, %d", $starting_row, $rows_per_round ) );

while ( ! empty( $stream_entries ) ) {
foreach ( $stream_entries as $entry ) {
$context = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}stream_context_tmp WHERE record_id = %s LIMIT 1", $entry->ID )
);

$new_entry = array(
'site_id' => $entry->site_id,
'blog_id' => $entry->blog_id,
'user_id' => $entry->author,
'user_role' => $entry->author_role,
'summary' => $entry->summary,
'created' => $entry->created,
'connector' => $context->connector,
'context' => $context->context,
'action' => $context->action,
'ip' => $entry->ip,
);

if ( $entry->object_id && 0 !== $entry->object_id ) {
$new_entry['object_id'] = $entry->object_id;
}

$wpdb->insert( $wpdb->base_prefix . 'stream', $new_entry );
}

$wpdb->insert( $wpdb->base_prefix . 'stream', $new_entry );
$starting_row += $rows_per_round;

$stream_entries = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}stream_tmp LIMIT %d, %d", $starting_row, $rows_per_round ) );
}

$wpdb->query( "DROP TABLE {$wpdb->base_prefix}stream_tmp, {$wpdb->base_prefix}stream_context_tmp" );
Expand Down