I'm writing a custom module for a news site. Stories have a field_importance that rates its importance on a scale of 1 to 10. I have a function that works out how this decays over time, so that a #8 story posted today rates higher than a #8 story posted yesterday. I'm planning on storing this in a field_weight for Views. I fear that updating all of those weights could be a fairly intense process, so I'm planning on putting it in hook_cron.

Even so, I'm worried about the performance of node_load. So far, everything I've seen on updating a field value requires you to first load the whole node. Looping over every story (even if I just limit it to those whose weights have not yet dropped to zero) and loading each one seems like it would take way more time and memory than necessary. My function needs to load:

  • The value of field_importance
  • The node's created time
  • Statistics on how many visits the node has had today

From that, I can calculate the new weight. My questions:

  1. Given what I'm loading, can I save significant time and memory by avoiding node_load?
  2. What would the best way be to do this without node_load?
  3. Can I use node_save to save the new field_weight value if I don't use node_load?

Comments

da_solver’s picture

Hi,
Sorry if these are dumb questions. I've learned never to assume anything :)

It sounds like you plan on implementing a custom field via the Drupal 7 field API. Is that correct?

Do you need the functionality of the Drupal 7 field api?

Or, would a simple custom database table suffice? You'd relate your custom table to "stories" by node id (nid) and version (vid) (if you need to support versions). A custom table would avoid the issue of loading all the attached fields of each related node instance.

At least something to consider.

Hope that helps a little.
Good Luck :)

jefgodesky’s picture

That does neatly side-step the whole issue. I guess the question is what it would take to bring that custom database value into views as something that I could sort the list on.

da_solver’s picture

Hi,
Are referring to the contributed module "views"? You don't need any contributed modules to "sort".

I would take a look at any of the Drupal core modules. Most of them select data, sort, display etc. By definition, all of the functionality (of Drupal's core) is performed without any contributed modules.

For example (I just picked something at random) :
http://api.drupal.org/api/drupal/modules!blog!blog.module/function/blog_block_view/7 , Selects blog posts and sorts them by creation date and time (I.E. ->orderBy('created', 'DESC').

I'm not that familiar with views. My understanding is, it's main purpose is to provide "ad hoc" reporting to end users. So again, is ad hoc reporting a critical part of your required functionality. There is no right or wrong. I'm simply posing an issue you could consider. Perhaps there is some other function a contributed module provides thats key to your requirements. However, if it's simply "sorting", that is done through the Drupal core database functions.

Hope that helps :)
Good Luck