Commit 049afa3b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add vote column to comments table.

The script update_votes.sql calculates the values for that column based
on the entries in votes table.

This is an optimization to avoid aggregating the votes in a separate
table every time we retrieve comments.

Also updated examples.sql to use the new full_visible_comments view -
this greatly simplified all queries.
parent 08ab4f5f
Loading
Loading
Loading
Loading
+34 −29
Original line number Diff line number Diff line
@@ -2,52 +2,57 @@

-- get comments for a particular member

SELECT * FROM visible_comments c JOIN targets ON c.target_id = targets.id
WHERE targets.domain = ? AND targets.type = ? AND targets.cls = ? AND targets.member = ?
SELECT *
FROM visible_comments
WHERE domain = ? AND type = ? AND cls = ? AND member = ?;

-- get 100 most recent comments

SELECT * FROM visible_comments c JOIN targets ON c.target_id = targets.id
WHERE targets.domain = ?
ORDER BY created_at DESC LIMIT 100
SELECT *
FROM full_visible_comments
WHERE domain = 'ext-js-4'
ORDER BY created_at DESC
LIMIT 100;

-- get number of comments for each target

SELECT
    target.type AS type,
    target.cls AS cls,
    target.member AS member,
    type AS type,
    cls AS cls,
    member AS member,
    count(*) AS cnt
FROM visible_comments c JOIN targets ON c.target_id = targets.id
WHERE target.domain = ?
GROUP BY target.id
FROM full_visible_comments
WHERE domain = 'ext-js-4'
GROUP BY target_id
ORDER BY cnt;

-- get number of comments for each class (including comments for class members)

SELECT
    target.cls AS cls,
    cls AS cls,
    count(*) AS cnt
FROM visible_comments c JOIN targets ON c.target_id = targets.id
WHERE target.domain = ? AND target.type = 'class'
GROUP BY target.cls
FROM full_visible_comments
WHERE domain = 'ext-js-4' AND type = 'class'
GROUP BY cls
ORDER BY cnt;

-- get users with most upvotes

SELECT
    users.username,
    SUM(c.vote) AS votes
FROM users LEFT JOIN voted_comments c ON c.user_id = users.id
GROUP BY users.id
    username,
    SUM(vote) AS votes
FROM full_visible_comments
GROUP BY username
ORDER BY votes DESC
LIMIT 10;

-- get users with most downvotes

SELECT
    users.username,
    SUM(c.vote) AS votes
FROM users LEFT JOIN voted_comments c ON c.user_id = users.id
GROUP BY users.id
    username,
    SUM(vote) AS votes
FROM full_visible_comments
GROUP BY username
HAVING votes IS NOT NULL
ORDER BY votes ASC
LIMIT 10;
@@ -55,10 +60,10 @@ LIMIT 10;
-- get users with most comments

SELECT
    users.username,
    username,
    COUNT(*) AS comment_count
FROM users LEFT JOIN visible_comments c ON c.user_id = users.id
GROUP BY users.id
FROM full_visible_comments
GROUP BY username
ORDER BY comment_count DESC
LIMIT 10;

@@ -77,7 +82,7 @@ LIMIT 10;
SELECT
    username,
    COUNT(*) AS comment_count
FROM users LEFT JOIN visible_comments c ON c.user_id = users.id
WHERE users.moderator = 1
GROUP BY users.id
FROM full_visible_comments
WHERE moderator = 1
GROUP BY username
ORDER BY comment_count DESC;
+4 −13
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ CREATE TABLE comments (
    target_id INT NOT NULL,
    content TEXT NOT NULL,
    content_html TEXT NOT NULL,
    vote INT NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL,
    deleted BOOLEAN NOT NULL DEFAULT 0,
    FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
@@ -72,20 +73,11 @@ CREATE TABLE readings (
    CONSTRAINT unique_readings UNIQUE KEY (user_id, comment_id)
) ENGINE = InnoDB, CHARACTER SET = utf8;

CREATE VIEW visible_comments AS SELECT * FROM comments WHERE deleted = 0;

CREATE VIEW voted_comments AS SELECT
    c.*,
    SUM(v.value) AS vote
FROM visible_comments c LEFT JOIN votes v ON c.id = v.comment_id
GROUP BY c.id;
CREATE OR REPLACE VIEW visible_comments AS SELECT * FROM comments WHERE deleted = 0;

-- comments table joined with users and targets for easier quering
CREATE VIEW full_visible_comments AS SELECT
    c.id,
    c.content,
    c.content_html,
    c.created_at,
CREATE OR REPLACE VIEW full_visible_comments AS SELECT
    c.*,
    users.username,
    users.external_id,
    users.email,
@@ -97,4 +89,3 @@ CREATE VIEW full_visible_comments AS SELECT
FROM visible_comments AS c
    LEFT JOIN users ON c.user_id = users.id
    LEFT JOIN targets ON c.target_id = targets.id;
+4 −0
Original line number Diff line number Diff line
-- calculates the votes column for all comments

UPDATE comments
SET vote = (SELECT SUM(value) FROM votes WHERE votes.comment_id = comments.id);