Skip to content

Instantly share code, notes, and snippets.

@saicitus
Last active March 10, 2017 00:58
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 saicitus/0115b96bbc1252ef704199d598d2abae to your computer and use it in GitHub Desktop.
Save saicitus/0115b96bbc1252ef704199d598d2abae to your computer and use it in GitHub Desktop.
-- a reference table
CREATE TABLE states (
code char(2) PRIMARY KEY,
full_name text NOT NULL,
general_sales_tax numeric(4,3)
);
-- distribute it to all workers
SELECT create_reference_table('states');
--large_table
CREATE TABLE logs(
id int,
user_id int,
state_code char(2) PRIMARY KEY,
...,
....,
)
--distribute the table logs on say user_id(could be anything)
SELECT create_distributed_table('logs','user_id');
--Query - Get me the number of logs grouped on state.
SELECT s.name, count(1) from logs l JOIN states s ON l.state_code=s.code GROUP BY s.code, s.name;
-- The above query uses the replicated states for the join operation even though the join is not on the partition column.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment