自动化维护任务 – Automated Maintenance Task (转)
1. Oracle有三个已定义好的automated maintenance tasks.
- Automatic Optimizer Statistics Collection—用于收集各种数据库对象的统计信息。这里又有三种模式:
- ‘ALL’ – Statistics are collected for all objects in the system
- ‘ORACLE’ – Statistics are collected for all Oracle owned objects
- ‘AUTO’ – Oracle decides for which objects to collect statistics
- 可以通过以下API进行设置
DBMS_STATS.SET_GLOBAL_PREFS ( pname VARCHAR2, pval VARCHAR2);
pname为”AUTOSTATS_TARGET”,pval为以上三个值之一。
- Automatic Segment Advisor—Identifies segments that have space available for reclamation, and makes recommendations on how to defragment those segments. You can also run the Segment Advisor manually to obtain more up-to-the-minute recommendations or to obtain recommendations on segments that the Automatic Segment Advisor did not examine for possible space reclamation.
- Automatic SQL Tuning Advisor—Examines the performance of high-load SQL statements, and makes recommendations on how to tune those statements. You can configure this advisor to automatically implement SQL profile recommendations.
2. 这些automated maintenace tasks在maintenance windows里得到执行。同样,Oracle已经定义好的7个windows,对应每周的每一天。周一到周五是从22:00到次日的02:00,周六和周日是从06:00到次日的02:00。
3. 如果不想这些automated maintenance tasks在一些windows里不被执行,或者完全不希望执行这些tasks。可以通过下面的API。
DBMS_AUTO_TASK_ADMIN.DISABLE(...)
4. 对于maintenance windows,系统会切换到相应的resource plan。如果你不想你原来的resource plan 被切换掉的话,记得在你设置resource plan时,在resource plan的名字前面加上force:。如下面所示:
ALTER SYSTEM SET RESOURCE_MANAGER_PLAN = 'FORCE:mydb_plan';
自动化是个好东西,不过这建立上你正确理解了它们的基础上,要不有时就会出现莫名其妙的结果了。
举个简单的例子:如果你在某个时候truncate了一个表,之后重新加载了海量的数据,如果在你truncate后,加载数据之前,收集统计信息的任务启动了,那么它会把这个表的统计信息更新为空表的数据。于是,你的很多涉及到这个表的SQL语句就会莫名其妙的慢下来甚至于执行不完了,原因就在于统计信息的错误变化(海量->空),导致CBO对很多语句从原来的索引访问者变成了致命的全表扫描了。