The flagging table is missing some indexes, especially when you have large set of data, in my case, 2.3 million flagged items. Following query goes extremely slow, around 1.2 seconds.

SELECT f.entity_id AS entity_id, f.flag_id AS flag_id, f.global AS global FROM flagging f WHERE (entity_type = 'user') AND (entity_id = '1') AND( (global = '1') OR (uid = '1') )

When you run explain on this query, the possible rows are, well, all, which is 2,3 million ..

After adding an index with two columns on entity_id and uid, the query time drops to 0.00008 seconds and the possible rows in the explain are now 178.

Patch upcoming.

CommentFileSizeAuthor
#2 2835895-2.patch1.34 KBswentel

Comments

swentel created an issue. See original summary.

swentel’s picture

Status: Active » Needs review
StatusFileSize
new1.34 KB

drush entup will pickup the new indexes.

Maybe we need more, but this one is a good start.

berdir’s picture

Did you leave out entity_type on purpose? I can see that this might make sense as most sits only have 1,2 or at most a handful of different values there and overlaps on the entity ID should be very rare..

Maybe a comment for this would make sense.

swentel’s picture

Not on purpose. I was doing some tests and I was already glad that a 50 second page load dropped to 1 second ... ;)

I remember adding it as a separate index, but I don't know anymore if it made much difference (probably won't). I'll do a test with adding as an extra column on the index in this patch, however, as you said, even that won't matter that much in most sites I think.

berdir’s picture

MySQL can only use a single index per query so yes, a second index won't help, and an additional column is probably not worth it and just makes the index bigger. Would only be needed if we'd make it a unique index, but hten we'd have to include the flag type too.

joachim’s picture

Status: Needs review » Needs work

This is a good start, but for global flags, the user ID is not considered in the query, surely?

berdir’s picture

Status: Needs work » Needs review

That doesn't matter, as long as the uid is second, the index will also work if we just join on entity_id.

joachim’s picture

Thanks for the explanation -- wasn't thinking it through clearly :/

Patch looks good as code, but does this need further review for the performance aspect, ideally from someone else with a site that has large amount of data?

berdir’s picture

Status: Needs review » Reviewed & tested by the community

I think this is fine as a starting point. We can always add more indexes, having too many that don't actually help is not a good idea and it makes sense to me that this is better and @swentel provided numbers.

  • joachim committed ab6913a on 8.x-4.x authored by swentel
    Issue #2835895 by swentel: Added index to the flagging table.
    
joachim’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone!

Committed.

Status: Fixed » Closed (fixed)

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