Bug 470117

Summary: Digikam query is slow on MariaDB while assigning detected Person
Product: [Applications] digikam Reporter: timmib
Component: Database-MysqlAssignee: Digikam Developers <digikam-bugs-null>
Status: REPORTED ---    
Severity: normal CC: caulier.gilles, metzpinguin
Priority: NOR    
Version First Reported In: 8.0.0   
Target Milestone: ---   
Platform: Ubuntu   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description timmib 2023-05-22 11:05:05 UTC
SUMMARY
When i accept a bunch of face-suggestions to a Person the update process is pretty slow. (~1s per Suggestion)

I detected two queries in mariaDB which might cause the problem.

SELECT  DISTINCT 
	Images.id, Images.name, Images.album, Albums.albumRoot, ImageInformation.rating, Images.category, ImageInformation.format, ImageInformation.creationDate, Images.modificationDate, Images.fileSize, ImageInformation.width, ImageInformation.height, ImageTagProperties.value, ImageTagProperties.property, ImageTagProperties.tagid 
	FROM Images 
	INNER JOIN ImageTagProperties ON ImageTagProperties.imageid=Images.id 
	LEFT JOIN ImageInformation ON Images.id=ImageInformation.imageid 
	INNER JOIN Albums ON Albums.id=Images.album 
	WHERE Images.status=1 AND ( ( ( (ImageTagProperties.tagid=30 OR ImageTagProperties.tagid IN (
		SELECT id FROM TagsTree WHERE pid=30)) AND ImageTagProperties.property='autodetectedPerson' ) OR ( (ImageTagProperties.tagid=30 OR ImageTagProperties.tagid IN (
		SELECT id FROM TagsTree WHERE pid=30)) AND ImageTagProperties.property='autodetectedFace' ) OR ( (ImageTagProperties.tagid=30 OR ImageTagProperties.tagid IN (
		SELECT id FROM TagsTree WHERE pid=30)) AND ImageTagProperties.property='ignoredFace' ) OR ( (ImageTagProperties.tagid=30 OR ImageTagProperties.tagid IN (
		SELECT id FROM TagsTree WHERE pid=30)) AND ImageTagProperties.property='tagRegion' ) ) ) LIMIT 10

and

SELECT  tagid, COUNT(*) FROM ImageTagProperties LEFT JOIN Images ON Images.id=ImageTagProperties.imageid WHERE ImageTagProperties.property='autodetectedFace' AND Images.status=1 	GROUP BY tagid


STEPS TO REPRODUCE
1. Import 130k pictures
2. Detect faces
3. Recognize faces
4. Select 10-20 suggested faces 
5. Wait for database to finish

OBSERVED RESULT
Slow processing of database update.

EXPECTED RESULT
10x faster processing, like locally on sqllite.

SOFTWARE/OS VERSIONS
ubuntu desktop and server 22.04
MariaDB 10.6

ADDITIONAL INFORMATION
Comment 1 timmib 2023-05-22 11:06:33 UTC
I did a lot of standard optimiziation to MaraDB. Like Query-Cache, buffer sizes and so on.
Comment 2 Maik Qualmann 2023-05-22 18:32:39 UTC
Git commit f3b0cc522a4eefd1e988ddc0872ca5f2831a2386 by Maik Qualmann.
Committed on 22/05/2023 at 18:31.
Pushed by mqualmann into branch 'master'.

use another query to check if a face tag still exists

M  +2    -3    core/utilities/facemanagement/database/faceutils.cpp

https://invent.kde.org/graphics/digikam/commit/f3b0cc522a4eefd1e988ddc0872ca5f2831a2386
Comment 3 Maik Qualmann 2023-05-23 06:10:44 UTC
The first query (where you have add a LIMIT of 10) is used to list the face tags. There is currently no optimization option here. You have activated the recursive listing of tags, which costs additional time since the TagsTree is also included.
We could just disable the view refresh when multiple face tags are confirmed to speed it up. But that would have other side effects.

The second query, I think, will be significantly accelerated, it is used to remove the thumbnail if the face tags are no longer available.

