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.
Comments
Comment #2
swentel commenteddrush entup will pickup the new indexes.
Maybe we need more, but this one is a good start.
Comment #3
berdirDid 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.
Comment #4
swentel commentedNot 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.
Comment #5
berdirMySQL 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.
Comment #6
joachim commentedThis is a good start, but for global flags, the user ID is not considered in the query, surely?
Comment #7
berdirThat doesn't matter, as long as the uid is second, the index will also work if we just join on entity_id.
Comment #8
joachim commentedThanks 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?
Comment #9
berdirI 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.
Comment #11
joachim commentedThanks everyone!
Committed.