--先检查--Count the amount of rows that would be deleted for all orphaned acknowledge entriesSELECT COUNT(*) FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users);SELECT COUNT(*) FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events);--Count the amount of rows that would be deleted for orphaned alerts entriesSELECT COUNT(*) FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions);SELECT COUNT(*) FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events);SELECT COUNT(*) FROM alerts WHERE NOT userid IN (SELECT userid FROM users);SELECT COUNT(*) FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);--Count the amount of rows that would be deleted for orphaned application entries that no longer map back to a hostSELECT COUNT(*) FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts);--Count the amount of rows that would be deleted for orphaned auditlog details (such as logins)SELECT COUNT(*) FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog);SELECT COUNT(*) FROM auditlog WHERE NOT userid IN (SELECT userid FROM users);--Count the amount of rows that would be deleted for orphaned conditionsSELECT COUNT(*) FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions);--Count the amount of rows that would be deleted for orphaned functionsSELECT COUNT(*) FROM functions WHERE NOT itemid IN (SELECT itemid FROM items);SELECT COUNT(*) FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers);--Count the amount of rows that would be deleted for orphaned graph itemsSELECT COUNT(*) FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs);SELECT COUNT(*) FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items);--Count the amount of rows that would be deleted for orphaned host_profilesSELECT COUNT(*) FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts);SELECT COUNT(*) FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts);--Count the amount of rows that would be deleted for orphaned host macro'sSELECT COUNT(*) FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts);-- Count the amount of rows that would be deleted for orphaned item dataSELECT COUNT(*) FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts);SELECT COUNT(*) FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications);SELECT COUNT(*) FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items);-- Count the amount of rows that would be deleted for orphaned HTTP check dataSELECT COUNT(*) FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest);SELECT COUNT(*) FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep);SELECT COUNT(*) FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items);SELECT COUNT(*) FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications);-- Count the amount of rows that would be deleted for orphaned maintenance dataSELECT COUNT(*) FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);SELECT COUNT(*) FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups);SELECT COUNT(*) FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);SELECT COUNT(*) FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts);SELECT COUNT(*) FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);SELECT COUNT(*) FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods);-- Count the amount of rows that would be deleted for orphaned mappingsSELECT COUNT(*) FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps);-- Count the amount of rows that would be deleted for orphaned media / user itemsSELECT COUNT(*) FROM media WHERE NOT userid IN (SELECT userid FROM users);SELECT COUNT(*) FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);SELECT COUNT(*) FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp);SELECT COUNT(*) FROM rights WHERE NOT id IN (SELECT groupid FROM groups);SELECT COUNT(*) FROM sessions WHERE NOT userid IN (SELECT userid FROM users);SELECT COUNT(*) FROM user_history WHERE NOT userid IN (SELECT userid FROM users);-- Count the amount of rows that would be deleted for orphaned screensSELECT COUNT(*) FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens);-- Count the amount of rows that would be deleted for orphaned events & triggersSELECT COUNT(*) FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers);SELECT COUNT(*) FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers);-- Count the amount of records in the history/trends table for items that no longer existSELECT COUNT(itemid) FROM history WHERE itemid NOT IN (SELECT itemid FROM items);SELECT COUNT(itemid) FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items);SELECT COUNT(itemid) FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items);SELECT COUNT(itemid) FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items);SELECT COUNT(itemid) FROM history_sync WHERE itemid NOT IN (SELECT itemid FROM items);SELECT COUNT(itemid) FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items);SELECT COUNT(itemid) FROM trends WHERE itemid NOT IN (SELECT itemid FROM items);SELECT COUNT(itemid) FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items);
--再删除--Delete all orphaned acknowledge entriesDELETE FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users);DELETE FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events);--Delete orphaned alerts entriesDELETE FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions);DELETE FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events);DELETE FROM alerts WHERE NOT userid IN (SELECT userid FROM users);DELETE FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);--Delete orphaned application entries that no longer map back to a hostDELETE FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts);--Delete orphaned auditlog details (such as logins)DELETE FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog);DELETE FROM auditlog WHERE NOT userid IN (SELECT userid FROM users);--Delete orphaned conditionsDELETE FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions);--Delete orphaned functionsDELETE FROM functions WHERE NOT itemid IN (SELECT itemid FROM items);DELETE FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers);--Delete orphaned graph itemsDELETE FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs);DELETE FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items);--Delete orphaed host_profilesDELETE FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts);DELETE FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts);--Delete orphaned host macro'sDELETE FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts);-- Delete orphaned item dataDELETE FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts);DELETE FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications);DELETE FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items);-- Delete orphaned HTTP check dataDELETE FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest);DELETE FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep);DELETE FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items);DELETE FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications);-- Delete orphaned maintenance dataDELETE FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);DELETE FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups);DELETE FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);DELETE FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts);DELETE FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);DELETE FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods);-- Delete orphaned mappingsDELETE FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps);-- Delete orphaned media / user itemsDELETE FROM media WHERE NOT userid IN (SELECT userid FROM users);DELETE FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);DELETE FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp);DELETE FROM rights WHERE NOT id IN (SELECT groupid FROM groups);DELETE FROM sessions WHERE NOT userid IN (SELECT userid FROM users);DELETE FROM user_history WHERE NOT userid IN (SELECT userid FROM users);-- ScreensDELETE FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens);-- Events & triggersDELETE FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers);DELETE FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers);-- Count the amount of records in the history/trends table for items that no longer existDELETE FROM history WHERE itemid NOT IN (SELECT itemid FROM items);DELETE FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items);DELETE FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items);DELETE FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items);DELETE FROM history_sync WHERE itemid NOT IN (SELECT itemid FROM items);DELETE FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items);DELETE FROM trends WHERE itemid NOT IN (SELECT itemid FROM items);DELETE FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items);