I noticed the message table is not indexed by anything other than mid (foreign key on uid and message_type, but no indexes).
Because we're often going to want to fetch messages for a given user, or fetch messages by age (which will not necessarily correlate with mid) it makes sense to index the message table by uid and by timestamp as well.
This falls in line with Drupal best practices, as for instance, the node table indexes on uid and node created time (actually, the node table indexes on every column). Makes sense to index by message_type as well.
I would say this should be a major priority from a performance standpoint.
Comment | File | Size | Author |
---|---|---|---|
#7 | Screenshot 12:11:12 9:45 PM.png | 11.55 KB | amitaibu |
#4 | message_add_timestamp_index.patch | 653 bytes | msonnabaum |
Comments
Comment #1
amitaibuPatch is welcome, and a benchmark even more ;)
Comment #2
JordanMagnuson CreditAttribution: JordanMagnuson commentedSure, I'll put this on my todo.
I agree that benchmarks would be nice, though considering that this is pretty basic sql query stuff, I think something would have to be seriously broken for an index on uid not to improve fetching rows by uid...
Comment #3
amitaibuI agree. Well, then at least a patch ;)
Comment #4
msonnabaum CreditAttribution: msonnabaum commentedIn the interest of only adding indexes that we know we're running into issues with, here's a patch that adds one to the timestamp column.
I ran into this in Commons, where we have a query like this:
Before this patch:
After:
Query on my local machine goes from about 250ms to 0.4ms.
Comment #5
msonnabaum CreditAttribution: msonnabaum commentedComment #6
ezra-g CreditAttribution: ezra-g commentedRe-titling and tagging.
Comment #7
amitaibuAfter applying patch and using a query that has WHERE on the Message-type (a typical use case), mysql still doesn't use the index key
EXPLAIN SELECT message.mid AS mid, message.timestamp AS message_timestamp FROM message message WHERE message.type = "example_create_node" ORDER BY message_timestamp DESC LIMIT 5 OFFSET 0;
Should we add an index for that as-well?
Comment #8
msonnabaum CreditAttribution: msonnabaum commentedInteresting. That query uses the timestamp index for me, but that's probably just a difference between envs (I'm running MariaDB 5.5 locally).
And yeah, we probably should add an index to type as well.
Comment #9
amitaibuAdd index to "type" as-well and committed, thanks.