0

Is the entity system in D7 not very well optimized? I am trying to iterate through 1500 entities using EntityFieldQuery(), and I had to extend my execution time to 300 seconds in order for the iteration to complete. I am getting 100% cpu usage just looping through each one.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type','student');
$query->fieldCondition('field_student_deleted','value',0);
$result = $query->execute();

foreach ($result['student'] as $student) {
  $data = entity_load_single('student',$student->id);
  krumo($data);
}

Getting the 300 second response with or without krumo. I know that fields in entities have to be connected with JOINS, which are a performance hit as it is. Is there any reliable way to do what I am trying to do? It seems silly that an SQL database can handle hundreds of thousands of queries at a time but the system gets bogged down trying to process 1500 records.

Note: I could load the data all at once (rather than one entity load at a time), but I am not sure if there's any difference in performance between running entity_load_single with a single entity ID vs entity_load with multiple IDs. under the hook, entity_load just runs entity_load_single in iteration to obtain the data. The indexes on the entities are all created using the entity construction kit. It's a UI designed entity not a programmatic one.

  • Can you load the data all at once rather than one entity load at a time? That can be expensive, and using entity_load passing in the array_keys of $result['student'] could be better. Making sure that your custom entity type also has better indexes might help, but otherwise the question is too broad. – mradcliffe Oct 24 at 17:34
  • I could, but I am not sure if there's any difference in performance between running entity_load_single with a single entity ID vs entity_load with multiple IDs. under the hook, entity_load just runs entity_load_single in iteration to obtain the data. The indexes on the entities are all created using the entity construction kit. It's a UI designed entity not a programatic one. – Ex0r Oct 24 at 18:03
  • Sure, but what about entity_load_multiple? – Clive Oct 24 at 18:04
  • entity_load_multiple isn't available in Drupal 7. That's what entity_load() was. I just can't believe something as simple as looping through an entity would be so taxing on a machine. It's also recommended to use EntityFieldQuery when getting all entities as it's supposedly faster and less taxing. Not sure how though as it seems it takes longer to run that than it does entity_load() – Ex0r Oct 24 at 19:35
  • That does seem outrageously slow for something basic. How many fields does your Student entity have? Is the 100% CPU mainly the database (assuming it runs local), or the php process? – Menno van den Heuvel Oct 25 at 10:17

Your Answer

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

Browse other questions tagged or ask your own question.