Optimize Site Gallery query for massive friend lists
Instead of selecting the full array of friend user IDs and pasting that into the main query, use subselects for hopefully better performance.
This commit is contained in:
parent
616f6ae76b
commit
02ec0a9116
|
@ -497,7 +497,6 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
|
||||||
explicitOK = user.Explicit // User opted-in for explicit content
|
explicitOK = user.Explicit // User opted-in for explicit content
|
||||||
|
|
||||||
blocklist = BlockedUserIDs(user)
|
blocklist = BlockedUserIDs(user)
|
||||||
friendIDs = FriendIDs(userID)
|
|
||||||
privateUserIDs = PrivateGrantedUserIDs(userID)
|
privateUserIDs = PrivateGrantedUserIDs(userID)
|
||||||
privateUserIDsAreFriends = PrivateGrantedUserIDsAreFriends(user)
|
privateUserIDsAreFriends = PrivateGrantedUserIDsAreFriends(user)
|
||||||
wheres = []string{}
|
wheres = []string{}
|
||||||
|
@ -525,8 +524,13 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
|
||||||
// Admins see everything on the site (only an admin user can get an admin view).
|
// Admins see everything on the site (only an admin user can get an admin view).
|
||||||
adminView = user.HasAdminScope(config.ScopePhotoModerator) && adminView
|
adminView = user.HasAdminScope(config.ScopePhotoModerator) && adminView
|
||||||
|
|
||||||
// Include ourself in our friend IDs.
|
// Friend IDs subquery, used in a "WHERE user_id IN ?" clause.
|
||||||
friendIDs = append(friendIDs, userID)
|
friendsQuery := fmt.Sprintf(`(
|
||||||
|
SELECT target_user_id
|
||||||
|
FROM friends
|
||||||
|
WHERE source_user_id = %d
|
||||||
|
AND approved IS TRUE
|
||||||
|
)`, userID)
|
||||||
|
|
||||||
// What sets of User ID * Visibility filters to query under?
|
// What sets of User ID * Visibility filters to query under?
|
||||||
var (
|
var (
|
||||||
|
@ -539,17 +543,22 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
|
||||||
// Shy users can only see their Friends photos (public or friends visibility)
|
// Shy users can only see their Friends photos (public or friends visibility)
|
||||||
// and any Private photos to whom they were granted access.
|
// and any Private photos to whom they were granted access.
|
||||||
visOrs = append(visOrs,
|
visOrs = append(visOrs,
|
||||||
|
fmt.Sprintf("(user_id IN %s AND visibility IN ?)", friendsQuery),
|
||||||
"(user_id IN ? AND visibility IN ?)",
|
"(user_id IN ? AND visibility IN ?)",
|
||||||
"(user_id IN ? AND visibility IN ?)",
|
"user_id = ?",
|
||||||
)
|
)
|
||||||
visPlaceholders = append(visPlaceholders,
|
visPlaceholders = append(visPlaceholders,
|
||||||
friendIDs, photosFriends,
|
photosFriends,
|
||||||
privateUserIDs, photosPrivate,
|
privateUserIDs, photosPrivate,
|
||||||
|
userID,
|
||||||
)
|
)
|
||||||
} else if friendsOnly {
|
} else if friendsOnly {
|
||||||
// User wants to see only self and friends photos.
|
// User wants to see only self and friends photos.
|
||||||
visOrs = append(visOrs, "(user_id IN ? AND visibility IN ?)")
|
visOrs = append(visOrs,
|
||||||
visPlaceholders = append(visPlaceholders, friendIDs, photosFriends)
|
fmt.Sprintf("(user_id IN %s AND visibility IN ?)", friendsQuery),
|
||||||
|
"user_id = ?",
|
||||||
|
)
|
||||||
|
visPlaceholders = append(visPlaceholders, photosFriends, userID)
|
||||||
|
|
||||||
// If their friends granted private photos, include those too.
|
// If their friends granted private photos, include those too.
|
||||||
if len(privateUserIDsAreFriends) > 0 {
|
if len(privateUserIDsAreFriends) > 0 {
|
||||||
|
@ -559,14 +568,16 @@ func PaginateGalleryPhotos(user *User, conf Gallery, pager *Pagination) ([]*Phot
|
||||||
} else {
|
} else {
|
||||||
// You can see friends' Friend photos but only public for non-friends.
|
// You can see friends' Friend photos but only public for non-friends.
|
||||||
visOrs = append(visOrs,
|
visOrs = append(visOrs,
|
||||||
|
fmt.Sprintf("(user_id IN %s AND visibility IN ?)", friendsQuery),
|
||||||
"(user_id IN ? AND visibility IN ?)",
|
"(user_id IN ? AND visibility IN ?)",
|
||||||
"(user_id IN ? AND visibility IN ?)",
|
fmt.Sprintf("(user_id NOT IN %s AND visibility IN ?)", friendsQuery),
|
||||||
"(user_id NOT IN ? AND visibility IN ?)",
|
"user_id = ?",
|
||||||
)
|
)
|
||||||
visPlaceholders = append(placeholders,
|
visPlaceholders = append(placeholders,
|
||||||
friendIDs, photosFriends,
|
photosFriends,
|
||||||
privateUserIDs, photosPrivate,
|
privateUserIDs, photosPrivate,
|
||||||
friendIDs, photosPublic,
|
photosPublic,
|
||||||
|
userID,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user