patternsqlMinor
What are the top voted comments on closed questions, and who made the comments?
Viewed 0 times
thetopmadewhatvotedarewhoquestionsclosedand
Problem
Following this linked question and the feedback it generated, I put together a new query which utilizes a JOIN.
It can get a bit slow on sites like Stack Overflow, where the returns are greater than 50 000.
WITH UpvotedComments AS (
SELECT
PostId,
Score,
Text,
--Comments.UserDisplayName doesn't return users' current display names
Commenter = (
SELECT DisplayName
FROM Users
WHERE Id = UserId
)
FROM Comments
WHERE Score > 0
), ClosedPosts AS (
SELECT Id, ClosedDate
FROM Posts
WHERE ClosedDate IS NOT NULL
)
SELECT Score, Text, Commenter
FROM UpvotedComments
INNER JOIN ClosedPosts ON UpvotedComments.PostId = ClosedPosts.Id
ORDER BY Score DESC, ClosedDate DESCIt can get a bit slow on sites like Stack Overflow, where the returns are greater than 50 000.
Solution
SEDE has two great features that you should incorporate into your code:
Input parameters
SEDE lets you declare fields to be input towards the query, like so:
And so, I suggest you use three of them for the following things:
By using pages, you can simplify your returning dataset and help to reduce the massive call times of the query.
Magic markup columns
SEDE has a great feature where certain columns can be transformed into special markup. The specific columns are:
As it stands...
your code is good, but you are doing a few things you can improve on:
- Input parameters
- Magic markup columns
Input parameters
SEDE lets you declare fields to be input towards the query, like so:
##Parameter:type?defaultvalue##And so, I suggest you use three of them for the following things:
- Threshold (The threshold of comment scores to select)
- Amount per page (The amount per page of comments)
- Page number (To go with the above one)
By using pages, you can simplify your returning dataset and help to reduce the massive call times of the query.
Magic markup columns
SEDE has a great feature where certain columns can be transformed into special markup. The specific columns are:
- Post Link (generated from Post Id)
- User Link (generated from User Id)
- Comment Link (generated from Comment Id)
- Suggested Edit Link (generated from Suggested Edit Id)
- Tags
As it stands...
your code is good, but you are doing a few things you can improve on:
- Your generation of
Commentercan be easily replaced withUserId as [User Link]to save extraneous calling of another table.
SELECT Id, ClosedDateyourSELECTion is redundant. To use the field, you don't actually have to select it, simply reference it. Seeing as the resulting table is only used for theIdanyway, it's pointless to have it.
Code Snippets
##Parameter:type?defaultvalue##Context
StackExchange Code Review Q#118343, answer score: 2
Revisions (0)
No revisions yet.