Skip to content

Instantly share code, notes, and snippets.

@waltherg
Last active March 4, 2016 20:45
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 waltherg/6496152c9c2c63f7727a to your computer and use it in GitHub Desktop.
Save waltherg/6496152c9c2c63f7727a to your computer and use it in GitHub Desktop.
Postgresql query test
CREATE SCHEMA IF NOT EXISTS my_test;
DROP TABLE IF EXISTS my_test.my_table;
DROP TABLE IF EXISTS my_test.a_table CASCADE;
DROP TABLE IF EXISTS my_test.a_b_relation;
CREATE TABLE my_test.my_table (
a integer,
b integer
);
CREATE TABLE my_test.a_table (
a integer PRIMARY KEY
);
CREATE TABLE my_test.a_b_relation (
a integer REFERENCES my_test.a_table (a),
b integer
);
CREATE OR REPLACE FUNCTION row_generator() RETURNS integer AS $$
BEGIN
FOR i in 1..1000000 LOOP
INSERT INTO my_test.my_table (SELECT CAST (RANDOM() * 1000 AS INT), CAST (RANDOM() * 1000 AS INT));
END LOOP;
RETURN (SELECT COUNT(*) FROM my_test.my_table);
END;
$$ language plpgsql;
SELECT * FROM row_generator();
INSERT INTO my_test.a_table (SELECT DISTINCT a FROM my_test.my_table);
SELECT COUNT(*) AS "unique a in a_table" FROM my_test.a_table;
INSERT INTO my_test.a_b_relation (SELECT * FROM my_test.my_table);
SELECT COUNT(*) AS "a-b relations" FROM my_test.a_b_relation;
EXPLAIN ANALYZE VERBOSE SELECT b FROM my_test.my_table WHERE a = 10;
EXPLAIN ANALYZE VERBOSE SELECT b FROM my_test.a_b_relation, my_test.a_table
WHERE my_test.a_table.a = my_test.a_b_relation.a AND my_test.a_table.a = 10;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment