zabbix db 优化sql脚本

  1. --先检查
  2. --Count the amount of rows that would be deleted for all orphaned acknowledge entries
  3. SELECT COUNT(*) FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users);
  4. SELECT COUNT(*) FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events);
  5. --Count the amount of rows that would be deleted for orphaned alerts entries
  6. SELECT COUNT(*) FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions);
  7. SELECT COUNT(*) FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events);
  8. SELECT COUNT(*) FROM alerts WHERE NOT userid IN (SELECT userid FROM users);
  9. SELECT COUNT(*) FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);
  10. --Count the amount of rows that would be deleted for orphaned application entries that no longer map back to a host
  11. SELECT COUNT(*) FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts);
  12. --Count the amount of rows that would be deleted for orphaned auditlog details (such as logins)
  13. SELECT COUNT(*) FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog);
  14. SELECT COUNT(*) FROM auditlog WHERE NOT userid IN (SELECT userid FROM users);
  15. --Count the amount of rows that would be deleted for orphaned conditions
  16. SELECT COUNT(*) FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions);
  17. --Count the amount of rows that would be deleted for orphaned functions
  18. SELECT COUNT(*) FROM functions WHERE NOT itemid IN (SELECT itemid FROM items);
  19. SELECT COUNT(*) FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers);
  20. --Count the amount of rows that would be deleted for orphaned graph items
  21. SELECT COUNT(*) FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs);
  22. SELECT COUNT(*) FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items);
  23. --Count the amount of rows that would be deleted for orphaned host_profiles
  24. SELECT COUNT(*) FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts);
  25. SELECT COUNT(*) FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts);
  26. --Count the amount of rows that would be deleted for orphaned host macro's
  27. SELECT COUNT(*) FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts);
  28. -- Count the amount of rows that would be deleted for orphaned item data
  29. SELECT COUNT(*) FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts);
  30. SELECT COUNT(*) FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications);
  31. SELECT COUNT(*) FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items);
  32. -- Count the amount of rows that would be deleted for orphaned HTTP check data
  33. SELECT COUNT(*) FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest);
  34. SELECT COUNT(*) FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep);
  35. SELECT COUNT(*) FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items);
  36. SELECT COUNT(*) FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications);
  37. -- Count the amount of rows that would be deleted for orphaned maintenance data
  38. SELECT COUNT(*) FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);
  39. SELECT COUNT(*) FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups);
  40. SELECT COUNT(*) FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);
  41. SELECT COUNT(*) FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts);
  42. SELECT COUNT(*) FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);
  43. SELECT COUNT(*) FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods);
  44. -- Count the amount of rows that would be deleted for orphaned mappings
  45. SELECT COUNT(*) FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps);
  46. -- Count the amount of rows that would be deleted for orphaned media / user items
  47. SELECT COUNT(*) FROM media WHERE NOT userid IN (SELECT userid FROM users);
  48. SELECT COUNT(*) FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);
  49. SELECT COUNT(*) FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp);
  50. SELECT COUNT(*) FROM rights WHERE NOT id IN (SELECT groupid FROM groups);
  51. SELECT COUNT(*) FROM sessions WHERE NOT userid IN (SELECT userid FROM users);
  52. SELECT COUNT(*) FROM user_history WHERE NOT userid IN (SELECT userid FROM users);
  53. -- Count the amount of rows that would be deleted for orphaned screens
  54. SELECT COUNT(*) FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens);
  55. -- Count the amount of rows that would be deleted for orphaned events & triggers
  56. SELECT COUNT(*) FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers);
  57. SELECT COUNT(*) FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers);
  58. -- Count the amount of records in the history/trends table for items that no longer exist
  59. SELECT COUNT(itemid) FROM history WHERE itemid NOT IN (SELECT itemid FROM items);
  60. SELECT COUNT(itemid) FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items);
  61. SELECT COUNT(itemid) FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items);
  62. SELECT COUNT(itemid) FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items);
  63. SELECT COUNT(itemid) FROM history_sync WHERE itemid NOT IN (SELECT itemid FROM items);
  64. SELECT COUNT(itemid) FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items);
  65. SELECT COUNT(itemid) FROM trends WHERE itemid NOT IN (SELECT itemid FROM items);
  66. SELECT COUNT(itemid) FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items);
 
  1. --再删除
  2. --Delete all orphaned acknowledge entries
  3. DELETE FROM acknowledges WHERE NOT userid IN (SELECT userid FROM users);
  4. DELETE FROM acknowledges WHERE NOT eventid IN (SELECT eventid FROM events);
  5. --Delete orphaned alerts entries
  6. DELETE FROM alerts WHERE NOT actionid IN (SELECT actionid FROM actions);
  7. DELETE FROM alerts WHERE NOT eventid IN (SELECT eventid FROM events);
  8. DELETE FROM alerts WHERE NOT userid IN (SELECT userid FROM users);
  9. DELETE FROM alerts WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);
  10. --Delete orphaned application entries that no longer map back to a host
  11. DELETE FROM applications WHERE NOT hostid IN (SELECT hostid FROM hosts);
  12. --Delete orphaned auditlog details (such as logins)
  13. DELETE FROM auditlog_details WHERE NOT auditid IN (SELECT auditid FROM auditlog);
  14. DELETE FROM auditlog WHERE NOT userid IN (SELECT userid FROM users);
  15. --Delete orphaned conditions
  16. DELETE FROM conditions WHERE NOT actionid IN (SELECT actionid FROM actions);
  17. --Delete orphaned functions
  18. DELETE FROM functions WHERE NOT itemid IN (SELECT itemid FROM items);
  19. DELETE FROM functions WHERE NOT triggerid IN (SELECT triggerid FROM triggers);
  20. --Delete orphaned graph items
  21. DELETE FROM graphs_items WHERE NOT graphid IN (SELECT graphid FROM graphs);
  22. DELETE FROM graphs_items WHERE NOT itemid IN (SELECT itemid FROM items);
  23. --Delete orphaed host_profiles
  24. DELETE FROM hosts_profiles WHERE NOT hostid IN (SELECT hostid FROM hosts);
  25. DELETE FROM hosts_profiles_ext WHERE NOT hostid IN (SELECT hostid FROM hosts);
  26. --Delete orphaned host macro's
  27. DELETE FROM hostmacro WHERE NOT hostid IN (SELECT hostid FROM hosts);
  28. -- Delete orphaned item data
  29. DELETE FROM items WHERE hostid NOT IN (SELECT hostid FROM hosts);
  30. DELETE FROM items_applications WHERE applicationid NOT IN (SELECT applicationid FROM applications);
  31. DELETE FROM items_applications WHERE itemid NOT IN (SELECT itemid FROM items);
  32. -- Delete orphaned HTTP check data
  33. DELETE FROM httpstep WHERE NOT httptestid IN (SELECT httptestid FROM httptest);
  34. DELETE FROM httpstepitem WHERE NOT httpstepid IN (SELECT httpstepid FROM httpstep);
  35. DELETE FROM httpstepitem WHERE NOT itemid IN (SELECT itemid FROM items);
  36. DELETE FROM httptest WHERE applicationid NOT IN (SELECT applicationid FROM applications);
  37. -- Delete orphaned maintenance data
  38. DELETE FROM maintenances_groups WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);
  39. DELETE FROM maintenances_groups WHERE groupid NOT IN (SELECT groupid FROM groups);
  40. DELETE FROM maintenances_hosts WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);
  41. DELETE FROM maintenances_hosts WHERE hostid NOT IN (SELECT hostid FROM hosts);
  42. DELETE FROM maintenances_windows WHERE maintenanceid NOT IN (SELECT maintenanceid FROM maintenances);
  43. DELETE FROM maintenances_windows WHERE timeperiodid NOT IN (SELECT timeperiodid FROM timeperiods);
  44. -- Delete orphaned mappings
  45. DELETE FROM mappings WHERE NOT valuemapid IN (SELECT valuemapid FROM valuemaps);
  46. -- Delete orphaned media / user items
  47. DELETE FROM media WHERE NOT userid IN (SELECT userid FROM users);
  48. DELETE FROM media WHERE NOT mediatypeid IN (SELECT mediatypeid FROM media_type);
  49. DELETE FROM rights WHERE NOT groupid IN (SELECT usrgrpid FROM usrgrp);
  50. DELETE FROM rights WHERE NOT id IN (SELECT groupid FROM groups);
  51. DELETE FROM sessions WHERE NOT userid IN (SELECT userid FROM users);
  52. DELETE FROM user_history WHERE NOT userid IN (SELECT userid FROM users);
  53. -- Screens
  54. DELETE FROM screens_items WHERE screenid NOT IN (SELECT screenid FROM screens);
  55. -- Events & triggers
  56. DELETE FROM trigger_depends WHERE triggerid_down NOT IN (SELECT triggerid FROM triggers);
  57. DELETE FROM trigger_depends WHERE triggerid_up NOT IN (SELECT triggerid FROM triggers);
  58. -- Count the amount of records in the history/trends table for items that no longer exist
  59. DELETE FROM history WHERE itemid NOT IN (SELECT itemid FROM items);
  60. DELETE FROM history_uint WHERE itemid NOT IN (SELECT itemid FROM items);
  61. DELETE FROM history_log WHERE itemid NOT IN (SELECT itemid FROM items);
  62. DELETE FROM history_str WHERE itemid NOT IN (SELECT itemid FROM items);
  63. DELETE FROM history_sync WHERE itemid NOT IN (SELECT itemid FROM items);
  64. DELETE FROM history_text WHERE itemid NOT IN (SELECT itemid FROM items);
  65. DELETE FROM trends WHERE itemid NOT IN (SELECT itemid FROM items);
  66. DELETE FROM trends_uint WHERE itemid NOT IN (SELECT itemid FROM items);
 





posted @ 2016-08-17 18:57  betterman.com  阅读(385)  评论(0编辑  收藏  举报