Optimize /api/similar
This commit is contained in:
parent
fd192310c7
commit
181ab5a8e7
20
main.go
20
main.go
@ -845,15 +845,17 @@ type webSimilarImage struct {
|
|||||||
|
|
||||||
func getSimilar(sha1 string, dhash int64, pixels int64, distance int) (
|
func getSimilar(sha1 string, dhash int64, pixels int64, distance int) (
|
||||||
result []webSimilarImage, err error) {
|
result []webSimilarImage, err error) {
|
||||||
// For distance ∈ {0, 1}, this query is quite inefficient.
|
// If there's a dhash, there should also be thumbnail dimensions.
|
||||||
// In exchange, it's generic.
|
var rows *sql.Rows
|
||||||
//
|
common := `SELECT sha1, width * height, IFNULL(thumbw, 0), IFNULL(thumbh, 0)
|
||||||
// If there's a dhash, there should also be thumbnail dimensions,
|
FROM image WHERE sha1 <> ? AND `
|
||||||
// so not bothering with IFNULL on them.
|
if distance == 0 {
|
||||||
rows, err := db.Query(`
|
rows, err = db.Query(common+`dhash = ?`, sha1, dhash)
|
||||||
SELECT sha1, width * height, IFNULL(thumbw, 0), IFNULL(thumbh, 0)
|
} else {
|
||||||
FROM image WHERE sha1 <> ? AND dhash IS NOT NULL
|
// This is generic, but quite inefficient for distance ∈ {0, 1}.
|
||||||
AND hamming(dhash, ?) = ?`, sha1, dhash, distance)
|
rows, err = db.Query(common+`dhash IS NOT NULL
|
||||||
|
AND hamming(dhash, ?) = ?`, sha1, dhash, distance)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user