Problem: Node has an Entityreference field, the selectbox widget loads all nodes from referenced content type. The number of nodes of this content type is over 2000, so the node/[nid]/edit page loads 36 sec. When I select autocomplete widget for entityreference field the same page loads 3 sec.

While handling this issue I`ve found:
1. getReferencableEntities function in EntityReference_SelectionHandler_Generic.class.php builds options array for the selectbox widget. In https://www.drupal.org/node/2151631 - the 2151631-getReferencableEntities-node-users-20.patch I`ve tested - it improves the performance so the page loads 3 sec. instead of 36 sec. - it fixes the problem by creating the options array using db_query.
2. Why is it so slow: The getReferencableEntities function uses function getLabel() to retrieve the label for each option. getLabel() goes to entity_metadata_no_hook_node_access function in Entity module. I`ve found that the preformance can be boosted if I change $default_revision = node_load($node->nid) to db_select, while not loading the whole node - we need just the "vid" field in that function. Such changes improve the performance form 36 sec to 5 sec for the node/[nid]/edit page.

It appears that Entity module performance can be improved by changing node_load to db_select, that in fact boosts the performance of Entityreference module.

Comments

hego created an issue. See original summary.

hego’s picture

StatusFileSize
new652 bytes

Status: Needs review » Needs work

The last submitted patch, 2: 2636332-entity_api_performance-1.patch, failed testing.

hego’s picture

temkin’s picture

Version: 7.x-1.1 » 7.x-1.x-dev
Status: Needs work » Needs review

Selected the right version. Marked for re-test.

temkin’s picture

azinck’s picture

This seems smart. Will test.

azinck’s picture

Status: Needs review » Reviewed & tested by the community

This does help; though not all that significantly if you're already running the entitycache module.

That said, RTBC #2

fago’s picture

Status: Reviewed & tested by the community » Closed (won't fix)

passing on this custom select result as a regular $node is an API violation which will lead to bugs once modules expect a regular entity. So this is something we cannot do.

azinck’s picture

@fago Can you elaborate on what you mean by your comment? Maybe I'm missing your point, but $default_revision is only used within our own function. Where is the API violation here?

ethant’s picture

StatusFileSize
new771 bytes

Rerolling to be compatible with entity 7.x-1.9