I have some node ids from entityfieldquery. I would like to get the values of a field from all of these nodes. Should I use db_query, or should do a node_load_multiple, and get the field value with a foreach? db_query seems more efficient.
2 Answers
You can use field_attach_load() which will load the fields for a group of entities. You can also pass in the field_id value so it doesn't load unnecessary fields. I benchmarked field_attach_load()
against entity_load()
(which is what node_load_multiple will call) and the former was approximately ten times faster.
Here is an example:
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'post')
->propertyCondition('status', 1);
$result = $query->execute();
if (isset($result['node'])) {
// field_attach_load() requires entities to be passed on it with the bundle
// and the ID instead of just IDs. We need to add these values to the result
// of EntityFieldQuery()
foreach ($result['node'] as &$node) {
$node->bundle = $node->type;
$node->id = $node->nid;
}
field_attach_load('node', $result['node'], FIELD_LOAD_CURRENT, array('field_id' => 'body'));
}
ya u r right. node_load_multiple fires huge pressure on db and it can effect on ur site's performance. So, always use db_select or db_query for ur use, if there is no option available then only use loads....