Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Followup from: #2073033: Optimize file usage updates in file/image fields (comments #14 and #15).
We want to figure out if files usage mechanism can be optimized. There are two proposals that needs investigation first:
- Add
addMultiple()
anddeleteMultiple()
to\Drupal\file\FileUsage\FileUsageInterface
. Those new methods should accept a list of files as first argument and same other arguments. Then, inDatabaseFileUsageBackend
, those methods should use optimized SQL statements like multi-record INSERTs. Basically we want to run a single multi-row SQL query instead of running a single-row query multiple times when updating the usage for more than one file once. - Investigate the possibility to replace the first argument of
::add()
and::delete()
(+multiple) methods with FIDs instead of full file objects. This is applicable also for[add|delete]Multiple()
new methods.
Comments
Comment #1
claudiu.cristeaTagging.
Comment #2
BerdirThe full objects are needed because file usage also controls the file status.
Comment #3
yched CreditAttribution: yched commented@Berdir: yes, it just seems weird that acting on the status is the job of FileUsage ?
But yes, that might be a bit much to refactor here.
Comment #4
BerdirThat's how it used to be, but that makes using this service so much more complicated to use, see https://api.drupal.org/api/drupal/modules%21file%21file.field.inc/functi....
And it doesn't change much. We still have to do this temporary/permanent thing, it would just be moved to the code that calls the API.
Comment #5
yched CreditAttribution: yched commentedAgreed, nevermind. Moving the methods to multiple would be good enough here.
Comment #6
claudiu.cristeaTaking this.
Comment #7
claudiu.cristeaAnd postponing till #2073033: Optimize file usage updates in file/image fields as it has to act on FileField rather than FileItem.
Comment #8
BerdirAre we sure that multiple is actually going to be faster? I guess the only improvement would be a single select to get the current usages, but then we need separate updates/deletes/inserts/file saves anyway?
Not sure if it's really worth an API addition...
Comment #9
claudiu.cristeaThis is the first improvement.
On an
addMultiple()
there will be no more than 3 queries, at least 2:For an entity ref field with 3 items the existing merge query will perform 6 queries, while this patch will perform 2 or 3 queries.
Here's a first idea of
addMultiple()
:Also I didn't dropped totally the idea of passing FIDs instead file objects. That is really absurd what is going on now. Running an update query for each file (I mean $file->setPermanent()) when we can do that in a single cheap query. I'm thinking that FileStorageController can implement bulk status set for files while it's really a storage controller job (not the model should implement that).
Comment #10
claudiu.cristeaNeeds profiling
Comment #11
mgiffordComment #18
kim.pepperClosing as outdated. Please re-open if you feel this is still relevant.