3

I have always known that loading multiple nodes at the same time is faster than loading nodes one by one.

What I wasn't aware of, is the huge performance difference between the two ways of loading nodes.

I made a little example to actually see the difference. Here's my results.

Load 1000 nodes one by one:

$start_time = microtime(TRUE);
foreach (range(1, 1000) as $nid) {
  $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);

  if ($node) {
    print_r($node->id());
  }
}
$end_time = microtime(TRUE);
print_r($end_time - $start_time);

Execution time: 10.7266 seconds.

Load 1000 nodes at the same time:

$start_time = microtime(TRUE);
$nids = [];
foreach (range(1, 1000) as $nid) {
  $nids[] = $nid;
}

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

foreach ($nodes as $node) {
  if ($node) {
    print_r($node->id());
  }
}
$end_time = microtime(TRUE);
print_r($end_time - $start_time);

Execution time: 1.1664 second.

How can loadMultiple be 9.2 times faster than loading the nodes one by one?

Is it the connection between Drupal and the database? What other factors are slowing down the process when loading the nodes one by one?

I have cleared the cache after every request to avoid the loaded nodes being cached.

I used the microtime function to calculate the exact execution time.

  • I really like your efforts on this site and I'm really happy about your activity here so please don't understand this wrong, I definitely don't want to scare you off. But while this question involves Drupal functions it's actually a question about PHP in general. It's up to you then to have a look a these functions and understand the bare PHP code which is being executed in there. If that's unclear you are better off asking on SO, in my opinion. – leymannx Jul 18 at 8:47
  • I think it’s Drupal related - the question is on how these two Drupal functions operate in the backend. – Jaypan Jul 18 at 8:50
  • Hi @leymannx, first of all thank you for the nice comment, I really appreciate. I just have a question. Imagine I'm going to ask this question on SO, I would have to tag the question drupal because PHP experts that doesn't know anything about Drupal would think \Drupal::entityTypeManager() that's definetely not a PHP function I know... So while it might be related to PHP code, I still think it's on topic here, because it's about some of Drupal's core functions and how they operate. – Jdrupal Jul 18 at 8:54
  • 3
    @leymannx This is definitely a Drupal-specific question, and there's actually quite a bit more to it than the current question/answer have gone into. For example, the premise of the multiple load being 20x quicker simply isn't true if relevant static caches have been primed. That, and other related, architectural decisions are, while not unique to or invented by Drupal, the result of specific decisions made by the core devs when this was built, so we should expect to cover it here IMO – Clive Jul 18 at 11:19
  • 1
    That's a really good question. I don't know how it got that many views on so shot time. I did not promote it or share it in any way. – Jdrupal Jul 22 at 16:32
7

It's likely that when you are loading individual nodes, you are loading the entity type manager, then for each note retrieve the storage, do a query to get the node data, and build the node. With the multiple process, some of the DB queries are likely combined, the entity type manager only needs to be initiated once, and the processing will be in a batch rather than having to run through the code multiple times.

  • 7
    Side note: EntityTypeManager won't be initiated multiple times. It's a service and therefore always the same instance will be used. The main performance difference between the two is definitely the db query(ies). – ssibal Jul 18 at 11:56
  • 1
    Did you notice that load() is a wrapper function for loadMutliple() in EntityStorageBase? – Insasse Jul 26 at 11:33
  • Yes but it will be called one thousand times - once for each node. – Jaypan Jul 26 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.