There's a certain performance issue with the "Ops" field (in Views). I was aware of this from the start, but didn't bother to bring this up.
If you look in 'flag_handler_field_ops.inc' you'll see that its query() method adds a JOIN to the query. The reason is explained in the "Find out if the content is flagged [...]" comment.
And since there's already the main JOIN, we end up with *two* JOINs when the "Ops" field is shown in a view.
The comment I mentioned should be more explicit:
The reason for this extra JOIN is for cases when the flag relationship is added with a "user scope" of "Any user". In this case the "Ops" field handler can't know, just by examining the available columns, whether the item is flagged by the currect user. So it effectively does an extra JOIN with a "user scope" of "Current user".
It's easy to rid of this extra JOIN. This should only be done when the "user scope" of the relationship is "Current user" (and this probably happens in 90% of the cases when we also show the "Ops" field). The query() method should see if its relationship has its 'user_scope' option set to 'current'. If it does, it should skip adding a JOIN and instead have the two aliases ('is_flagged' and 'content_id') point to the columns of the current table.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | 404150.flag_.views-flagging-double-join.patch | 2.97 KB | joachim |
| #9 | flag-404150-views-field-ops-performance-9.patch | 3.38 KB | q0rban |
Comments
Comment #1
mooffie commentedA different issue:
It's possible to add yet another performance enhancement to this "Ops" field:
In flag_handler_field_ops::pre_render() we're doing a query to find out which items are flaggable. We can add an "Assume all items are flaggable" checkbox to the Ops configuration form. This will allow the admin to skip this query.
But this is a minor (negligible, probably) preformance issue because this query is a simple one which doesn't affect the [potentially complex] main one.
Comment #2
mitchell commentedMoving
Comment #3
quicksketchA related support request: #728556: Flag_content table joined 2 times.
Comment #4
mooffie commentedComment #5
gregglesMarked #1162578: Slow view caused by joining to flag_content twice as a duplicate.
Comment #6
crea commentedSubscribing
Comment #7
quicksketchUpdating title for clarity.
Comment #8
q0rban commentedI'm running into an issue with this not mentioned in the first post. If you have multiple flag ops fields on a view, the join happens multiple times. Normally this would never happen, but when you are using something like flag_form, you can actually "edit" a flag, as well as unflag/flag.
Comment #9
q0rban commentedAttached patch fixes both my issue in #8, and bypasses the extra join when the user_scope option is set to "Current User" on the relationship.
Comment #10
IWasBornToWin commentedUnless I am doing something wrong I'm not able to apply this patch. Error: Patch cannot be applied to selected content.
Comment #11
joachim commentedPatch applies, but I'm still getting two joins:
This is the bit I'm suspicious of. Table aliases are tricksy things, and I don't think you can necessarily rely on them like that.
Comment #12
joachim commentedComment #13
joachim commentedI've looked at this again with a clearer head, and now I get it :) The table alias gets a flag ID appended to it to guarantee uniqueness: 'flag_content_current_user_' . $flag->fid.
Patch looks good, and very well-documented too.
Here's a reroll for the DB changes in 3.x.
Comment #14
joachim commentedIssue #404150 by q0rban: Fixed unnecessary re-adding of table to views showing the 'ops' field with the 'current user' limit on the flag relationship.
Comment #15
joachim commentedThe patch at #9 should be ok to apply to 2.x -- I don't think I changed anything like comments in my reroll.
Comment #16
IWasBornToWin commentedIf i install the latest dev do I need to change anything on my end? I have several views joined with flag and want to make sure I can simply upgrade to latest dev.
Thanks
Comment #17
joachim commentedI'm not sure what you mean exactly -- and at any rate, please could you post a support request rather than take an issue off-topic?
Comment #18
IWasBornToWin commentedMaybe I wasn't clear enough, you said:
This leads me to believe I can upgrade to avoid the problems of this issue, but I do not want to upgrade if it will cause headaches with all my current views joined to flags.
Comment #19
joachim commentedComment 14 tells you that this issue was fixed on 3.x, because of the way the status was changed.
However, I do not recommend switching production sites to 3.x yet. If you want this fix on 2.x please help test the patch.
Comment #21
ivnishClosed as outdated because Drupal 7 is EOL