Drupal core architecture: why no node pre-query hook?

Events happening in the community are now at Drupal community events on www.drupal.org.
willieseabrook's picture

Hi everyone,

This seems so obvious, so I'm sure this must have been thought of before and discarded.

So I'm really asking why this idea been discarded.

Why isn't there a pre-query hook on the node module, where other modules can add their joins?

Take your regular node orientated site, and in particular a listing page with say 50 nodes on it.

Current architecture means (number of nodes loaded) * (1 node query) * (number of modules you're using) * (number of queries each module uses in nodeapi/load)

So if you're using say: node, comment, node location, cck field, module x, module y (6 modules), it's not 1 query per node_load, its at least 6 and usually more like 20 as most modules execute more than one query per load.

And that's for a simple site. Many sites these days have upwards of 20 modules jumping in on the node_load fun, not a mere 6.

However, if there was a pre-query hook on node module, where each module could get their data by adding joins, you could head towards 1 query per node load.

This wouldn't cover one-many situations of course (although it may still help there), but that doesn't mean that improving one-one situations isn't worthwhile.

It should have significantly positive performance implications for site that load many nodes (for example on listing pages).

Further, if this strategy was implemented along with node_load_multiple (which I'm pretty sure is in 7), the performance implications would be even larger.

So, there's got to be a good reason (s) why this isn't done. What is it?

Comments

I was going to say that

Alexander N's picture

I was going to say that 'load' (in hook_nodeapi()'s $op terminology) already does what you're proposing, but it actually doesn't, since by the time 'load' is reached Drupal has already queried the DB to get the basic fields. So now I'm wondering about this too.

Can't find a good reason either. But it must be the same (or related) reason why hook_db_rewrite_sql() lets us add JOINs, WHEREs, and DISTINCTs to the queries and yet it doesn't provide a way to alter the SELECT clause.

I see that functions such as node_load() build their SELECT clause with code such as:

<?php
$fields
= drupal_schema_fields_sql('node', 'n');
?>

and hard coded stuff like:

<?php
$fields
= array_diff($fields, array('n.vid', 'n.title', 'r.nid'));
?>

What's funny is that node_load() builds the SELECT like that, it queries the database with it, and then it goes on to invoke hook_nodeapi() for all modules. Makes you think that all we need to do is swap these two bits of code and give hook_nodeapi() the possibility to add items to the SELECT list. But that means we'd be adding JOINs through hook_db_rewrite_sql() and SELECTs through node_api(), and that'd be sort of... displeasing...

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: