Clicky

Downloads value of node field only having nid by different ways and comparison the speed of their implementation

 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 ().

Results:

TYPE

MESSAGE

exec_time_field_load

0.00136303901672

exec_time_node_load

1.50203704834E-5

exec_time_sql_query

0.000192880630493

   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:

TYPE

MESSAGE

exec_time_field_load

0.0032691955566406

exec_time_node_load

0.0067908763885498

exec_time_sql_query

0.00057196617126465

   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;

Results:

TYPE

MESSAGE

exec_time_field_load

0.0061240196228

exec_time_node_load

0.000473022460938

exec_time_sql_query

0.000407934188843

   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.

Conclusions

   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.

Category: 
Quick Free Quote
We respect your privacy. NO SPAM No selling your personal data.
We will respond to your query & collect further details within 24 hours. Guaranteed!

Support

We are friendly people who love to talk. So go ahead and contact us.

Technologies

Awards & Certification