· 6 years ago · Jul 26, 2019, 11:08 PM
1DELETE FROM appointments
2WHERE (appointments.company_id NOT IN (6, 753, 785, 1611))
3
4DELETE FROM mappings
5WHERE mappings.id IN (SELECT mappings.id
6 FROM code_mappings
7 INNER JOIN codes ON codes.remote_id = mappings.code_remote_id
8 WHERE (codes.company_id NOT IN (6, 753, 785, 1611)))
9
10DELETE FROM mappings AS m
11WHERE EXISTS ( SELECT 1
12 FROM code_mappings AS cm
13 INNER JOIN codes AS c
14 ON c.remote_id = cm.code_remote_id
15 WHERE
16 (
17 c.company_id NOT IN (6, 753, 785, 1611)
18 AND cm.id = m.id
19 )
20)
21
22CREATE INDEX company_id_idx ON appointments (company_id);
23
24CREATE INDEX remote_id_company_id_idx ON codes (remote_id, company_id);
25
26CREATE INDEX code_remote_id_id_idx ON code_mappings (code_remote_id, id);
27
28-- If you don't already have a primary key OR index on `id` column in the `mappings` table, then create one:
29
30ALTER TABLE mappings ADD PRIMARY KEY (id);
31-- Choose primary key, or index: CREATE INDEX id_idx ON mappings (id);