Avoid the Pitfall of node_load_multiple API Function

In a large website with many nodes, stop using the node_load_multiple function. It potentially limits the site growing.

According to the document: "This function should be used whenever you need to load more than one node from the database." But, I want to say that we should avoid using this function as this open the door to system crash in the future.

I had written a blog before Design a Drupal website with a million nodes in mind. I had used this function as an example. It is true that node_load_multiple function enhance the performance. But, it comes with a price. When we load thousands of node into the memory, it exhausts the web server memory instantly. Then we get this infamous message: "Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate XYZ bytes) in ...".

This issue brought me to an attention when I am reading the code done by one of the Drupal shops. In one of the recently launched high-profile project, the function is used to load all the show nodes in the system. It sounds to be troublesome to me at the beginning. We can not load all show nodes if we are not sure how many it will be? Where there are a small amount of node in the system, that is fine. But if it is for a system there are over hundred thousand nodes, that is a big problem. As time goes by, we add more shows into the system; The utility function needs more memory to handle it. The physical memory limits the ability to add infinity show nodes into the system.

What is the best way to handle this situation? Avoid using the node_load_multiple function. If we have to use the node_load_multiple, limit the number of the node for node_load_multiple to load.

Comments

I think it would be rare case where you would be using this function to load all the nodes in a system. Yes, that would not be good to use it for that. But there will be a lot of cases even in sites with thousands of node where you would use this function to load a limited number of nodes. If there is a situation where you have to load all the nodes on a site there are probably other problems with your code.

This article is pretty much wrong. It would be much more instructive to write about node_load_multiple() (or node_load() for that matter) being an expensive operation, and how to recognize and avoid usage that would cause scalability issues.

Avoid loading more data into memory than memory you have. That's a tautology. node_load_multiple() is incredibly useful and you should use it.

It is a slippy slope to the scalability issue. I think it worthwhile to bring attention to other developers. Will not waste time on it if I did not see this issue in our code and even a contributed module.

Saying not to use node_load_multiple because it would allow you to crash your site is like saying you shouldn't use PHP because it is not strongly typed. In both cases it's up to the developer to make good choices for their target system and blaming the tool for poor decisions made be the wielder of the tool is foolish.

Of course you should use node_load_multiple if you need to load multiple nodes. It's just a matter of passing in the nids you want. The title of the article is confusing at best and the article doesn't do a good job explaining the actual problem(loading all the nodes in the system). To be also honest, I have not seen anyone actually using this function to load all the nodes on the site without passing nids...that can indeed be irresponsible at times.

Why do you need to load all nodes? To get a field value? Use my sandbox project to do that: https://www.drupal.org/sandbox/mikeytown2/2209909 Every tool can be misused.

Yes doing node_load_multiple() is bad. But doing node_load_multiple(array(nids...)) is no problem at all. That's the reason the function utilizes not only an array of nids, but also conditions. If someone is loading all the nodes on a site in a single page request, then they are doing it wrong.

Add new comment

Target Image