SHOW:
|
|
- or go back to the newest paste.
| 1 | - | -- Table: emails |
| 1 | + | --DROP TABLE emails; |
| 2 | ||
| 3 | - | DROP TABLE emails; |
| 3 | + | |
| 4 | ( | |
| 5 | id serial NOT NULL, | |
| 6 | subject text, | |
| 7 | created_at timestamp without time zone, | |
| 8 | updated_at timestamp without time zone, | |
| 9 | folder text, | |
| 10 | message_id text, | |
| 11 | CONSTRAINT emails_pkey PRIMARY KEY (id) | |
| 12 | - | "references" text[], |
| 12 | + | |
| 13 | WITH ( | |
| 14 | OIDS=FALSE | |
| 15 | ); | |
| 16 | ||
| 17 | -- Index: emails_message_id_index | |
| 18 | ||
| 19 | -- DROP INDEX emails_message_id_index; | |
| 20 | ||
| 21 | CREATE INDEX emails_message_id_index | |
| 22 | ON emails | |
| 23 | USING btree | |
| 24 | (message_id COLLATE pg_catalog."default"); | |
| 25 | ||
| 26 | INSERT INTO emails( | |
| 27 | subject, created_at, updated_at, folder, message_id) | |
| 28 | VALUES ('howzitgoin', now(), now(), null, '53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail');
| |
| 29 | ||
| 30 | - | subject, created_at, updated_at, folder, "references", message_id) |
| 30 | + | |
| 31 | - | VALUES ('root', now(), now(), 'INBOX', '{}', 'CAEBV8YT3W6XZSZph7hq0Nmc-mxZ7Ao+RorC=ZrKm1mh-QumjgA@mail.gmail.com');
|
| 31 | + | subject, created_at, updated_at, folder, message_id) |
| 32 | VALUES ('Re: howzitgoin', now(), now(), 'INBOX', 'CAEBV8YTu_A6LtP_uGuQ-QSVj3zojWUiwcjGZpsPPEz1Pj3_i1A@mail.gmail.com');
| |
| 33 | ||
| 34 | - | subject, created_at, updated_at, folder, "references", message_id) |
| 34 | + | |
| 35 | - | VALUES ('one', now(), now(), 'INBOX', '{CAEBV8YT3W6XZSZph7hq0Nmc-mxZ7Ao+RorC=ZrKm1mh-QumjgA@mail.gmail.com}','536b9d2354247_23fd346dd78f01108f@9369fd83-7877-4eec-8cc8-4b207abbae85.mail');
|
| 35 | + | subject, created_at, updated_at, folder, message_id) |
| 36 | VALUES ('Re: howzitgoin', now(), now(), 'INBOX', '53856b88a2a09_23fa9605badd01601b@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail');
| |
| 37 | ||
| 38 | - | subject, created_at, updated_at, folder, "references", message_id) |
| 38 | + | |
| 39 | - | VALUES ('two', now(), now(), 'INBOX', '{CAEBV8YT3W6XZSZph7hq0Nmc-mxZ7Ao+RorC=ZrKm1mh-QumjgA@mail.gmail.com,536b9d2354247_23fd346dd78f01108f@9369fd83-7877-4eec-8cc8-4b207abbae85.mail}','CAEBV8YRm0-bmZ06UdWXbUyezRbTC=BtDo2xztH0-uYwUYtJ0Bw@mail.gmail.com');
|
| 39 | + | subject, created_at, updated_at, folder, message_id) |
| 40 | VALUES ('Re: howzitgoin', now(), now(), 'INBOX', 'CAEBV8YT6vx6buOKUga4f=bcNGq_=WzwiqEzm2FWm3HoLZ8SbJA@mail.gmail.com');
| |
| 41 | ||
| 42 | - | subject, created_at, updated_at, folder, "references", message_id) |
| 42 | + | |
| 43 | - | VALUES ('three', now(), now(), 'INBOX', '{CAEBV8YT3W6XZSZph7hq0Nmc-mxZ7Ao+RorC=ZrKm1mh-QumjgA@mail.gmail.com,536b9d2354247_23fd346dd78f01108f@9369fd83-7877-4eec-8cc8-4b207abbae85.mail,CAEBV8YRm0-bmZ06UdWXbUyezRbTC=BtDo2xztH0-uYwUYtJ0Bw@mail.gmail.com}','536b9d5b6d64c_23fd346dd78f0111bf@9369fd83-7877-4eec-8cc8-4b207abbae85.mail');
|
| 43 | + | subject, created_at, updated_at, folder, message_id) |
| 44 | VALUES ('Re: howzitgoin', now(), now(), 'INBOX', 'CAEBV8YRGwkNSb3cxquS5abSCmnwLn37GxCpg74mQe7=3SC5cdQ@mail.gmail.com');
| |
| 45 | ||
| 46 | - | subject, created_at, updated_at, folder, "references", message_id) |
| 46 | + | |
| 47 | - | VALUES ('four', now(), now(), 'INBOX', '{CAEBV8YT3W6XZSZph7hq0Nmc-mxZ7Ao+RorC=ZrKm1mh-QumjgA@mail.gmail.com,536b9d2354247_23fd346dd78f01108f@9369fd83-7877-4eec-8cc8-4b207abbae85.mail,CAEBV8YRm0-bmZ06UdWXbUyezRbTC=BtDo2xztH0-uYwUYtJ0Bw@mail.gmail.com,536b9d5b6d64c_23fd346dd78f0111bf@9369fd83-7877-4eec-8cc8-4b207abbae85.mail}','CAEBV8YR97FSa5Bf-_+NrkV+edDM0R-NwSQmW0CXGwyg0EuG6qg@mail.gmail.com');
|
| 47 | + | subject, created_at, updated_at, folder, message_id) |
| 48 | VALUES ('Not part of thread', now(), now(), 'INBOX', 'CAEBV8YRGwkNSb3cxquS5abSCmnwLn37GxCpg74mQe7=3SC5cdQ@mail.gmail.com');
| |
| 49 | - | SELECT DISTINCT e.id, folder, subject, message_id, "references", e.updated_at, |
| 49 | + | |
| 50 | - | (select count(message_id) from emails where (select "references"[1] from emails where message_id = e.message_id) = ANY ("references") or message_id = (select "references"[1] from emails where message_id = e.message_id)) as replies
|
| 50 | + | -- Table: replies |
| 51 | - | FROM "emails" e |
| 51 | + | |
| 52 | - | WHERE ("folder" = 'INBOX'); |
| 52 | + | -- DROP TABLE replies; |
| 53 | ||
| 54 | CREATE TABLE replies | |
| 55 | ( | |
| 56 | id serial NOT NULL, | |
| 57 | message_id text, | |
| 58 | created_at timestamp without time zone, | |
| 59 | email_id integer NOT NULL, | |
| 60 | CONSTRAINT replies_pkey PRIMARY KEY (id), | |
| 61 | CONSTRAINT replies_email_id_fkey FOREIGN KEY (email_id) | |
| 62 | REFERENCES emails (id) MATCH SIMPLE | |
| 63 | ON UPDATE NO ACTION ON DELETE CASCADE | |
| 64 | ) | |
| 65 | WITH ( | |
| 66 | OIDS=FALSE | |
| 67 | ); | |
| 68 | ||
| 69 | ||
| 70 | -- Index: replies_email_id_index | |
| 71 | ||
| 72 | -- DROP INDEX replies_email_id_index; | |
| 73 | ||
| 74 | CREATE INDEX replies_email_id_index | |
| 75 | ON replies | |
| 76 | USING btree | |
| 77 | (email_id); | |
| 78 | ||
| 79 | -- Index: replies_message_id_index | |
| 80 | ||
| 81 | -- DROP INDEX replies_message_id_index; | |
| 82 | ||
| 83 | CREATE INDEX replies_message_id_index | |
| 84 | ON replies | |
| 85 | USING btree | |
| 86 | (message_id COLLATE pg_catalog."default"); | |
| 87 | ||
| 88 | ||
| 89 | ||
| 90 | INSERT INTO replies(message_id, created_at, email_id) | |
| 91 | VALUES ('53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 2);
| |
| 92 | ||
| 93 | INSERT INTO replies(message_id, created_at, email_id) | |
| 94 | VALUES ('53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 3);
| |
| 95 | ||
| 96 | INSERT INTO replies(message_id, created_at, email_id) | |
| 97 | VALUES ('CAEBV8YTu_A6LtP_uGuQ-QSVj3zojWUiwcjGZpsPPEz1Pj3_i1A@mail.gmail.com', now(), 3);
| |
| 98 | ||
| 99 | INSERT INTO replies(message_id, created_at, email_id) | |
| 100 | VALUES ('53856b88a2a09_23fa9605badd01601b@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 4);
| |
| 101 | ||
| 102 | INSERT INTO replies(message_id, created_at, email_id) | |
| 103 | VALUES ('CAEBV8YTu_A6LtP_uGuQ-QSVj3zojWUiwcjGZpsPPEz1Pj3_i1A@mail.gmail.com', now(), 4);
| |
| 104 | ||
| 105 | INSERT INTO replies(message_id, created_at, email_id) | |
| 106 | VALUES ('53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 4);
| |
| 107 | ||
| 108 | INSERT INTO replies(message_id, created_at, email_id) | |
| 109 | VALUES ('53856b88a2a09_23fa9605badd01601b@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 5);
| |
| 110 | ||
| 111 | INSERT INTO replies(message_id, created_at, email_id) | |
| 112 | VALUES ('CAEBV8YTu_A6LtP_uGuQ-QSVj3zojWUiwcjGZpsPPEz1Pj3_i1A@mail.gmail.com', now(), 5);
| |
| 113 | ||
| 114 | INSERT INTO replies(message_id, created_at, email_id) | |
| 115 | VALUES ('53856b1448c89_23fa9605badd015951@3a139e8c-0b81-42c2-8e59-133c262e96a9.mail', now(), 5);
| |
| 116 | ||
| 117 | ||
| 118 | SELECT "emails".id, "emails".message_id, subject, "count", "emails".created_at, "count" | |
| 119 | FROM "emails" | |
| 120 | LEFT JOIN | |
| 121 | ( | |
| 122 | SELECT "email_id", count(*) AS "count" FROM "replies" GROUP BY "email_id" | |
| 123 | ) AS "r" | |
| 124 | ON ("emails"."id" = "r"."email_id")
| |
| 125 | WHERE (("folder" = 'INBOX')
| |
| 126 | --AND ("emails"."id" NOT IN (SELECT "email_id" FROM "replies"))
| |
| 127 | ) ORDER BY "created_at" DESC LIMIT 7 OFFSET 0 |