From c595ef68c7ce266d44ecf27b949c497b345e48c4 Mon Sep 17 00:00:00 2001 From: Tobias Schottdorf Date: Mon, 14 Mar 2016 18:27:59 -0400 Subject: [PATCH] Use more efficient query See https://github.com/cockroachdb/cockroach/issues/4925#issuecomment-196513251 --- photos/db.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/photos/db.go b/photos/db.go index 703d0b8..3018a56 100644 --- a/photos/db.go +++ b/photos/db.go @@ -315,8 +315,10 @@ func listComments(tx *sql.Tx, userID int, commentIDs *[][]byte) ([]byte, error) if err != nil { return nil, err } - const selectSQL = ` -SELECT commentID, userID, message, timestamp FROM comments WHERE photoID = $1 ORDER BY timestamp DESC LIMIT 100` + const selectSQL = `SELECT commentID, userID, message, timestamp FROM comments ` + + `WHERE photoID = $1 AND commentID IN ` + + `(SELECT commentID FROM comments WHERE photoID = $1 ORDER BY timestamp DESC LIMIT 100)` + + `ORDER BY timestamp DESC` rows, err := tx.Query(selectSQL, photoID) switch { case err == sql.ErrNoRows: