数据库报错
GATHER_STATS_JOB encountered errors. Check the trace file.
Errors in file /opt/Oracle/diag/rdbms/dbserver1/dbserver1/trace/dbserver1_j003_10544.trc:
ORA-20011: Approximate NDV failed: ORA-01476: divisor is equal to zero
环境
ORACLE 11G R2
RedHat 5.3 FOR 64 BIT
解决
网上给出的结论是BUG。
Bug No: 6040840
Filed 09-MAY-2007 Updated 10-MAY-2007
Product Oracle Server - Enterprise Edition Product Version 9.2.0.8
Platform. AIX5L Based Systems (64-bit) Platform. Version No Data
Database Version 9.2.0.8 Affects Platforms Generic
Severity Severe Loss of Service Status Duplicate Bug. To Filer
Base Bug 5645718 Fixed in Product Version No Data
Problem statement:
DBMS_STATS.GATHER_TABLE_STATS FAILS WITH ORA-1476.
WORKAROUND: ----------- n/a . RELATED BUGS: ------------- Bug#5645718.
不过我的数据库版本是11G,应该不是这个BUG。
检查日志发现:
*** 2012-09-29 06:00:16.870
GATHER_STATS_JOB: GATHER_TABLE_STATS('"MIS"','"T_SALES_ORDER_ITEM"','""', ...)
ORA-20011: Approximate NDV failed: ORA-01476: divisor is equal to zero
检查T_SALES_ORDER_ITEM表发现该表select的时候也报错:
ORA-01476: divisor is equal to zero
查看表结构:
CREATE TABLE T_SALES_ORDER_ITEM
(
ID NUMBER(18) NOT NULL,
......
PREPAY_RATE NUMBER GENERATED ALWAYS AS (ROUND(TO_NUMBER(TO_CHAR("PREPAYMONEY"))*100/("PRICE"*"QUANTITY"),2))
......
最后 select price,quantity from T_SALES_ORDER_ITEM发现price有等于0的值!!!问题并不难解决,发现问题才是至关重要的。
修改PREPAY_RATE列,添加decode判断函数:
PREPAY_RATE NUMBER GENERATED ALWAYS AS (DECODE("PRICE",0,0,ROUND(TO_NUMBER(TO_CHAR("PREPAYMONEY"))*100/("PRICE"*"QUANTITY"),2)))