Skip to content

Instantly share code, notes, and snippets.

@guiltry
Last active April 12, 2016 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guiltry/0da4b3b1d6ac2c25b006d85f861e2c75 to your computer and use it in GitHub Desktop.
Save guiltry/0da4b3b1d6ac2c25b006d85f861e2c75 to your computer and use it in GitHub Desktop.
---
CREATE TABLE users(
id integer PRIMARY KEY,
name varchar(50),
...
);
CREATE TABLE user_followers(
user_id integer,
follower_id integer,
...
CONSTRAINT user_id_follower_id PRIMARY KEY(user_id, follower_id)
);
CREATE MATERIALIZED VIEW popular_user AS
SELECT user_id, count(1) AS follower
FROM user_followers
GROUP BY user_id
;
CREATE INDEX ON popular_user (user_id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment