| 1 | -- views.sql
|
|---|
| 2 | -- Погледи (views) и помошни индекси за нив.
|
|---|
| 3 |
|
|---|
| 4 | CREATE INDEX IF NOT EXISTS idx_sites_region ON Sites(region_id);
|
|---|
| 5 |
|
|---|
| 6 | CREATE INDEX IF NOT EXISTS idx_sites_status ON Sites(protection_status_id);
|
|---|
| 7 |
|
|---|
| 8 | DROP VIEW IF EXISTS Heritage_Full_Overview CASCADE;
|
|---|
| 9 |
|
|---|
| 10 | CREATE INDEX IF NOT EXISTS idx_objects_site_id ON Objects(site_id);
|
|---|
| 11 |
|
|---|
| 12 | CREATE INDEX IF NOT EXISTS idx_fragments_site_id ON Fragments(site_id);
|
|---|
| 13 |
|
|---|
| 14 | CREATE OR REPLACE VIEW Heritage_Full_Overview AS
|
|---|
| 15 | SELECT
|
|---|
| 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
|
|---|
| 23 | FROM Fragments f
|
|---|
| 24 | JOIN Objects o ON f.object_id = o.object_id
|
|---|
| 25 | JOIN Sites s ON f.site_id = s.site_id
|
|---|
| 26 | JOIN Regions r ON s.region_id = r.region_id
|
|---|
| 27 | JOIN Protection_Status ps ON s.protection_status_id = ps.protection_status_id;
|
|---|
| 28 |
|
|---|
| 29 | --podeleni spored lokalitet
|
|---|
| 30 |
|
|---|
| 31 | CREATE INDEX IF NOT EXISTS idx_objects_site_id ON Objects(site_id);
|
|---|
| 32 |
|
|---|
| 33 | CREATE INDEX IF NOT EXISTS idx_fragments_site_id ON Fragments(site_id);
|
|---|
| 34 |
|
|---|
| 35 | ANALYZE Objects;
|
|---|
| 36 |
|
|---|
| 37 | ANALYZE Fragments;
|
|---|
| 38 |
|
|---|
| 39 | create OR replace VIEW Site_Statistics AS
|
|---|
| 40 | SELECT
|
|---|
| 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
|
|---|
| 47 | FROM Sites s
|
|---|
| 48 | JOIN Regions r ON s.region_id = r.region_id
|
|---|
| 49 | LEFT 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
|
|---|
| 54 | LEFT 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
|
|---|
| 61 | CREATE OR REPLACE VIEW Protected_Sites_Inventory AS
|
|---|
| 62 | SELECT
|
|---|
| 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
|
|---|
| 68 | FROM Sites s
|
|---|
| 69 | JOIN Regions r
|
|---|
| 70 | ON s.region_id = r.region_id
|
|---|
| 71 | JOIN Protection_Status ps
|
|---|
| 72 | ON s.protection_status_id = ps.protection_status_id
|
|---|
| 73 | LEFT 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
|
|---|
| 79 | WHERE ps.name = 'Заштитен'
|
|---|
| 80 | ORDER BY s.discovery_year DESC;
|
|---|
| 81 |
|
|---|
| 82 | --spored materijali se prebaruva
|
|---|
| 83 | CREATE OR REPLACE VIEW Objects_with_Materials AS
|
|---|
| 84 | SELECT
|
|---|
| 85 | o.object_id,
|
|---|
| 86 | o.title,
|
|---|
| 87 | m.name AS material
|
|---|
| 88 | FROM Objects o
|
|---|
| 89 | LEFT JOIN Materials_Objects om
|
|---|
| 90 | ON o.object_id = om.object_id
|
|---|
| 91 | LEFT JOIN Materials m
|
|---|
| 92 | ON om.material_id = m.material_id;
|
|---|
| 93 |
|
|---|
| 94 | -- kade se naogaat vo koj lokalitet momentalno
|
|---|
| 95 | CREATE OR REPLACE VIEW Object_Current_Location AS
|
|---|
| 96 | SELECT
|
|---|
| 97 | o.object_id,
|
|---|
| 98 | o.title,
|
|---|
| 99 | i.name AS institution
|
|---|
| 100 | FROM Objects o
|
|---|
| 101 | JOIN Object_Location_History olh ON olh.object_id = o.object_id
|
|---|
| 102 | JOIN Institutions i ON i.institution_id = olh.institution_id
|
|---|
| 103 | WHERE olh.end_date IS NULL;
|
|---|
| 104 |
|
|---|
| 105 | CREATE INDEX idx_olh_end_date ON Object_Location_History(end_date);
|
|---|
| 106 |
|
|---|
| 107 | --na koja kultura pripaga
|
|---|
| 108 | CREATE OR REPLACE VIEW Object_with_Culture AS
|
|---|
| 109 | SELECT
|
|---|
| 110 | o.object_id,
|
|---|
| 111 | o.title,
|
|---|
| 112 | c.name AS culture,
|
|---|
| 113 | cat.name AS category
|
|---|
| 114 | FROM Objects o
|
|---|
| 115 | JOIN Object_Classification oc ON oc.object_id = o.object_id
|
|---|
| 116 | JOIN Culture c ON c.culture_id = oc.culture_id
|
|---|
| 117 | JOIN Categories cat ON cat.category_id = oc.category_id;
|
|---|
| 118 |
|
|---|
| 119 | CREATE INDEX idx_oc_object_id ON Object_Classification(object_id);
|
|---|
| 120 |
|
|---|
| 121 | --koi predmeti na koi izlozbi
|
|---|
| 122 |
|
|---|
| 123 | CREATE OR REPLACE VIEW Exhibition_Objects AS
|
|---|
| 124 | SELECT
|
|---|
| 125 | e.exhibition_id,
|
|---|
| 126 | e.name AS exhibition,
|
|---|
| 127 | o.object_id,
|
|---|
| 128 | o.title AS object,
|
|---|
| 129 | i.name AS institution
|
|---|
| 130 | FROM Object_Exhibition oe
|
|---|
| 131 | JOIN Exhibitions e ON e.exhibition_id = oe.exhibition_id
|
|---|
| 132 | JOIN Objects o ON o.object_id = oe.object_id
|
|---|
| 133 | JOIN Institutions i ON i.institution_id = e.location_institution_id;
|
|---|
| 134 |
|
|---|
| 135 | CREATE INDEX idx_oe_exhibition_id ON Object_Exhibition(exhibition_id);
|
|---|
| 136 |
|
|---|
| 137 | CREATE 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 |
|
|---|
| 141 | CREATE OR REPLACE VIEW Research_Access_Details AS
|
|---|
| 142 | SELECT
|
|---|
| 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
|
|---|
| 150 | FROM Researcher_Access ra
|
|---|
| 151 | JOIN Users u ON u.user_id = ra.user_id
|
|---|
| 152 | JOIN Objects o ON o.object_id = ra.object_id
|
|---|
| 153 | JOIN Institutions i ON i.institution_id = ra.institution_id
|
|---|
| 154 | JOIN Status_Types s ON s.status_id = ra.access_status_id
|
|---|
| 155 | WHERE s.type = 'access';
|
|---|
| 156 |
|
|---|
| 157 | CREATE INDEX idx_ra_user_id ON Researcher_Access(user_id);
|
|---|
| 158 |
|
|---|
| 159 | CREATE INDEX idx_ra_object_id ON Researcher_Access(object_id);
|
|---|
| 160 |
|
|---|
| 161 | ANALYZE Researcher_Access;
|
|---|
| 162 |
|
|---|
| 163 | CREATE INDEX idx_ra_institution_id ON Researcher_Access(institution_id);
|
|---|
| 164 |
|
|---|
| 165 | ANALYZE Objects;
|
|---|
| 166 |
|
|---|
| 167 | CREATE INDEX IF NOT EXISTS idx_tsl_treatment
|
|---|
| 168 | ON Treatment_Step_Log(treatment_id);
|
|---|
| 169 |
|
|---|
| 170 | CREATE INDEX IF NOT EXISTS idx_treatments_object
|
|---|
| 171 | ON Treatments(object_id);
|
|---|
| 172 |
|
|---|
| 173 | CREATE INDEX IF NOT EXISTS idx_tsl_treatment_user_step
|
|---|
| 174 | ON Treatment_Step_Log(treatment_id, performed_by_user, step_number);
|
|---|
| 175 |
|
|---|
| 176 | CREATE INDEX idx_ra_user_object
|
|---|
| 177 | ON Researcher_Access(user_id, object_id);
|
|---|
| 178 |
|
|---|
| 179 | --tretmani vrz odreden objekt
|
|---|
| 180 |
|
|---|
| 181 | CREATE OR REPLACE VIEW Treatment_History AS
|
|---|
| 182 | SELECT
|
|---|
| 183 | t.object_id,
|
|---|
| 184 | o.title,
|
|---|
| 185 | t.treatment_date,
|
|---|
| 186 | tsl.step_number,
|
|---|
| 187 | tsl.step_description,
|
|---|
| 188 | u.full_name
|
|---|
| 189 | FROM Treatments t
|
|---|
| 190 | JOIN Objects o ON o.object_id = t.object_id
|
|---|
| 191 | JOIN Treatment_Step_Log tsl ON tsl.treatment_id = t.treatment_id
|
|---|
| 192 | JOIN Users u ON u.user_id = tsl.performed_by_user;
|
|---|
| 193 |
|
|---|
| 194 | --koj avtor koi publikacii gi napravil
|
|---|
| 195 |
|
|---|
| 196 | CREATE OR REPLACE VIEW Publications_with_Authors AS
|
|---|
| 197 | SELECT
|
|---|
| 198 | pa.publication_id,
|
|---|
| 199 | p.title,
|
|---|
| 200 | a.full_name AS author
|
|---|
| 201 | FROM Publication_Authors pa
|
|---|
| 202 | JOIN Publications p ON p.publication_id = pa.publication_id
|
|---|
| 203 | JOIN Authors a ON a.author_id = pa.author_id;
|
|---|
| 204 |
|
|---|
| 205 | CREATE INDEX idx_pa_publication_id ON Publication_Authors(publication_id);
|
|---|
| 206 |
|
|---|
| 207 | CREATE INDEX idx_pa_author_id ON Publication_Authors(author_id);
|
|---|
| 208 |
|
|---|
| 209 | ANALYZE Publication_Authors;
|
|---|
| 210 |
|
|---|
| 211 | ANALYZE Publications;
|
|---|