Simple key => value array with QueryInterface I'm looking at building a simple key => value array with a certain type of node I have in order to generate a select list in a form.

The thing is that I have thousands of nodes of this type and the page takes forever to load with the current code I have.

Is there a way to load only certain fields using the QueryInterface in order to cut on loading times? Is there a more efficient way to do this?

$query = \Drupal::entityQuery('node')
  -&gt;condition('type', 'product')
  -&gt;condition('status', 1)
;

$nids = $query-&gt;execute();

$nodeStorage = \Drupal::entityTypeManager()-&gt;getStorage('node');

// Load multiple nodes
$nodes = $nodeStorage-&gt;loadMultiple($nids);

$list = array();

foreach ($nodes as $node) {
  $list[$node-&gt;field_code-&gt;value] = $node-&gt;title-&gt;value . ' (' . $node-&gt;field_code-&gt;value . ')';
}
The only way I know to do this is similar to what @Ivan Jaros mentions in the comment above. That is to use a Database service instead of EntityQuery service. There is a handy method on the query Statement, fetchAllKeyed, that I use all of the time in Drupal 7.

$database = \Drupal::service('database');
$query = $database-&gt;select('node', 'n');
$query-&gt;join('node_field_data', 'nfd', 'n.nid = n.nfd.nid');
$query
  -&gt;fields('n', ['nid']),
  -&gt;fields('nfd', ['title']) 
  -&gt;condition('n.type', 'product')
  -&gt;condition('nfd.status', 1)
  -&gt;execute()
  -&gt;fetchAllKeyed();


Unfortunately this has drawbacks:


Multilingual - you need to do this manually in the query above.
Probably not going to work well with non-SQL drivers.
Might run into performance issues still because the data is not cached.