Backend: Wrap SQL select for MySQL 8 #599

In MySQL, you can't modify the same table which you use in the
SELECT part (DELETE and UPDATE with subquery):
https://dev.mysql.com/doc/refman/8.0/en/update.html

We need to verify how this affects large photo libraries
as it probably "breaks" the query optimizer. So might lead to
bad performance as well as high CPU and/or memory usage.
This commit is contained in:
Michael Mayer 2020-11-15 12:15:14 +01:00
parent e22e6c6d37
commit 2bad5b3cdd
2 changed files with 2 additions and 2 deletions

View file

@ -143,7 +143,7 @@ func IndexedFiles() (result FileMap, err error) {
// CleanDuplicates removes all files from the duplicates table that don't exist in the files table.
func CleanDuplicates() error {
if res := UnscopedDb().Delete(entity.Duplicate{}, "file_hash IN (SELECT d.file_hash FROM duplicates d LEFT JOIN files f ON d.file_hash = f.file_hash AND f.file_missing = 0 AND f.deleted_at IS NULL WHERE f.file_hash IS NULL)"); res.Error != nil {
if res := UnscopedDb().Delete(entity.Duplicate{}, "file_hash IN (SELECT file_hash FROM (SELECT d.file_hash FROM duplicates d LEFT JOIN files f ON d.file_hash = f.file_hash AND f.file_missing = 0 AND f.deleted_at IS NULL WHERE f.file_hash IS NULL) AS tmp)"); res.Error != nil {
return res.Error
}

View file

@ -86,7 +86,7 @@ func PhotosMissing(limit int, offset int) (entities entity.Photos, err error) {
// ResetPhotoQuality resets the quality of photos without primary file to -1.
func ResetPhotoQuality() error {
return Db().Table("photos").
Where("id IN (SELECT photos.id FROM photos LEFT JOIN files ON photos.id = files.photo_id AND files.file_primary = 1 WHERE files.id IS NULL GROUP BY photos.id)").
Where("id IN (SELECT id FROM (SELECT photos.id FROM photos LEFT JOIN files ON photos.id = files.photo_id AND files.file_primary = 1 WHERE files.id IS NULL GROUP BY photos.id) AS tmp)").
Update("photo_quality", -1).Error
}