JBAS-1263 Browse entry cleanup utility SQL
authorBill Erickson <berickxx@gmail.com>
Wed, 2 Mar 2016 21:34:50 +0000 (16:34 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
SQL to delete un-referenced (dangling) browse entries.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/utility-scripts/browse_cleanup/delete-stale-browse-entries.sql [new file with mode: 0644]

diff --git a/KCLS/utility-scripts/browse_cleanup/delete-stale-browse-entries.sql b/KCLS/utility-scripts/browse_cleanup/delete-stale-browse-entries.sql
new file mode 100644 (file)
index 0000000..a46672d
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+Remove all browse entries that are not linked to either a bib record
+(via metabib.browse_*_entry_def_map) or an authority record (via 
+(metabib.browse_*_entry_simple_heading_map).
+
+Stale browse entries are never removed by the code.  They just languish.
+Run this script after a browse entry re-ingest, particularly if browse
+indexes change, resulting in a lot of new browse entries.
+*/
+
+DELETE FROM metabib.browse_author_entry WHERE id IN (
+    SELECT DISTINCT(entry.id)
+    FROM metabib.browse_author_entry entry
+        LEFT JOIN metabib.browse_author_entry_def_map map 
+            ON (map.entry = entry.id)
+        LEFT JOIN metabib.browse_author_entry_simple_heading_map hmap 
+            ON (hmap.entry = entry.id)
+    WHERE map.id IS NULL AND hmap.id IS NULL
+);
+
+DELETE FROM metabib.browse_author_entry WHERE id IN (
+    SELECT DISTINCT(entry.id)
+    FROM metabib.browse_author_entry entry
+        LEFT JOIN metabib.browse_author_entry_def_map map 
+            ON (map.entry = entry.id)
+        LEFT JOIN metabib.browse_author_entry_simple_heading_map hmap 
+            ON (hmap.entry = entry.id)
+    WHERE map.id IS NULL AND hmap.id IS NULL
+);
+
+DELETE FROM metabib.browse_series_entry WHERE id IN (
+    SELECT DISTINCT(entry.id)
+    FROM metabib.browse_series_entry entry
+        LEFT JOIN metabib.browse_series_entry_def_map map 
+            ON (map.entry = entry.id)
+        LEFT JOIN metabib.browse_series_entry_simple_heading_map hmap 
+            ON (hmap.entry = entry.id)
+    WHERE map.id IS NULL AND hmap.id IS NULL
+);
+
+DELETE FROM metabib.browse_title_entry WHERE id IN (
+    SELECT DISTINCT(entry.id)
+    FROM metabib.browse_title_entry entry
+        LEFT JOIN metabib.browse_title_entry_def_map map 
+            ON (map.entry = entry.id)
+        LEFT JOIN metabib.browse_title_entry_simple_heading_map hmap 
+            ON (hmap.entry = entry.id)
+    WHERE map.id IS NULL AND hmap.id IS NULL
+);
+
+
+DELETE FROM metabib.browse_call_number_entry WHERE id IN (
+    SELECT DISTINCT(entry.id)
+    FROM metabib.browse_call_number_entry entry
+        LEFT JOIN metabib.browse_call_number_entry_def_map map 
+            ON (map.entry = entry.id)
+    WHERE map.id IS NULL
+    -- no simple_heading_map for call numbers
+);
+