DatabaseCreation: views.sql

File views.sql, 5.8 KB (added by 231511, 27 hours ago)

samo pogledi

Line 
1-- views.sql
2-- Погледи (views) и помошни индекси за нив.
3
4CREATE INDEX IF NOT EXISTS idx_sites_region ON Sites(region_id);
5
6CREATE INDEX IF NOT EXISTS idx_sites_status ON Sites(protection_status_id);
7
8DROP VIEW IF EXISTS Heritage_Full_Overview CASCADE;
9
10CREATE INDEX IF NOT EXISTS idx_objects_site_id ON Objects(site_id);
11
12CREATE INDEX IF NOT EXISTS idx_fragments_site_id ON Fragments(site_id);
13
14CREATE OR REPLACE VIEW Heritage_Full_Overview AS
15SELECT
16 f.fragment_id,
17 f.description AS fragment_description,
18 o.title AS object_title,
19 s.site_id,
20 s.site_name,
21 r.name AS region,
22 ps.name AS protection_status
23FROM Fragments f
24JOIN Objects o ON f.object_id = o.object_id
25JOIN Sites s ON f.site_id = s.site_id
26JOIN Regions r ON s.region_id = r.region_id
27JOIN Protection_Status ps ON s.protection_status_id = ps.protection_status_id;
28
29--podeleni spored lokalitet
30
31CREATE INDEX IF NOT EXISTS idx_objects_site_id ON Objects(site_id);
32
33CREATE INDEX IF NOT EXISTS idx_fragments_site_id ON Fragments(site_id);
34
35ANALYZE Objects;
36
37ANALYZE Fragments;
38
39create OR replace VIEW Site_Statistics AS
40SELECT
41 s.site_id,
42 s.site_name,
43 r.name AS region,
44 COALESCE(o_counts.total_objects, 0) AS total_objects,
45 COALESCE(f_counts.total_fragments, 0) AS total_fragments,
46 (CURRENT_DATE - (floor(random() * 3650) * interval '1 day'))::timestamp AS last_checked_at
47FROM Sites s
48JOIN Regions r ON s.region_id = r.region_id
49LEFT JOIN (
50 SELECT site_id, COUNT(*) AS total_objects
51 FROM Objects
52 GROUP BY site_id
53) o_counts ON s.site_id = o_counts.site_id
54LEFT JOIN (
55 SELECT site_id, COUNT(*) AS total_fragments
56 FROM Fragments
57 GROUP BY site_id
58) f_counts ON s.site_id = f_counts.site_id;
59
60--zastiteni lokaliteti
61CREATE OR REPLACE VIEW Protected_Sites_Inventory AS
62SELECT
63 s.site_id,
64 s.site_name,
65 r.name AS region,
66 s.discovery_year,
67 COALESCE(o.total_objects_count, 0) AS total_objects_count
68FROM Sites s
69JOIN Regions r
70 ON s.region_id = r.region_id
71JOIN Protection_Status ps
72 ON s.protection_status_id = ps.protection_status_id
73LEFT JOIN (
74 SELECT site_id, COUNT(*) AS total_objects_count
75 FROM Objects
76 GROUP BY site_id
77) o
78 ON s.site_id = o.site_id
79WHERE ps.name = 'Заштитен'
80ORDER BY s.discovery_year DESC;
81
82--spored materijali se prebaruva
83CREATE OR REPLACE VIEW Objects_with_Materials AS
84SELECT
85 o.object_id,
86 o.title,
87 m.name AS material
88FROM Objects o
89LEFT JOIN Materials_Objects om
90 ON o.object_id = om.object_id
91LEFT JOIN Materials m
92 ON om.material_id = m.material_id;
93
94-- kade se naogaat vo koj lokalitet momentalno
95CREATE OR REPLACE VIEW Object_Current_Location AS
96SELECT
97 o.object_id,
98 o.title,
99 i.name AS institution
100FROM Objects o
101JOIN Object_Location_History olh ON olh.object_id = o.object_id
102JOIN Institutions i ON i.institution_id = olh.institution_id
103WHERE olh.end_date IS NULL;
104
105CREATE INDEX idx_olh_end_date ON Object_Location_History(end_date);
106
107--na koja kultura pripaga
108CREATE OR REPLACE VIEW Object_with_Culture AS
109SELECT
110 o.object_id,
111 o.title,
112 c.name AS culture,
113 cat.name AS category
114FROM Objects o
115JOIN Object_Classification oc ON oc.object_id = o.object_id
116JOIN Culture c ON c.culture_id = oc.culture_id
117JOIN Categories cat ON cat.category_id = oc.category_id;
118
119CREATE INDEX idx_oc_object_id ON Object_Classification(object_id);
120
121--koi predmeti na koi izlozbi
122
123CREATE OR REPLACE VIEW Exhibition_Objects AS
124SELECT
125 e.exhibition_id,
126 e.name AS exhibition,
127 o.object_id,
128 o.title AS object,
129 i.name AS institution
130FROM Object_Exhibition oe
131JOIN Exhibitions e ON e.exhibition_id = oe.exhibition_id
132JOIN Objects o ON o.object_id = oe.object_id
133JOIN Institutions i ON i.institution_id = e.location_institution_id;
134
135CREATE INDEX idx_oe_exhibition_id ON Object_Exhibition(exhibition_id);
136
137CREATE INDEX idx_oe_object_id ON Object_Exhibition(object_id);
138
139--koj naucnik ili istrazuvas pobaral da istrazuva predmet dali mu e odobreno momentalno
140
141CREATE OR REPLACE VIEW Research_Access_Details AS
142SELECT
143 ra.user_id,
144 u.full_name,
145 ra.object_id,
146 o.title AS object,
147 i.name AS institution,
148 s.status_name AS access_status,
149 ra.access_date
150FROM Researcher_Access ra
151JOIN Users u ON u.user_id = ra.user_id
152JOIN Objects o ON o.object_id = ra.object_id
153JOIN Institutions i ON i.institution_id = ra.institution_id
154JOIN Status_Types s ON s.status_id = ra.access_status_id
155WHERE s.type = 'access';
156
157CREATE INDEX idx_ra_user_id ON Researcher_Access(user_id);
158
159CREATE INDEX idx_ra_object_id ON Researcher_Access(object_id);
160
161ANALYZE Researcher_Access;
162
163CREATE INDEX idx_ra_institution_id ON Researcher_Access(institution_id);
164
165ANALYZE Objects;
166
167CREATE INDEX IF NOT EXISTS idx_tsl_treatment
168ON Treatment_Step_Log(treatment_id);
169
170CREATE INDEX IF NOT EXISTS idx_treatments_object
171ON Treatments(object_id);
172
173CREATE INDEX IF NOT EXISTS idx_tsl_treatment_user_step
174ON Treatment_Step_Log(treatment_id, performed_by_user, step_number);
175
176CREATE INDEX idx_ra_user_object
177ON Researcher_Access(user_id, object_id);
178
179--tretmani vrz odreden objekt
180
181CREATE OR REPLACE VIEW Treatment_History AS
182SELECT
183 t.object_id,
184 o.title,
185 t.treatment_date,
186 tsl.step_number,
187 tsl.step_description,
188 u.full_name
189FROM Treatments t
190JOIN Objects o ON o.object_id = t.object_id
191JOIN Treatment_Step_Log tsl ON tsl.treatment_id = t.treatment_id
192JOIN Users u ON u.user_id = tsl.performed_by_user;
193
194--koj avtor koi publikacii gi napravil
195
196CREATE OR REPLACE VIEW Publications_with_Authors AS
197SELECT
198 pa.publication_id,
199 p.title,
200 a.full_name AS author
201FROM Publication_Authors pa
202JOIN Publications p ON p.publication_id = pa.publication_id
203JOIN Authors a ON a.author_id = pa.author_id;
204
205CREATE INDEX idx_pa_publication_id ON Publication_Authors(publication_id);
206
207CREATE INDEX idx_pa_author_id ON Publication_Authors(author_id);
208
209ANALYZE Publication_Authors;
210
211ANALYZE Publications;