Downloads value of node field only having nid by different ways and comparison the speed of their implementation
Often the challenge is to get the value of one field nodes, having only nid, without whole object of the node. Let we already have a node identifier that interests us: $ nid = $ some_value;
Download value of the required field can make by three convenient ways:
> Direct query in the database. A quick way to get the required value directly from SQL:
> Download the whole object of the node and get value from it. The most common way, the easiest, but only when in the node is few fields:
> Download the field value. Load the field value using field_get_items ():
All three options are simple, do not take a long to the writing, but performed quite differently and have a time difference of performance.
If you have a simple and small website, with a small load, the node_load () seem the easiest option, but at the load increases should think on performance and load on the server .
Verify the implementation of these variations on several different projects.
All test projects are on the same host on SSD drives:
A small project. Get the text box, node contains 7 text fields and 2 node_reference, the number of node ~ 600;
Execution time can be checked by at aid of the php function - microtime ().
As we see, when we have simple nodes and a small number of them - the fastest performed - node_load , but getting of the result through field_get_items () gave the worst results.
Online catalog with the ability to order products. Get the text box, the product contains 20 fields, 2 term_refernce 2 field_collection,number of node ~ 6000;
Results radically different - by increasing the size of the object node, function node_load () is executed several times longer. Not much better performed downloading through the pitch. However, as in the first version, a direct request to the base remained at approximately the same time of performance and showed the best results.
Big online shop on commerce, receive text box, more than 400 active modules, options of goods, the number of node ~ 14000;
If in the commerce in the field product_reference as Inlineentityform - Multiplevalues, then it is not very load a function node_load (). However, a direct request to the database by the field value is executed the fastest.
So we can safely say, that for getting the value of one field from the node, with only her ID, through the above options, the best way is the direct query to the database.