I have a site with about 125,000 items flagged. We're using flags for A LOT of different things.
Using a view that has a relationship to the flag_content table, that isn't marked as required, was really slow. The problem query was returning in about 2 seconds. When marking as required, it returned very quickly (INNER JOIN vs. LEFT JOIN). However, we need to optionally find items that aren't flagged with a particular flag so INNER JOIN wasn't an option.
The JOIN to the table 'flag_content' table was on the 'content_id' and 'fid' columns.
Using EXPLAIN on MySQL, I saw that it was using the 'content_type_content_id_uid_sid' index for the JOIN, but that only includes the 'content_id', so MySQL was forced to examine thousands of additional rows (as I could see from the 'rows' column in the EXPLAIN).
I added a new index which includes 'content_id' and 'fid' and now the query returns in less than 0.1 seconds!
Attached is a patch that adds this index, including the hook_update_X() function to add it. I made two patches, one for 6.x-2.x and one for 6.x-1.x (we're actually still using 1.x).
However, I don't know how the patches will interact. Since if you go through this upgrade in 1.x and then later upgrade to 2.x, it will try to add the index again. I don't know exactly how to deal with that... Any ideas?
Please let me know what you think!
Regards,
David.
| Comment | File | Size | Author |
|---|---|---|---|
| #33 | 1650512.32.flag_.follow-up-index-exists.patch | 569 bytes | joachim |
| #25 | flag-view-index-6.x-1.x-24.patch | 962 bytes | socketwench |
| #15 | flag-view-index-6.x-2.x-14.patch | 873 bytes | jackbravo |
| #13 | flag-view-index-6.x-2.x-13.patch | 873 bytes | jackbravo |
| #10 | 1650512-flag-indexing.patch | 824 bytes | snufkin |
Comments
Comment #1
dsnopekOoops! Set the wrong version.
Comment #2
socketwench commentedTestbot? What say you?
Comment #4
joachim commentedAlso, new features need to go on the newest branch first.
Comment #5
joachim commentedTagging.
Comment #6
snufkin commentedRerolled the patch for D7, tested with fresh install and update as well.
Comment #7
snufkin commentedComment #8
joachim commentedThanks! Will commit after lunch.
Just a note that I don't think we need a 73xx update for this on the 3.x branch, as that's still in dev and nobody should be installing it on anything other than a test setup.
Comment #9
joachim commentedAh, http://api.drupal.org/api/drupal/modules!system!system.api.php/function/... has changed on D7: the $ret return is gone and there's a parameter now.
Comment #10
snufkin commentedHmm I was sure i changed that, probably forgot to reroll the patch.
Comment #11
joachim commentedThanks!
Committed to both 7 branches.
Issue #1650512 by dsnopek, snufkin: Added index to {flag_content} to improve Views performance.
Comment #12
joachim commentedSetting back to 6-2 branch. The patches on the original post look like they may need rerolling.
Comment #13
jackbravo commentedhere's a re-roll of the patch. I couldn't stop myself from participating just after my "tocayo" Joachim =P.
Comment #15
jackbravo commentedOups!!!!
wrong function name. Sorry.
Comment #16
dsnopekThanks guys for diving into this! It looks like you're already on your way to getting patches into all versions. Please let me know if there is any way that I can help out!
Comment #17
socketwench commentedLooks good, tests clean. Committed! http://drupalcode.org/project/flag.git/commit/23d8bde
Comment #18
socketwench commentedI suspect we need to port this to the D7 branch too.
Comment #19
joachim commentedIt's already on D7 -- did you mean 6.x-1.x?
Comment #20
socketwench commentedAhhh, no I meant 7.x-2.x/3.x. Since it's in there already, marking as fixed.
I thought we weren't touching 6.x-1.x?
Comment #21
dsnopekIt'd be great to get on 6.x-1.x because I do have some sites there! And it's a simple change.
But I suppose I should upgrade them. :-) I know that the "beta" tag doesn't really apply to 6.x-2.x, the code is very stable.
Comment #22
joachim commented> I thought we weren't touching 6.x-1.x?
If a commit on 6-2 cherry-picks cleanly to 6-1, I'll take it. For now....
In this case though, it's an update function and a whole malarkey, so no.
There's a dozen or so commits on the 6-1 branch since the last release, so we should make one last stable release.
Comment #23
socketwench commentedGood point. I'll work on that.
Comment #24
snufkin commentedWould the fact that the update hooks are different now cause an issue? e.g. if someone updates from 6.x-2.x to 7.x-3.x and already had the index due to this patch, the update hook will fail on 7.x.
Comment #25
socketwench commentedHere you go.
Comment #27
socketwench commentedOdd. It applies locally, and I did a pull before modifying the code...
Comment #28
socketwench commentedComment #29
socketwench commented#25: flag-view-index-6.x-1.x-24.patch queued for re-testing.
Comment #30
socketwench commentedAhhhhh. I needed to re-tag the issue as 6.x-1.x. Now it tests clean.
Comment #31
joachim commentedI thought we were saying we wouldn't bother backporting this to 6-1 as it adds update functions?
snufkin's point is about the flag_update_7203 -- it needs some sort if (if !index exists) wrapped around it.
Comment #32
socketwench commentedAhhh, yeah, you're right. I missed your reply. My bad. This is what I get for doing this late at night...
Comment #33
joachim commentedCommitted this follow-up fix, based on http://api.drupal.org/api/drupal/modules!user!user.install/function/user....