Optimize SQL: CountLikesReceived

This commit is contained in:
Noah Petherbridge 2024-08-25 20:43:22 -07:00
parent e0e98d8df6
commit 3921691319

View File

@ -77,20 +77,34 @@ func CountLikesGiven(user *User) int64 {
// CountLikesReceived by a user. // CountLikesReceived by a user.
func CountLikesReceived(user *User) int64 { func CountLikesReceived(user *User) int64 {
var count int64 var count int64
DB.Model(&Like{}).Joins(
"LEFT OUTER JOIN photos ON (likes.table_name = 'photos' AND likes.table_id = photos.id)", // Do a UNION query as it's more efficient than joining Likes to all the other tables.
).Joins( result := DB.Raw(`
"LEFT OUTER JOIN users ON (likes.table_name = 'users' AND likes.table_id = users.id)", SELECT sum(c) FROM (
).Joins( SELECT count(*) AS c
"LEFT OUTER JOIN comments ON (likes.table_name = 'comments' AND likes.table_id = comments.id)", FROM likes
).Where(` WHERE likes.table_name = 'users' AND likes.table_id = ?
(likes.table_name = 'photos' AND photos.user_id = ?)
OR UNION
(likes.table_name = 'users' AND likes.table_id = ?)
OR SELECT count(*) AS c
(likes.table_name = 'comments' AND comments.user_id = ?)`, FROM likes
user.ID, user.ID, user.ID, JOIN photos ON (likes.table_name='photos' AND likes.table_id=photos.id)
).Count(&count) WHERE photos.user_id = ?
UNION
SELECT count(*) AS c
FROM likes
JOIN comments ON (likes.table_name = 'comments' AND likes.table_id=comments.id)
WHERE comments.user_id = ?
) AS c
`, user.ID, user.ID, user.ID).Scan(&count)
if result.Error != nil {
log.Error("CountLikesReceived: %s", result.Error)
}
return count return count
} }