3

I created a custom module which lists the last X nodes from a certain content type, with the following query:

SELECT n.title, r.teaser, r.timestamp
FROM node n
INNER JOIN node_revisions r ON r.vid = n.vid
INNER JOIN term_node tn ON tn.nid = n.nid
INNER JOIN term_data t ON t.tid = tn.tid
WHERE t.name= 'breaking_news'
AND n.status = 1
ORDER BY n.nid DESC LIMIT 25

Then I do a node_load($nid) to check if there is a image and an audio file inside, for example

$mynode = node_load($queryresutl->nid);
if ($mynode->field_efe_audio[0]['filepath']) {  
  $audio = 'yep, has audio'; 
}

But I've read that node_load() could be a performance killer, so I wonder if this is true and if there's is an alternative to check fields in my module?

  • 1
    Did you try to directly query the field_data_field_efe_audio table using your nid as etid ? – silkAdmin Sep 19 '11 at 10:30
  • Do you mean, query the field_data_field_efe_audio table for each node ? I didn't. Should be better than node_load()? – atom Sep 19 '11 at 10:33
  • Yeah definitely, since node_load will query all field attached to your nodes. Other solution actually would be create a custom table that get updated on node creation/update/delete only if the current node contains an audio content, you can then just query that table. I think both solutions should be pretty much same performance, just a matter of organization – silkAdmin Sep 19 '11 at 10:38
  • To answer the question "is node_load slower" use the devel module -> Use the Devel module to analyze performance to test the different solutions. As "ram4nd" suggests, caching might be the real solution -> A beginner's guide to caching data in Drupal 7. – DeeZone Sep 19 '11 at 18:09
  • Thank you! let me check the link about devel module and performance analysis. Im already using cache in my custom module but in some sections I cant cache for a long time, just a few minutes – atom Sep 19 '11 at 19:03
5

First i would make sure that using node_load() does slower your script. If you are sure it really does you can either:

  • 1- Create an index_audio table with a nid and use_audio columns. You could update this table on node creation/update/delete. Then you can just query this table to make sure a specific node does have audio attached to it. I would use that solution only if i you have to intensively make such a check

  • 2- Directly query your field table with your nid as etid, in your instance : field_data_field_efe_audio table see if it returns FALSE or some actual data

|improve this answer|||||
  • Well I also need the image filepath so maybe solution 1 doesnt fit my needs, but #2 could be. The thing I trying to figure out is if 2 queries per node are better than one call to node_load! Thank you – atom Sep 19 '11 at 14:05
  • It will be faster as this you query less data from the DB.. Now is it worth the trouble ? I guess it probably depends on how complex is your node, and how intensively will this query run on your production site. – silkAdmin Sep 19 '11 at 15:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.