diff options
author | Dirk Engling <erdgeist@erdgeist.org> | 2019-03-07 15:25:08 +0100 |
---|---|---|
committer | Dirk Engling <erdgeist@erdgeist.org> | 2019-03-07 15:25:08 +0100 |
commit | a448659fb888440de4fd447f07e9629dd5150624 (patch) | |
tree | 3d2f8597f1329a986109d7de22a18a5af4012b5d | |
parent | 0e52aaa51092c5535cd4e2686bc5c15f7f7b9c5b (diff) |
Turn postgres description document into proper sql batch
-rw-r--r-- | src/postprocess/postgres.sql | 110 | ||||
-rw-r--r-- | src/postprocess/postgres.txt | 76 |
2 files changed, 110 insertions, 76 deletions
diff --git a/src/postprocess/postgres.sql b/src/postprocess/postgres.sql new file mode 100644 index 0000000..2d89d1c --- /dev/null +++ b/src/postprocess/postgres.sql | |||
@@ -0,0 +1,110 @@ | |||
1 | \timing on | ||
2 | |||
3 | CREATE TABLE telefonbuch ( | ||
4 | id SERIAL PRIMARY KEY, | ||
5 | presence_flag bigint, | ||
6 | reverse_flag bigint, | ||
7 | biz_flag bigint, | ||
8 | zip character varying(64)[] DEFAULT '{}', | ||
9 | nachname character varying(256)[] DEFAULT '{}', | ||
10 | vorname character varying(192)[] DEFAULT '{}', | ||
11 | zusaetze character varying(512)[] DEFAULT '{}', | ||
12 | strasse character varying(64)[] DEFAULT '{}', | ||
13 | hausnummer character varying(32)[] DEFAULT '{}', | ||
14 | verweise text[] DEFAULT '{}', | ||
15 | ort character varying(64)[] DEFAULT '{}', | ||
16 | vorwahl character varying(10)[] DEFAULT '{}', | ||
17 | rufnummer character varying(32)[] DEFAULT '{}', | ||
18 | web character varying(256)[] DEFAULT '{}', | ||
19 | email character varying(256)[] DEFAULT '{}', | ||
20 | coords character varying(32)[] DEFAULT '{}' | ||
21 | ); | ||
22 | |||
23 | COPY Telefonbuch ( presence_flag, reverse_flag, biz_flag, zip, nachname, vorname, zusaetze, strasse, hausnummer, verweise, ort, vorwahl, rufnummer, web, email, coords ) FROM '/Users/erdgeist/Coding/Telefonbuch/work/telefonbuch.txt' WITH NULL AS ''; | ||
24 | |||
25 | -- # Clean up the old ones | ||
26 | DROP TABLE IF EXISTS table_nachname, table_vorname, table_zusaetze, table_strasse, table_hausnummer, table_zip, table_ort, table_verweise, table_vorwahl, table_rufnummer, table_web, table_email; | ||
27 | |||
28 | -- # tables we use for indexing | ||
29 | CREATE TABLE table_nachname ( telefonbuch_id integer, offs integer, value text); | ||
30 | CREATE TABLE table_vorname ( telefonbuch_id integer, offs integer, value text); | ||
31 | CREATE TABLE table_zusaetze ( telefonbuch_id integer, offs integer, value text); | ||
32 | CREATE TABLE table_strasse ( telefonbuch_id integer, offs integer, value text); | ||
33 | CREATE TABLE table_hausnummer ( telefonbuch_id integer, offs integer, value text); | ||
34 | CREATE TABLE table_zip ( telefonbuch_id integer, offs integer, value text); | ||
35 | CREATE TABLE table_ort ( telefonbuch_id integer, offs integer, value text); | ||
36 | CREATE TABLE table_verweise ( telefonbuch_id integer, offs integer, value text); | ||
37 | CREATE TABLE table_vorwahl ( telefonbuch_id integer, offs integer, value text); | ||
38 | CREATE TABLE table_rufnummer ( telefonbuch_id integer, offs integer, value text); | ||
39 | CREATE TABLE table_web ( telefonbuch_id integer, offs integer, value text); | ||
40 | CREATE TABLE table_email ( telefonbuch_id integer, offs integer, value text); | ||
41 | |||
42 | -- # Take copies of all content rows for indexing | ||
43 | INSERT INTO table_nachname ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(nachname ) WITH ORDINALITY u(value, offs); | ||
44 | INSERT INTO table_vorname ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(vorname ) WITH ORDINALITY u(value, offs); | ||
45 | INSERT INTO table_zusaetze ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(zusaetze ) WITH ORDINALITY u(value, offs); | ||
46 | INSERT INTO table_strasse ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(strasse ) WITH ORDINALITY u(value, offs); | ||
47 | INSERT INTO table_hausnummer ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(hausnummer ) WITH ORDINALITY u(value, offs); | ||
48 | INSERT INTO table_zip ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(zip ) WITH ORDINALITY u(value, offs); | ||
49 | INSERT INTO table_ort ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(ort ) WITH ORDINALITY u(value, offs); | ||
50 | INSERT INTO table_verweise ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(verweise ) WITH ORDINALITY u(value, offs); | ||
51 | INSERT INTO table_vorwahl ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(vorwahl ) WITH ORDINALITY u(value, offs); | ||
52 | INSERT INTO table_rufnummer ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(rufnummer ) WITH ORDINALITY u(value, offs); | ||
53 | INSERT INTO table_web ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(web ) WITH ORDINALITY u(value, offs); | ||
54 | INSERT INTO table_email ( telefonbuch_id, offs, value ) SELECT id, offs, value FROM Telefonbuch CROSS JOIN unnest(email ) WITH ORDINALITY u(value, offs); | ||
55 | |||
56 | ALTER TABLE table_nachname ADD PRIMARY KEY (telefonbuch_id, offs); | ||
57 | ALTER TABLE table_vorname ADD PRIMARY KEY (telefonbuch_id, offs); | ||
58 | ALTER TABLE table_zusaetze ADD PRIMARY KEY (telefonbuch_id, offs); | ||
59 | ALTER TABLE table_strasse ADD PRIMARY KEY (telefonbuch_id, offs); | ||
60 | ALTER TABLE table_hausnummer ADD PRIMARY KEY (telefonbuch_id, offs); | ||
61 | ALTER TABLE table_zip ADD PRIMARY KEY (telefonbuch_id, offs); | ||
62 | ALTER TABLE table_ort ADD PRIMARY KEY (telefonbuch_id, offs); | ||
63 | ALTER TABLE table_verweise ADD PRIMARY KEY (telefonbuch_id, offs); | ||
64 | ALTER TABLE table_vorwahl ADD PRIMARY KEY (telefonbuch_id, offs); | ||
65 | ALTER TABLE table_rufnummer ADD PRIMARY KEY (telefonbuch_id, offs); | ||
66 | ALTER TABLE table_web ADD PRIMARY KEY (telefonbuch_id, offs); | ||
67 | ALTER TABLE table_email ADD PRIMARY KEY (telefonbuch_id, offs); | ||
68 | |||
69 | -- # equality indexes | ||
70 | CREATE INDEX idx_nachname ON table_nachname USING btree (value, telefonbuch_id, offs); | ||
71 | CREATE INDEX idx_vorname ON table_vorname USING btree (value, telefonbuch_id, offs); | ||
72 | CREATE INDEX idx_zusaetze ON table_zusaetze USING btree (value, telefonbuch_id, offs); | ||
73 | CREATE INDEX idx_strasse ON table_strasse USING btree (value, telefonbuch_id, offs); | ||
74 | CREATE INDEX idx_hausnummer ON table_hausnummer USING btree (value, telefonbuch_id, offs); | ||
75 | CREATE INDEX idx_zip ON table_zip USING btree (value, telefonbuch_id, offs); | ||
76 | CREATE INDEX idx_ort ON table_ort USING btree (value, telefonbuch_id, offs); | ||
77 | CREATE INDEX idx_verweise ON table_verweise USING btree (value, telefonbuch_id, offs); | ||
78 | CREATE INDEX idx_vorwahl ON table_vorwahl USING btree (value, telefonbuch_id, offs); | ||
79 | CREATE INDEX idx_rufnummer ON table_rufnummer USING btree (value, telefonbuch_id, offs); | ||
80 | CREATE INDEX idx_web ON table_web USING btree (value, telefonbuch_id, offs); | ||
81 | CREATE INDEX idx_email ON table_email USING btree (value, telefonbuch_id, offs); | ||
82 | |||
83 | -- # full text indexes | ||
84 | CREATE INDEX trgm_idx_nachname ON table_nachname USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
85 | CREATE INDEX trgm_idx_vorname ON table_vorname USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
86 | CREATE INDEX trgm_idx_zusaetze ON table_zusaetze USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
87 | CREATE INDEX trgm_idx_strasse ON table_strasse USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
88 | CREATE INDEX trgm_idx_hausnummer ON table_hausnummer USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
89 | CREATE INDEX trgm_idx_zip ON table_zip USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
90 | CREATE INDEX trgm_idx_ort ON table_ort USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
91 | CREATE INDEX trgm_idx_verweise ON table_verweise USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
92 | CREATE INDEX trgm_idx_vorwahl ON table_vorwahl USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
93 | CREATE INDEX trgm_idx_rufnummer ON table_rufnummer USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
94 | CREATE INDEX trgm_idx_web ON table_web USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
95 | CREATE INDEX trgm_idx_email ON table_email USING gin (value gin_trgm_ops, telefonbuch_id, offs); | ||
96 | |||
97 | -- # slow way: | ||
98 | -- # SELECT * FROM Telefonbuch WHERE nachname = 'CCC'; | ||
99 | |||
100 | /*# using the index: | ||
101 | # SELECT * FROM Telefonbuch | ||
102 | # INNER JOIN indexed_view_nachname ON indexed_view_nachname.id = Telefonbuch.id | ||
103 | # INNER JOIN indexed_view_zip ON indexed_view_zip.id = Telefonbuch.id | ||
104 | #WHERE indexed_view_nachname.value = 'CCC' | ||
105 | #AND indexed_view_zip.value = '10117'; | ||
106 | |||
107 | # Source: https://stackoverflow.com/questions/39480580/how-to-index-a-string-array-column-for-pg-trgm-term-any-array-column-que/39584236 | ||
108 | # Tricks: SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = 'table_zusaetze'::regclass; | ||
109 | |||
110 | */ | ||
diff --git a/src/postprocess/postgres.txt b/src/postprocess/postgres.txt deleted file mode 100644 index e47ca00..0000000 --- a/src/postprocess/postgres.txt +++ /dev/null | |||
@@ -1,76 +0,0 @@ | |||
1 | CREATE TABLE telefonbuch ( | ||
2 | id SERIAL PRIMARY KEY, | ||
3 | presence_flag bigint, | ||
4 | reverse_flag bigint, | ||
5 | biz_flag bigint, | ||
6 | zip character varying(64)[] DEFAULT '{}', | ||
7 | nachname character varying(256)[] DEFAULT '{}', | ||
8 | vorname character varying(192)[] DEFAULT '{}', | ||
9 | zusaetze character varying(512)[] DEFAULT '{}', | ||
10 | strasse character varying(64)[] DEFAULT '{}', | ||
11 | hausnummer character varying(32)[] DEFAULT '{}', | ||
12 | verweise text[] DEFAULT '{}', | ||
13 | ort character varying(64)[] DEFAULT '{}', | ||
14 | vorwahl character varying(10)[] DEFAULT '{}', | ||
15 | rufnummer character varying(32)[] DEFAULT '{}', | ||
16 | web character varying(256)[] DEFAULT '{}', | ||
17 | email character varying(256)[] DEFAULT '{}', | ||
18 | coords character varying(32)[] DEFAULT '{}' | ||
19 | ); | ||
20 | |||
21 | COPY Telefonbuch ( presence_flag, reverse_flag, biz_flag, zip, nachname, vorname, zusaetze, strasse, hausnummer, verweise, ort, vorwahl, rufnummer, web, email, coords ) FROM 'telefonbuch.txt' WITH NULL AS ''; | ||
22 | |||
23 | CREATE MATERIALIZED VIEW indexed_view_nachname AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(nachname) WITH ORDINALITY u(value, row) WITH data; | ||
24 | CREATE MATERIALIZED VIEW indexed_view_vorname AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(vorname) WITH ORDINALITY u(value, row) WITH data; | ||
25 | CREATE MATERIALIZED VIEW indexed_view_vorwahl AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(vorwahl) WITH ORDINALITY u(value, row) WITH data; | ||
26 | CREATE MATERIALIZED VIEW indexed_view_rufnummer AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(rufnummer) WITH ORDINALITY u(value, row) WITH data; | ||
27 | CREATE MATERIALIZED VIEW indexed_view_strasse AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(strasse) WITH ORDINALITY u(value, row) WITH data; | ||
28 | CREATE MATERIALIZED VIEW indexed_view_hausnummer AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(hausnummer) WITH ORDINALITY u(value, row) WITH data; | ||
29 | CREATE MATERIALIZED VIEW indexed_view_zusaetze AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(zusaetze) WITH ORDINALITY u(value, row) WITH data; | ||
30 | CREATE MATERIALIZED VIEW indexed_view_verweise AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(verweise) WITH ORDINALITY u(value, row) WITH data; | ||
31 | CREATE MATERIALIZED VIEW indexed_view_zip AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(zip) WITH ORDINALITY u(value, row) WITH data; | ||
32 | CREATE MATERIALIZED VIEW indexed_view_ort AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(ort) WITH ORDINALITY u(value, row) WITH data; | ||
33 | CREATE MATERIALIZED VIEW indexed_view_web AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(web) WITH ORDINALITY u(value, row) WITH data; | ||
34 | CREATE MATERIALIZED VIEW indexed_view_email AS SELECT id, value, row FROM Telefonbuch CROSS JOIN unnest(email) WITH ORDINALITY u(value, row) WITH data; | ||
35 | |||
36 | # equality indexes | ||
37 | CREATE INDEX idx_nachname ON indexed_view_nachname USING btree (value); | ||
38 | CREATE INDEX idx_vorname ON indexed_view_vorname USING btree (value); | ||
39 | CREATE INDEX idx_vorwahl ON indexed_view_vorwahl USING btree (value); | ||
40 | CREATE INDEX idx_rufnummer ON indexed_view_rufnummer USING btree (value); | ||
41 | CREATE INDEX idx_strasse ON indexed_view_strasse USING btree (value); | ||
42 | CREATE INDEX idx_hausnummer ON indexed_view_hausnummer USING btree (value); | ||
43 | CREATE INDEX idx_zusaetze ON indexed_view_zusaetze USING btree (value); | ||
44 | CREATE INDEX idx_verweise ON indexed_view_verweise USING btree (value); | ||
45 | CREATE INDEX idx_zip ON indexed_view_zip USING btree (value); | ||
46 | CREATE INDEX idx_ort ON indexed_view_ort USING btree (value); | ||
47 | CREATE INDEX idx_web ON indexed_view_web USING btree (value); | ||
48 | CREATE INDEX idx_email ON indexed_view_email USING btree (value); | ||
49 | |||
50 | # full text indexes | ||
51 | CREATE INDEX trgm_idx_nachname ON indexed_view_nachname USING gin (value gin_trgm_ops); | ||
52 | CREATE INDEX trgm_idx_vorname ON indexed_view_vorname USING gin (value gin_trgm_ops); | ||
53 | CREATE INDEX trgm_idx_vorwahl ON indexed_view_vorwahl USING gin (value gin_trgm_ops); | ||
54 | CREATE INDEX trgm_idx_rufnummer ON indexed_view_rufnummer USING gin (value gin_trgm_ops); | ||
55 | CREATE INDEX trgm_idx_strasse ON indexed_view_strasse USING gin (value gin_trgm_ops); | ||
56 | CREATE INDEX trgm_idx_hausnummer ON indexed_view_hausnummer USING gin (value gin_trgm_ops); | ||
57 | CREATE INDEX trgm_idx_zusaetze ON indexed_view_zusaetze USING gin (value gin_trgm_ops); | ||
58 | CREATE INDEX trgm_idx_verweise ON indexed_view_verweise USING gin (value gin_trgm_ops); | ||
59 | CREATE INDEX trgm_idx_zip ON indexed_view_zip USING gin (value gin_trgm_ops); | ||
60 | CREATE INDEX trgm_idx_ort ON indexed_view_ort USING gin (value gin_trgm_ops); | ||
61 | CREATE INDEX trgm_idx_web ON indexed_view_web USING gin (value gin_trgm_ops); | ||
62 | CREATE INDEX trgm_idx_email ON indexed_view_email USING gin (value gin_trgm_ops); | ||
63 | |||
64 | # slow way: | ||
65 | SELECT * FROM Telefonbuch WHERE nachname = 'CCC'; | ||
66 | |||
67 | # using the index: | ||
68 | SELECT * FROM Telefonbuch | ||
69 | INNER JOIN indexed_view_nachname ON indexed_view_nachname.id = Telefonbuch.id | ||
70 | INNER JOIN indexed_view_zip ON indexed_view_zip.id = Telefonbuch.id | ||
71 | WHERE indexed_view_nachname.value = 'CCC' | ||
72 | AND indexed_view_zip.value = '10117'; | ||
73 | |||
74 | |||
75 | # Source: https://stackoverflow.com/questions/39480580/how-to-index-a-string-array-column-for-pg-trgm-term-any-array-column-que/39584236 | ||
76 | |||