I had the URL widget running at one point and it seemed ok, but now it is taking about 20 seconds to present a page and it consumes gobs of memory. The DEVEL query log shows the following only when URL widget is enabled:

5368.651_nodereference_potential_references_standard
SELECT DISTINCT n.nid, n.title AS node_title, n.type AS node_type FROM node n WHERE (n.type = 'thing') ORDER BY n.title, n.type

I am referencing about 600,000 nodes of a single type. If I disable the widget the page loads fine. I am using nodereference_autoreference, and although the system is not telling me I need to rebuild permissions I'm wondering if this query is taking a long time in checking permissions for each node? Does anyone have any suggestions as to what I should troubleshoot? As I said, it was working before as far as I can recall, but now it takes along time and I have to jack the php memory limits up to absurd levels just to get the edit page to load...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

Which page are you getting these slow queries on: when viewing a node that has a Node Reference link on it or when creating/editing a node that contains a node reference?

The query you've referenced comes from Node Reference module itself, and as far as I can tell Node Reference URL doesn't ever call it directly (though it might happen if you're using the autocomplete fallback or when saving the node). But I can't find anything that would cause Node Reference URL Widget to cause this behavior. You should try using just a normal Node Reference widget (like Autocomplete) and see if the problem exists there also.

Something that may be more efficient in your case: Use a view to restrict the list of possible nodes instead of restricting by type. This will allow you to optimize your query by removing the ORDER BY and avoiding any sorts on the returned list.

quicksketch’s picture

Ah, okay I found where this is being called. Node Reference URL Widget validates that a NID is a valid reference before showing the widget on the node edit form. Here's the stack trace of sorts:

nodereference_url_widget()
optionwidgets_options()
content_allowed_values()
nodereference_allowed_values()
_nodereference_potential_references()
_nodereference_potential_references_standard()

It's worth noting the Node Reference itself will do this same validation upon saving the node, Node Reference URL Widget simply does it when the form is displayed before prepopulating the field, so you'd run into this on save with a normal widget, whereas you get it both on save and on the display of the form when using Node Reference URL Widget.

The only solution I can see in your case is using a View to optimize the query instead of letting Node Reference use its default one.

quicksketch’s picture

Title: Passing reference from URL grinds system to a halt » Optimize validation of referenced NIDs from the URL
Category: support » task
Status: Active » Needs review
FileSize
758 bytes

Heh, okay I know this is a lot of info all at once. I still think Node Reference is a part of the problem, but this patch might be able to help things further. Right now Node Reference URL Widget actually gets a full list of ALL referenceable nodes. This means in your case, it builds an array of 600,000 nid => title mappings, just to see if a NID exists in that list. I think this is because I wanted to avoid a call to a "private" function in Node Reference module, but if this gives us a significant boost, then it'll be worth maintaining.

Could you apply this patch and see if it helps with your problem?

Todd Young’s picture

That was amazing response time, thanks! I think you're right on target, I will apply the patch and report back.

Todd Young’s picture

Initial report: Unless I made a mistake, it seems to break URL reference. The field comes up blank.

quicksketch’s picture

Shoot, well that's what I get for putting up a patch and not trying it myself. This patch should work, but your report on if this fixes the performance problem would still be invaluable.

Todd Young’s picture

FWIW, if I comment out your NULL check it works!

Todd Young’s picture

Crossed in the mail, checking next patch...

Todd Young’s picture

S U C C E S S - Very nice.....

Going to goof around with it some more to be sure.

quicksketch’s picture

FWIW, if I comment out your NULL check it works!

After some more research, I found that Node Reference does NOT confirm that a reference is valid upon submission unless you're using the Autocomplete widget. This means that Node Reference URL Widget must do its own confirmation somewhere, so it's rather important that we keep this validation, otherwise you'll be able to reference any node just by changing the URL.

S U C C E S S - Very nice.....

Hurray! :-)

Todd Young’s picture

Yes, it is looking very good. I can't analyze whether other problems (security, etc) might arise from your patch, but this performs perfectly. You, sir, nailed it.

Thank you!

quicksketch’s picture

Excellent, I'll do a bit of validation testing and I'll probably put this as-is into the next release.

quicksketch’s picture

Status: Needs review » Fixed

I checked it out and everything seems to be functioning exactly like before, just in a lot more efficient manner. Committed and I'll roll the 1.6 version shortly.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.