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);

来自为知笔记(Wiz)

时间: 2024-10-06 13:55:47

zabbix db 优化sql脚本的相关文章

闲谈SQL脚本优化

摘要: 闲来无事,便想寻找大师级别优化SQL脚本案例,也算是读后留点笔记,摘录内容都会链接到所参考网址,如有冒犯,还望博主见谅:有些文章只有多动手多动脑才能理解其中的意思,看了需要实际操作,才不枉大师们的良苦用心: 一.正确的建立索引 [摘录:我是如何在SQLServer中处理每天四亿三千万记录的] 首先,我们需要明白几个索引的要点: 索引之后,按索引字段重复最少的来排序,会达到最优的效果.以我们的表来说,如果建立了No的聚集索引,把No放在where子句的第一位是最佳的,其次是Id,然后是Mg

[转]Oracle DB 通过SQL 优化管理性能

? 将SQL 优化指导用于: – 确定使用资源最多的 SQL 语句 – 优化使用资源最多的 SQL 语句 ? 使用SQL 访问指导优化工作量 SQL 优化 SQL 优化进程 ? 确定没有很好地优化的SQL 语句. ? 优化各条语句. ? 优化整个应用程序. 一般情况下,效果最明显的优化工作是SQL 优化.没有很好地优化的SQL 会不必要地使用过多资源.这种低效率会降低可伸缩性.使用更多的OS 和数据库资源并增加响应时间.要对没有很好地优化的SQL 语句进行优化,必须先确定这些语句,然后再进行优化

zabbix数据库优化脚本

分享一个zabbix数据库的优化脚本,适合2.0版本. 对history,hostory_uint按日分区,trends,trends_uint按月分区: 关闭Houserkeeper: vim zabbix_server.conf DisableHousekeeper=1 对history,hostory_uint每日建立第二天的分区并删除45天前的分区 对trends,trends_uint每月20号建立下一个月的分区,并删除12个月前的分区 时间可以自己修改 由于events表的大小对仪表

如何用 SQL Tuning Advisor (STA) 优化SQL语句

在Oracle10g之前,优化SQL是个比较费力的技术活,不停的分析执行计划,加hint,分析统计信息等等.在10g中,Oracle推出了自己的SQL优化辅助工具: SQL优化器(SQL Tuning Advisor :STA),它是新的DBMS_SQLTUNE包.使用STA一定要保证优化器是CBO模式下. 执行DBMS_SQLTUNE包进行sql优化需要有advisor的权限: SQL> create user dave identified by dave; 用户已创建. SQL> gra

50种方法优化SQL Server数据库查询(转载)

原文地址:http://www.cnblogs.com/zhycyq/articles/2636748.html 查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 5.网络速度慢 6.查询出的数据量过大(可以采用多次查询,其他的方法降低数据量) 7.锁或者死锁(这也是查询慢最常见的问题,是程序设计的缺陷) 8.sp_lock,sp_who,活动的用

在EA中将画出的ER图转换成SQL脚本

在进行数据库设计的时候,想着正好安装着EA软件呢,看能不能用EA画ER模型.结果发现不仅能画,而且还能进行整套数据库设计(生成SQL脚本). 下面以机房收费系统用户--学生为例.学生可以查看余额,可以查看上机记录,可以查看上机状态,可以查看充值记录,可以修改密码.步骤如下: (1)建立ER图之前,首先在EA中应选择使用哪种数据库.如图所示: (2)画学生用户的ER模型图,如图所示: (3)转换成数据模型图,选中所有的实体和属性名,右击,点击转换出现如图: (4)选择ERD to Data Mod

通过调整表union all的顺序优化SQL

操作系统:Windows XP 数据库版本:SQL Server 2005 今天遇到一个SQL,过滤条件是自动生成的,因此,没法通过调整SQL的谓词达到优化的目的,只能去找SQL中的“大表”.有一个视图返回的结果集比较大,如果能调整的话,也只能调整该视图了. 看了一下该视图的结构,里面还套用了另一层视图,直接看最里层视图的查询SQL. SELECT a.dfeesum_no , a.opr_amt - ISNULL(b.dec_pay, 0) - ISNULL(b.dec_corrpay, 0)

java程序执行SQL脚本文件

首先引入ibatis-common-2.jar包 import com.ibatis.common.jdbc.ScriptRunner; import com.ibatis.common.resources.Resources; jpetstore测试代码如下: package com.ibatis.jpetstore.test; import java.sql.DriverManager; import java.util.Properties; import com.ibatis.commo

sql脚本过大,无法打开的解决方法

从外网把数据库中的表结构以及数据用"导出脚本"的方式导出来(1.628G的样子),但是在本机执行sql脚本的时候,直接就是out of memory. 之前执行60M的脚本没出过这种问题,直接双击打开.sql脚本文件,然后执行就完了.现在主要是这文件太大了,就报错了,后面不停的google,终于找到答案了,使用sqlcmd命令,可完成这一工作. 具体操作如下: step1: 在查询分析器中执行下面的sql语句 select @@servername :获得数据库服务器名称. step2