---
title: "困扰n久的问题，得到解决：Oracle安装一久，就会非常缓慢，拖垮APServer"
author: "netfork"
date: "2011-03-22T00:41:33Z"
tags: []
categories: ["数据库"]
slug: "n-oracle-apserver"
draft: false
lastmod: "2011-03-22T00:41:33Z"
id: 970060
url: "https://netfork.iteye.com/blog/970060"
---

时间一久，网站的AP服务器访问DB的速度就会非常缓慢，只有重装数据库才能解决问题。  
近期又遭遇了上述问题，昨天尤甚，一度以为有小黑客侵犯，打开后台的access log，发现访问并不见多猛，每秒十几个的样子，就这样的量，已经把网站搞挂了。。。。  
烦烦烦！！！

反复google不得其解。偶然间发现DB所在的磁盘碎片太多了，整理。。。期待速度可以提高，结果，问题依旧！

再次google，发现有人提到了相同的问题，回答最多的是索引碎片，找到了索引rebuild的方法，使用analyze 命令分析索引，然后得到分析结果，发现真是应该rebuild一下了，把所有的索引rebuild完后，重启WEB服务器，性能明显提高，网站性能基本恢复了。。。

可以松一口气了吗？静观明晚八点以后！

 

 

贴一段索引监视的表和存储过程：

 

create table MONITORINDEX

(

  INDEX\_NAME  VARCHAR2(50),

  DEL\_LF\_ROWS NUMBER,

  LF\_ROWS     NUMBER,

  RATE        NUMBER(4,2),

  MONITORDATE DATE default sysdate not null

)

create or replace procedure analyzeindex is

  v\_sql varchar2(100);

begin

  for a in (select index\_name from all\_indexes where owner = USER) loop

    v\_sql := ' analyze index ' || a.index\_name || ' validate structure';

    execute immediate v\_sql;

    insert into monitorindex

      (index\_name, del\_lf\_rows, lf\_rows, rate)

      select name,

             del\_lf\_rows,

             lf\_rows,

             round(del\_lf\_rows * 100 / (lf\_rows + del\_lf\_rows), 2)

        from index\_stats;

  end loop;

end analyzeindex;

 

call analyzeindex();

 

create or replace procedure clearallindex is

  v\_sql varchar2(100);

begin

  for a in (select index\_name from all\_indexes where owner = USER) loop

    v\_sql := ' alter index ' || a.index\_name || ' rebuild';

    execute immediate v\_sql;

  end loop;

end clearallindex;

 

call clearallindex();