And yes, SQLite on a local SSD is significantly faster than MySQL. Only an internal MySQL on SSD connected via a socket is then faster than SQLite.

Maik
Comment 4 caulier.gilles 2023-05-23 06:25:33 UTC
Online doc about the database criteria:

https://docs.digikam.org/fr/setup_application/database_settings.html#database-type-criteria
Comment 5 timmib 2023-05-23 06:45:00 UTC
Thanks a lot for the fast and accurate reaction and feedback.

>>You have activated the recursive listing of tags.
Where can i deactivate this?

Is there a (nightly) build which i can use to try out your changes?
Comment 6 caulier.gilles 2023-05-23 06:53:59 UTC
The automatic nightly build are mostly broken since a while on the KDE infra, due to the current migration to gitlab infra.

We (i personally)  build mine on my computers at home. I will start a new build the evening (Paris time). Files will be available at this url:

https://files.kde.org/digikam/

Gilles Caulier
Comment 7 timmib 2023-05-23 07:11:50 UTC
Thanks a lot. I will try the build in the evening. 

In the meantime i can spend some time optimizing the mariaDB config :o)
Comment 8 timmib 2023-05-23 08:59:43 UTC
Btw. it is actually much faster when you select another person while updating. (digikam 8.0.0)
Comment 9 caulier.gilles 2023-05-23 16:10:02 UTC
Done. AppImage bundle  pre-release is online:

https://files.kde.org/digikam/digiKam-8.1.0-20230523T152806-x86-64.appimage.mirrorlist

Gilles Caulier
Comment 10 timmib 2023-05-23 16:54:54 UTC
Sorry, to say but there is no improvement at all. I measured before ~22 secondes for accepting 10 suggestions. And now it is the same +-1s.

Measured from clicking the "checkmark"-Button unti the progress-bar disapears after 100% completion.
Comment 11 Maik Qualmann 2023-05-23 17:39:03 UTC
In Menu-> View disable the option to show recursive tags.
Do you write the metadata in the images?
Have you enabled ExifTool to write metadata?

Maik
Comment 12 timmib 2023-05-23 18:45:57 UTC
>>In Menu-> View disable the option to show recursive tags.
This actually helped a lot. Now i am down to  ~8s for 10 for accepting 10 suggestions.

>>Do you write the metadata in the images?
Nope.

>>Have you enabled ExifTool to write metadata?
Nope. My settings look like the screenshot in the documenation https://docs.digikam.org/en/setup_application/metadata_settings.html
Comment 13 Maik Qualmann 2023-05-25 06:26:53 UTC
Git commit 788db1ecaeef516107c27c5c76cd182d2de56508 by Maik Qualmann.
Committed on 25/05/2023 at 06:25.
Pushed by mqualmann into branch 'master'.

remove unused refresh function and adjust timer time

M  +4    -36   core/libs/models/itemalbummodel.cpp
M  +1    -4    core/libs/models/itemalbummodel.h

https://invent.kde.org/graphics/digikam/-/commit/788db1ecaeef516107c27c5c76cd182d2de56508
Comment 14 Maik Qualmann 2023-05-28 19:25:25 UTC
Git commit 0395ddd9b8e1518368e9dca3148f9d06a00510b4 by Maik Qualmann.
Committed on 28/05/2023 at 19:24.
Pushed by mqualmann into branch 'master'.

further optimization of the album item  model refresh

M  +21   -4    core/libs/models/itemalbummodel.cpp

https://invent.kde.org/graphics/digikam/-/commit/0395ddd9b8e1518368e9dca3148f9d06a00510b4
Comment 15 caulier.gilles 2023-10-11 05:05:11 UTC
Timmib,

What's about this file using current 8.2.0 AppImage Linux bundle ? It's
reproducible ?

https://files.kde.org/digikam/

Thanks in advance

Gilles Caulier
Comment 16 caulier.gilles 2025-04-12 19:49:52 UTC
timmib,

The digiKam 8.7.0 pre-release AppImage bundle for Linux have been rebuild from scratch
with many improvements and updates especially with the face management
workflow. Please test with this version to see if the problem is reproducible.

Link to download: https://files.kde.org/digikam/

Thanks in advance

Gilles Caulier