This patch removes the kind of redundant joins in Views queries that CCK tends to create. See http://groups.drupal.org/node/11288 for discussion.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | query-optimize-256381-2.patch | 2.65 KB | bjaspan |
| query-optimize.patch | 1.78 KB | bjaspan |
Comments
Comment #2
bjaspan commented| Status | File | Size |
|---|---|---|
| new | query-optimize-256381-2.patch | 2.65 KB |
The patch now handles adjust_join() correctly (I think :-)). I made a simple test case: Content type test1 has three single value fields:
- field_field1: text
- field_field2: integer
- field_noderef: noderef to 'story'
Without the optimization, we get the query:
SELECT node.nid AS nid,
node_data_field_field2.field_field2_value AS node_data_field_field2_field_field2_value,
node_data_field_field2.nid AS node_data_field_field2_nid,
node_data_field_field1.field_field1_value AS node_data_field_field1_field_field1_value,
node_data_field_field1.nid AS node_data_field_field1_nid,
field_noderef_nid.title AS field_noderef_nid_title,
field_noderef_nid.nid AS field_noderef_nid_nid
FROM {node} node
LEFT JOIN {content_type_type1} node_data_field_noderef ON node.vid = node_data_field_noderef.vid
INNER JOIN {node} field_noderef_nid ON node_data_field_noderef.field_noderef_nid = field_noderef_nid.nid
LEFT JOIN {content_type_type1} node_data_field_field2 ON node.vid = node_data_field_field2.vid
LEFT JOIN {content_type_type1} node_data_field_field1 ON node.vid = node_data_field_field1.vid
With the optimization, we get the query:
SELECT node.nid AS nid,
node_data_field_noderef.field_field2_value AS node_data_field_noderef_field_field2_value,
node_data_field_noderef.nid AS node_data_field_noderef_nid,
node_data_field_noderef.field_field1_value AS node_data_field_noderef_field_field1_value,
field_noderef_nid.title AS field_noderef_nid_title,
field_noderef_nid.nid AS field_noderef_nid_nid
FROM {node} node
LEFT JOIN {content_type_type1} node_data_field_noderef ON node.vid = node_data_field_noderef.vid
INNER JOIN {node} field_noderef_nid ON node_data_field_noderef.field_noderef_nid = field_noderef_nid.nid
I also fixed the style comments.
This patch has 11 lines of code (including four "}") and 28 lines of comments. ;-)
I just realized a problem. The optimization is working, but the selected fields are getting the wrong AS name: they are being based on the existed queued table name, not the optimized-away joined table name. This needs to be fixed.
Comment #3
bjaspan commentedDiscussion with Earl on IRC concludes that the field renaming issue I mentioned above is not a problem; Views does not guarantee that fields names will be consistent and provides other mechanisms for accessing the data reliably. Therefore, CNR.
Comment #5
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #1
merlinofchaos commentedQuick code style comments: unary operator ! should not have space between it and the variable it operates on. $quetab should probably be an actual word (I'm less strict about that than Drupal default style, though) but I'd be just as happy with $table for the variable.
We definitely have to run it through adjust_join or it won't match.
If the join is run through adjust_join you *may* not need to check relationship which means we might be able to do something slick with array_filter? It may be faster.