diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2024-12-29 18:10:15 +0100 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2024-12-29 18:10:15 +0100 |
commit | 181ab5a8e775a70eb14f51f36d52a99b78195052 (patch) | |
tree | 41224175fc321c9fdc2475305d994716f7c3c1cd | |
parent | fd192310c722f9169dab06282a908ddef509fb58 (diff) | |
download | gallery-181ab5a8e775a70eb14f51f36d52a99b78195052.tar.gz gallery-181ab5a8e775a70eb14f51f36d52a99b78195052.tar.xz gallery-181ab5a8e775a70eb14f51f36d52a99b78195052.zip |
Optimize /api/similarHEADorigin/mastermaster
-rw-r--r-- | main.go | 20 |
1 files changed, 11 insertions, 9 deletions
@@ -845,15 +845,17 @@ type webSimilarImage struct { func getSimilar(sha1 string, dhash int64, pixels int64, distance int) ( result []webSimilarImage, err error) { - // For distance ∈ {0, 1}, this query is quite inefficient. - // In exchange, it's generic. - // - // If there's a dhash, there should also be thumbnail dimensions, - // so not bothering with IFNULL on them. - rows, err := db.Query(` - SELECT sha1, width * height, IFNULL(thumbw, 0), IFNULL(thumbh, 0) - FROM image WHERE sha1 <> ? AND dhash IS NOT NULL - AND hamming(dhash, ?) = ?`, sha1, dhash, distance) + // If there's a dhash, there should also be thumbnail dimensions. + var rows *sql.Rows + common := `SELECT sha1, width * height, IFNULL(thumbw, 0), IFNULL(thumbh, 0) + FROM image WHERE sha1 <> ? AND ` + if distance == 0 { + rows, err = db.Query(common+`dhash = ?`, sha1, dhash) + } else { + // This is generic, but quite inefficient for distance ∈ {0, 1}. + rows, err = db.Query(common+`dhash IS NOT NULL + AND hamming(dhash, ?) = ?`, sha1, dhash, distance) + } if err != nil { return nil, err } |