Improving loggable extension querying performance #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The issue is that the query got by the getLogEntriesQuery method don’t use the correct MySQL index therefore the performance is very low when the ext_log_entries has a several millions of rows.
The problem is that the “object_id” field in ext_log_entries table is a varchar(64) type field.
The table has the next indexes:
getLogEntriesQuery method queries entries by objectId, objectClass and ordered by version. The right index should be log_version_lookup_idx but mysql always use log_class_lookup_idx even thought you force the log_version_lookup_idx index. This happens because objectId is stored as a varchar field and then is queried using an integer value. As an example:
where log.objectId = 1 AND log.objectClass = “AppBundle\Entity\FooClass” ORDER BY log.version DESC -> this uses log_class_lookup_idx index
where log.objectId = “1” AND log.objectClass = “AppBundle\Entity\FooClass” ORDER BY log.version DESC -> this uses log_version_lookup_idx index
In my case, I have 35 million rows in ext_log_entries table. When I perform a query with log_class_lookup_idx index, it takes several minutes!!! (a lot of time). But the same query with the log_version_lookup_idx takes a few milliseconds (great).
I propose, change the line 57 and 89 on Gedmo/Loggable/Entity/Repository/LogEntryRepository.php
Original: $objectId = $wrapped->getIdentifier();
To: $objectId = (string) $wrapped->getIdentifier();
By this way, Mysql will use log_version_lookup_idx index and it will improve performance in a big way.
Thanks a lot.