Get taxonomy name in cycle (performance) I have a subset of data ($query->execute) where 1 column is an ID of taxonomy.

I need to replace 'term id' on 'term name'. Right now I just make 'lookup' for each id and replace it with name:

foreach ($entries as $entry) {
  $row = array_map('check_plain', (array) $entry);

  // --&gt; HERE I WANT TO AVOID LOOKUP FOR SAME ID
  $location = taxonomy_term_load($row['term']);
  $row['term']=$location-&gt;name;
}


However I'm not sure if it is a proper way to do that, since I have many similar ID and I want to avoid useless lookups (so I do lookup only for 'unknown' ID)

Should I build my own cache variable-list for every new ID or there is already an efficient way to do it?
You can use custom query like next:

$result = db_select('taxonomy_term_data', 't')
  -&gt;fields('t', array('tid', 'name'))
  -&gt;condition('t.tid', $tids, 'IN')
  -&gt;execute()-&gt;fetchAllKeyed();


Or you can do join with entity table for single query.