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.

Comments

dsnopek’s picture

Version: 7.x-2.0-beta6 » 6.x-2.x-dev

Ooops! Set the wrong version.

socketwench’s picture

Testbot? What say you?

Status: Needs review » Needs work

The last submitted patch, flag-view-index-6.x-2.x.patch, failed testing.

joachim’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev
Category: task » feature

Also, new features need to go on the newest branch first.

joachim’s picture

Issue tags: +Novice

Tagging.

snufkin’s picture

StatusFileSize
new865 bytes

Rerolled the patch for D7, tested with fresh install and update as well.

snufkin’s picture

Status: Needs work » Needs review
joachim’s picture

Thanks! 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.

joachim’s picture

Status: Needs review » Needs work

Ah, 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.

snufkin’s picture

Status: Needs work » Needs review
StatusFileSize
new824 bytes

Hmm I was sure i changed that, probably forgot to reroll the patch.

joachim’s picture

Status: Needs review » Fixed

Thanks!

Committed to both 7 branches.

Issue #1650512 by dsnopek, snufkin: Added index to {flag_content} to improve Views performance.

joachim’s picture

Version: 7.x-2.x-dev » 6.x-2.x-dev
Status: Fixed » Patch (to be ported)

Setting back to 6-2 branch. The patches on the original post look like they may need rerolling.

jackbravo’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new873 bytes

here's a re-roll of the patch. I couldn't stop myself from participating just after my "tocayo" Joachim =P.

Status: Needs review » Needs work

The last submitted patch, flag-view-index-6.x-2.x-13.patch, failed testing.

jackbravo’s picture

Status: Needs work » Needs review
StatusFileSize
new873 bytes

Oups!!!!

wrong function name. Sorry.

dsnopek’s picture

Thanks 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!

socketwench’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, tests clean. Committed! http://drupalcode.org/project/flag.git/commit/23d8bde

socketwench’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

I suspect we need to port this to the D7 branch too.

joachim’s picture

It's already on D7 -- did you mean 6.x-1.x?

socketwench’s picture

Status: Patch (to be ported) » Fixed

Ahhh, 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?

dsnopek’s picture

It'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.

joachim’s picture

> 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.

socketwench’s picture

Good point. I'll work on that.

snufkin’s picture

Would 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.

socketwench’s picture

Status: Fixed » Needs review
StatusFileSize
new962 bytes

Here you go.

Status: Needs review » Needs work

The last submitted patch, flag-view-index-6.x-1.x-24.patch, failed testing.

socketwench’s picture

Odd. It applies locally, and I did a pull before modifying the code...

socketwench’s picture

Version: 6.x-2.x-dev » 6.x-1.x-dev
Status: Needs work » Needs review
socketwench’s picture

#25: flag-view-index-6.x-1.x-24.patch queued for re-testing.

socketwench’s picture

Ahhhhh. I needed to re-tag the issue as 6.x-1.x. Now it tests clean.

joachim’s picture

Version: 6.x-1.x-dev » 7.x-2.x-dev
Status: Needs review » Needs work

I 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.

socketwench’s picture

I thought we were saying we wouldn't bother backporting this to 6-1 as it adds update functions?

Ahhh, yeah, you're right. I missed your reply. My bad. This is what I get for doing this late at night...

joachim’s picture

Status: Needs work » Fixed
StatusFileSize
new569 bytes

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