-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
52 lines (46 loc) · 1.96 KB
/
schema.sql
File metadata and controls
52 lines (46 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- =============================================================
-- Section 09 — Analyse Queries (EXPLAIN / EXPLAIN ANALYZE)
-- =============================================================
-- Reuses the same idea as Section 08 — a 300k-row employee table
-- — but lives in its own schema so the two sections don't collide.
-- =============================================================
DROP SCHEMA IF EXISTS adv_explain CASCADE;
CREATE SCHEMA adv_explain;
SET search_path TO adv_explain;
CREATE TABLE employee (
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(255) NOT NULL,
department VARCHAR(50) NOT NULL,
country CHAR(2) NOT NULL,
salary INTEGER NOT NULL,
is_active BOOLEAN NOT NULL,
hired_on DATE NOT NULL
);
CREATE TABLE department (
name VARCHAR(50) PRIMARY KEY,
head_name VARCHAR(100) NOT NULL
);
INSERT INTO department VALUES
('engineering','Alice Engineer'),
('sales','Bob Sales'),
('marketing','Carol Marketing'),
('support','Dave Support'),
('finance','Eve Finance'),
('people','Frank People'),
('design','Gina Design'),
('data','Hugo Data');
INSERT INTO employee (first_name, last_name, email, department, country, salary, is_active, hired_on)
SELECT
(ARRAY['Ada','Alan','Grace','Linus','Margaret','Dennis','Katherine','Radia','Barbara','Edsger'])[1 + (n % 10)],
(ARRAY['Lovelace','Turing','Hopper','Torvalds','Hamilton','Ritchie','Johnson','Perlman','Liskov','Dijkstra'])[1 + ((n / 10) % 10)],
'user' || n || '@example.com',
(ARRAY['engineering','sales','marketing','support','finance','people','design','data'])[1 + (n % 8)],
(ARRAY['GB','US','FR','DE','ES','IT','NL','PL','PT','SE'])[1 + (n % 10)],
30000 + (n % 120) * 500,
(n % 20) <> 0,
DATE '2015-01-01' + ((n % 3650) || ' days')::INTERVAL
FROM generate_series(1, 300000) AS n;
ANALYZE employee;
ANALYZE department;