I have tried to use Drupal's batch_set() function to execute a SQL insert with 10.000 items. It exceeds 300 MB of PHP memory. 512 MB of PHP memory are getting the job done. This is the case, if I use the batch_set() function and if I do not. There is currently no benefit in my use-case, either I use batch_set() or I am not using it. I expect from batch_set() to keep the PHP memory consumption low.
Here is what I am doing. After an user updates the user account, I am updating all nodes for internal purpose. This service method's code is being triggered:
$batch = array( 'title' => 'Rebuilding user permissions', 'init_message' => 'Start rebuilding user permissions.', 'progress_message' => 'Completed @current step of @total.', 'error_message' => 'Rebuilding user permissions has encountered an error.', 'operations' => array( array( [get_class($this), 'updateGrants'], [$grantsByNid] ), ), ); batch_set($batch);
The $grantsByNid contains around 10.000 items. The method which is triggered via the batch, is this:
/**
* @param array $grantsByNid
*/
public static function updateGrants($grantsByNid) {
$query = \Drupal::service('database')->insert('node_access');
$query->fields(
['nid', 'langcode', 'fallback', 'gid', 'realm', 'grant_view', 'grant_update', 'grant_delete']
);
foreach ($grantsByNid as $nid => $grants) {
foreach ($grants as $grant) {
$query->values([$nid, $grant->langcode, 1, $grant->gid, $grant->realm, $grant->grant_view, $grant->grant_update, $grant->grant_delete]);
}
}
$query->execute();
}
How can I prevent $query->execute(); from causing this error:
( ! ) Fatal error: Allowed memory size of 201326592 bytes exhausted (tried to allocate 20480 bytes) in /home/jepster/PhpstormProjects/permissions-by-term-behat/drupal/core/lib/Drupal/Core/Cache/DatabaseCacheTagsChecksum.php on line 108
I have googled a lot and I cannot find out, why Drupal 8's database functionality is using all the memory and why the batch functionality is not preventing this.