DoctrineExtensions icon indicating copy to clipboard operation
DoctrineExtensions copied to clipboard

Improving loggable extension querying performance

Open adagber opened this issue 10 years ago • 2 comments

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:

  • PRIMARY (id)
  • log_class_lookup_idx (object_class)
  • log_date_lookup_idx (logged_at)
  • log_user_lookup_idx (username)
  • log_version_lookup_idx (object_id, object_class, version)

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.

adagber avatar Aug 14 '15 07:08 adagber

you should PR to https://github.com/Atlantic18/DoctrineExtensions regarding mapping, you can extend the loggable mapped supperclass instead and modify the annotation for objectId to be integer instead. Although, loggable is not that usable, check a specific impl audit bundle

l3pp4rd avatar Aug 14 '15 09:08 l3pp4rd

Hi, How can I PR to https://github.com/Atlantic18/DoctrineExtensions from my current fork?

adagber avatar Sep 01 '15 08:09 adagber