Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

People: Improve update performance on MariaDB #1804

Merged
merged 1 commit into from
Jan 3, 2022
Merged

People: Improve update performance on MariaDB #1804

merged 1 commit into from
Jan 3, 2022

Conversation

srett
Copy link
Contributor

@srett srett commented Dec 11, 2021

MariaDB/MySQL traditionally have performance issues for queries of
type UPDATE ... WHERE xxx IN (SELECT ...)

Instead, use JOINs which are much faster.

As discussed in this thread, I open this PR after verifying that all tests still pass in the docker compose dev environment, and I've been running these changes on my private collection and nothing blew up so far. This is the first time I touched Go code so I hope it's not too ugly. :-)

@CLAassistant
Copy link

CLAassistant commented Dec 11, 2021

CLA assistant check
All committers have signed the CLA.

default:
update = "UPDATE photos SET checked_at = NULL WHERE id IN (SELECT f.photo_id FROM files f JOIN %s m ON m.file_uid = f.file_uid WHERE m.face_id = ?)"
}
update = fmt.Sprintf(update, Marker{}.TableName())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May lead to problems in that dialects can have different parameters or need them in a different order. So it's much easier for developers to make mistakes or they have to refactor backwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that's an unnecessary footgun. I'll pull the .Exec() call up into the branches.
Related: I was wondering why the markers table name is pulled from the according class via .TableName(), but the others aren't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we used temporary table names during development, while the other table names did not change after the initial stable release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR. Tests still pass. Wasn't sure about the err var, could have put return statements into the branches, but I think it can be confusing if a non-void function doesn't end in a return statement.

MariaDB/MySQL traditionally have performance issues for queries of
type UPDATE ... WHERE xxx IN (SELECT ...)

Instead, use JOINs which are much faster.

Signed-off-by: Simon Rettberg <simon.rettberg@rz.uni-freiburg.de>
@lastzero
Copy link
Member

lastzero commented Jan 3, 2022

Thanks a lot! I'll merge this so everyone can test it using the Development Preview 👍

@lastzero lastzero merged commit 35f45f4 into photoprism:develop Jan 3, 2022
@lastzero lastzero added the please-test Ready for acceptance test label Jan 3, 2022
@lastzero lastzero changed the title MySQL/MariaDB: Turn subquery-updates into joins for better performance MariaDB: Improve Update Performance Using Joins Jan 3, 2022
@lastzero lastzero changed the title MariaDB: Improve Update Performance Using Joins People: Improve MariaDB update performance Jan 3, 2022
@lastzero lastzero changed the title People: Improve MariaDB update performance People: Improve update performance for MariaDB Jan 3, 2022
@lastzero lastzero changed the title People: Improve update performance for MariaDB People: Improve update performance on MariaDB Jan 3, 2022
@srett srett deleted the mysql-join-updates branch January 22, 2022 17:57
@lastzero lastzero added released Available in the stable release performance Performance Optimization and removed please-test Ready for acceptance test labels Mar 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance Optimization released Available in the stable release
Projects
Status: Release 🌈
Development

Successfully merging this pull request may close these issues.

3 participants