Why is accessing paragraphs slow? Is there a better approach? I'm trying to get the target_revision_id of a paragraph within a paragraph within a node. I can do this, but it is slow.

$all_nodes = db_select('node','e')-&gt;fields('e',array('nid'));
$all_nodes-&gt;condition('type','author');
$data = $all_nodes-&gt;execute();
$the_result = $data-&gt;fetchAll(\PDO::FETCH_ASSOC);
foreach ($the_result as $node){
    $node_load =  \Drupal\node\Entity\Node::load($node['nid']);
    foreach ($node_load-&gt;field_paragraphs as $item) {
      $para_id = $item-&gt;entity-&gt;field_affiliation_details-&gt;target_revision_id;
      //then, store the para_ids in some array
      $para_ids[] = $para_id;
    }
}


Performing the reference "$item->entity" makes my app slow down by a lot. It is really just Kilobytes of data. Is there maybe a better approach to this? 

Thanks for any help.
Well after fiddling around with it some more, I suppose the best answer here is to just cache the variable  $para_ids using \Drupal::cache() and then update it whenever there is a node/entity update or insert. I could look into database optimization to see what is actually happening with the $item->entity reference, and put an index on the joins/where statements but I am not very confident that is the best way.