Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Use more efficient query #45

Merged
merged 1 commit into from
Mar 15, 2016
Merged

Use more efficient query #45

merged 1 commit into from
Mar 15, 2016

Conversation

tbg
Copy link
Member

@tbg tbg commented Mar 14, 2016

@@ -258,8 +258,11 @@ func listPhotos(tx *sql.Tx, userID int, photoIDs *[][]byte) error {
if err != nil {
return err
}
const selectSQL = `
SELECT id, caption, commentCount, latitude, longitude, timestamp FROM photos WHERE userID = $1 ORDER BY timestamp DESC LIMIT 100`
const selectSQL = `SELECT commentID, userID, message, timestamp FROM comments ` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be in the wrong spot. You're selecting comments here not photos. Did you mean this to be in the listComments ,method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you change the correct SELECT? The original was FROM photos while this one is FROM comments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, weak search-fu. Let me put this in the right spot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my new comment on cockroachdb/cockroach#4925. I think we could change the schema instead of the query.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like Peter's suggestion, that would be even better. If the index stores all the needed columns (commentID, userID, message, timestamp), it should work.

The change LGTM though if you want to try this first.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy if someone wants to take it over - I've got some loose ends to tie up. @RaduBerinde, would you mind?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW Specifically Peter's suggestion is to change commentsByPhotoID to be:

CREATE UNIQUE INDEX IF NOT EXISTS commentsByPhotoID ON comments (photoID, timestamp) STORING (commentID, userID, message);

Verified locally this would do the right thing:

+-------+---------+------------------------------------+
| Level |  Type   |            Description             |
+-------+---------+------------------------------------+
|     0 | revscan | comments@commentsByPhotoID /10-/11 |
+-------+---------+------------------------------------+

But this means we will be storing each message twice. So I'm no longer sure which one is better :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm about to head out and don't know much about testing/running this (I don't even have this repo cloned). I think it's fine if you merge this as-is for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also dump the commentsByPhotoID index entirely and change the primary key of the comments table to PRIMARY KEY (photoID, timestamp, commentID).

@tbg
Copy link
Member Author

tbg commented Mar 14, 2016

@petermattis If you'd like to take this over, please go ahead - otherwise, I'm ready for an LGTM :-)

tbg added a commit that referenced this pull request Mar 15, 2016
@tbg tbg merged commit 1c01c5a into master Mar 15, 2016
@tbg tbg deleted the better_query branch March 15, 2016 01:35
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants