Problem/Motivation
We have been seeing slow response when viewing orders in a large commerce site via /admin/commerce/orders/{id} with the commerce_log module enabled. Having analysed the issue, it has been attributed to the query that selects the log entries for the order entity using the source entity ID. As there is no index on this field, the query is inefficient and with a modest number of concurrent admin users viewing orders, database resources are quickly exhausted and the site fails.
Proposed resolution
Add an index to the field when the module is enabled.
Provide an update hook for existing installs.
Remaining tasks
Currently working on a patch
Comments
Comment #2
alanhdev commentedAdding a patch to add an index to the source_entity_id field in the commerce_log table.
Provides the index for new installs and adds an update hook to fix existing installs.
Comment #3
alanhdev commentedComment #4
bojanz commentedGood catch!
One question:
Wouldn't you want the index to cover both source_entity_id and source_entity_type, since they're always used in tandem?
Something like this:
Comment #5
alanhdev commentedI'd say yes to that in theory, but in practice the source_entity_type is always going to be 'commerce_order' so it won't offer an improvement, just make the index longer - and we have something like 7 million entries in that table.
Comment #6
bojanz commentedThat is true only for now. We have an issue for adding logging to payments: #2845321: Add payment logging to orders.
There is no guarantee that sites aren't already using the API for other entity types. For those the index won't be completely precise, because IDs might be shared between entity types.
So i'd prefer #4 if we can prove that it doesn't cause problems for your site.
Comment #7
alanhdev commentedNo probs. I'll re-roll the patch.
Comment #8
alanhdev commentedRe-rolled the patch to add index on source_entity_id and source_entity_type.
The module could do with some attention to the field lengths. The source_entity_type ends up as default 255 after installation so I've restricted the index size on that field to 32.
Comment #9
alanhdev commentedComment #10
travis-bradbury commentedGiven 330,000 orders and 2,250,000 log entries, queries on commerce_log were often taking over 500ms. With the index this patch provides, it became reasonably fast.
Before:
After:
Comment #11
jsacksick commentedThe patch looks good to me, though I was sure this was covered by #2907367: Add indexes to important fields for some reason.
I wonder if we could reuse our CommerceContentEntityStorageSchema introduced there. Curious about the 32 limitation on the index, won't that be potentially an issue, considering the column itself doesn't have a 32 character limit?
The update hook should probably take care of updating the entity type definition as well.
Comment #12
jsacksick commentedOk you're right, the "entity_type" shouldn't be longer than 32 characters, but I believe we should restrict the column/field itself which removes the need for a limit on the index itself, going to work on a patch that does that and see if I can reuse CommerceContentEntityStorageSchema.
Comment #13
rszrama commentedRevising the metadata, preparing for the 2.25 release.
Comment #14
jsacksick commentedOk, so changing the maximum length of an existing base field was more complex than I thought, it took me several attempts to get it right and to get rid of the warnings on the status report page after the update, but I think I got it right this time.
Comment #15
travis-bradbury commentedLooks good to me. I didn't know about the entity type length limit, but fixing it in the table instead of having an index that doesn't match the field makes a lot more sense.
Here's a before and after the patch, showing it's as fast as expected after, same as in #10.
Comment #17
jsacksick commentedCommitted! Thanks for your feedback with testing! I also tried applying the patch myself on an existing project as well to ensure there weren't errors reported in the status report page and it all looked ok!