Problem/Motivation

Right now, every time we save an entity, we delete all rows in the data and revision data tables and insert them again.

However, many fields never change and especially when working with translations, in the 90% (at least) use case, only one translation actually changes.

The only exception is saving a new revision in the revision data table, then we just insert.

Proposed resolution

Instead, do the following:

1. If this is a new entity or a new revision for the revision data table, just insert.
2. If there are any removed languages, delete those rows.
3. Then loop over the still-existing languages
3a) The language is new, just insert
3b) The language existed before. Map the loaded entity, get the differences and do an update query just for those, if there are changes.

I don't know too much about DB internals, but I'd expect this to be *considerably* faster in many scenarios. Will run some tests soon.

Example with adding a new translation:
HEAD: DELETE, N insert queries (N = total amount of languages)
Patch: 1 insert query (existing translations did not change)

Similar with e.g. updating one translation out of 5, then on HEAD, we do a DELETE and then 5 inserts, with this, we only need to do a single update query for the fields that really changed.

There's a certain amount of overhead, due to loading the revision, due to having to map it, but I would assume this is still faster in almost all cases.

This also explicitly uses the new loaded revision ID insted of $entity->original. I'm pretty sure we have some very nasty bugs related to that and conditional field saving. Need to open an issue.

Remaining tasks

* Apply the same to base and revision tables?
* Avoid loading the loaded revision multiple times. Store it or pass it around somehow.
* Profile/Benchmark this.
* Fix tests/code.

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir created an issue. See original summary.

Berdir’s picture

Status: Active » Needs review
FileSize
3.44 KB
Berdir’s picture

I did some profiling on this, but I'm not quite seeing the results I was hoping for. One reason is that mapToStorageRecord() is expensive.

With a single language, blackfire.io causes enough overhead to make it considerably slower. With just some manual microtime() comparisons, it seems to be slightly faster with the attached optimzation (aka, when saving the default revision).

With multiple translations, it gets a bit faster, but still not as much as I was hoping.

Status: Needs review » Needs work

The last submitted patch, 3: entity-optimize-shared-table-save-2875861-3.patch, failed testing.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

JacobSanford’s picture

@Berdir, your patch in #3 no longer applied to 8.6.x. A reroll with no further modifications is attached.

Interdiff empty due to reroll.

Regards!

matsbla’s picture

Issue tags: +Performance
hchonov’s picture

I did some profiling on this, but I'm not quite seeing the results I was hoping for. One reason is that mapToStorageRecord() is expensive.

I guess we'll not have to use mapToStorageRecord() once #2862574: Add ability to track an entity object's dirty fields (and see if it has changed) is ready and then we'll see the results you were hoping for :).

Berdir’s picture

Yes, that might help. We still have to use it, but only once and that one call can be optimized to only look at changed fields from the start :)

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.