如何查找Oracle中表上的索引占用的实际空间?


11

我想查找oracle 10g中的表上的索引占用的实际空间。我不打算包括oracle保留的空间以供将来使用。(不应该考虑oracle的开销。)我希望使用的字节而不是分配的字节。

你能帮助我前进吗?

另外,还有一种方法可以找到表中长字段的实际大小。

PS:vsize()和dbms_lob.getlength()不起作用。


您能否分享为什么这两种方法都不适合您?
jcolebrand

1
您应该单独问第二个问题。
Leigh Riffel

@好点。让我们看看用户是否意识到他们已经被迁移,并通过所有步骤在这里得到答案。代表他们指着我。
jcolebrand

Answers:


8
SELECT idx.index_name, SUM(bytes)
  FROM dba_segments seg,
       dba_indexes  idx
 WHERE idx.table_owner = <<owner of table>>
   AND idx.table_name  = <<name of table>>
   AND idx.owner       = seg.owner
   AND idx.index_name  = seg.segment_name
 GROUP BY idx.index_name

将显示每个索引实际消耗的空间量。我不清楚这是否正是您要考虑的开销类型,以及如何在索引的上下文中区分“已用”和“已分配”。如果要考虑索引中的可用空间,则可以使用DBMS_SPACE.SPACE_USAGE过程来确定索引中有多少个部分为空的块。


2
我相信OP理解分配给您的查询返回的内容(并且在a之后不会{自动}收缩delete <<name of table>>),而不是所使用的大小,该大小随索引中条目的数量而变化。
勒内Nyffenegger

2
无需访问DBA表就可以使用此命令: SELECT idx.index_name, SUM(bytes) FROM user_segments seg, user_indexes idx WHERE idx.table_name = 'EMERGE_REPORTING_DETAIL' AND idx.index_name = seg.segment_name GROUP BY idx.index_name
Richard Dingwall

@RichardDingwall-假设您以拥有该索引的Oracle用户身份登录,那么它将起作用。但是,如果您对使用多少空间感兴趣,那么您通常将以拥有该表的用户身份以外的用户身份登录。
贾斯汀·凯夫

6

为了衡量(我相信您对索引的理解)分配和使用的索引大小,我可能会使用 dbms_space

create or replace procedure tq84_index_size_proc 
as

  OBJECT_OWNER_in         varchar2(30) :=  user;
  OBJECT_NAME_in          varchar2(30) := 'TQ84_SIZE_IX';
  OBJECT_TYPE_in          varchar2(30) := 'INDEX';
  SAMPLE_CONTROL_in       number       :=  null;
  SPACE_USED_out          number;
  SPACE_ALLOCATED_out     number;
  CHAIN_PCENT_out         number;

  SUM_SEGMENT             number;

begin

  dbms_space.object_space_usage (
    OBJECT_OWNER           => OBJECT_OWNER_in        ,
    OBJECT_NAME            => OBJECT_NAME_in         ,
    OBJECT_TYPE            => OBJECT_TYPE_in         ,
    SAMPLE_CONTROL         => SAMPLE_CONTROL_in      ,
    SPACE_USED             => SPACE_USED_out         ,
    SPACE_ALLOCATED        => SPACE_ALLOCATED_out    ,
    CHAIN_PCENT            => CHAIN_PCENT_out
  );

  select sum(bytes) into SUM_SEGMENT 
    from user_segments
   where segment_name = OBJECT_NAME_in;


  dbms_output.put_line('Space Used:      ' || SPACE_USED_out);
  dbms_output.put_line('Space Allocated: ' || SPACE_ALLOCATED_out);
  dbms_output.put_line('Segment:         ' || SUM_SEGMENT);

end;
/

此过程测量名为* TQ84_SIZE_IX *的索引的分配和使用的大小。为了完整起见,我还添加了所报告的字节数user_segments

现在,可以看到以下过程:

create table tq84_size (
  col_1 varchar2(40),
  col_2 number
);

create index tq84_size_ix on tq84_size(col_1);

insert into tq84_size values ('*', 0);
commit;
exec tq84_index_size_proc;

索引中只有一个条目时,返回以下图:

Space Used:      1078
Space Allocated: 65536
Segment:         65536

填满索引...

insert into tq84_size 
select substr(object_name || object_type, 1, 40),
       rownum
  from dba_objects,
       dba_types
 where rownum < 500000;
commit;

...并再次获得数据...

exec tq84_index_size_proc;

...报告:

Space Used:      25579796
Space Allocated: 32505856
Segment:         32505856

然后,如果索引为“空”:

delete from tq84_size;
commit;
exec tq84_index_size_proc;

表明:

Space Used:      4052714
Space Allocated: 32505856
Segment:         32505856

这表明分配的大小不会缩小,而已使用的大小会缩小。


2

万一有人来这里寻找长场大小的方法,下面是一种方法。如果问题分开,我将删除此答案。

样本数据...

CREATE TABLE TLONG 
(
  C1 Number(3),
  C2 LONG 
);

INSERT INTO TLONG VALUES (1,'abcd');
INSERT INTO TLONG VALUES (2,'abc');
INSERT INTO TLONG VALUES (3,'ab');
INSERT INTO TLONG VALUES (4,'1234567890');

完成工作的功能...(对于生产,应该放在包装中)

CREATE OR REPLACE FUNCTION GetLongLength (pKey Number) RETURN Number Is
   vLong Long;
BEGIN
   SELECT C2 INTO vLong FROM TLONG WHERE C1 = pKey;
   Return Length(vLong);
END;
/

SHOW ERRORS;

测试功能...

SELECT rownum, GetLongLength(rownum) FROM dual CONNECT BY rownum<=4;

ROWNUM                 GETLONGLENGTH(ROWNUM)  
---------------------- ---------------------- 
1                      4                      
2                      3                      
3                      2                      
4                      10                   

0

我必须修改RenéNyffenegger的答案,以使其更通用并且更容易查看架构中所有索引的空间使用情况。

以为我会在这里分享修改后的代码,以防其他人发现它有用:

--==========================================
-- Show space usage by all indexes in schema
--==========================================
-- Required to show output in SQLDeveloper, which would supress it otherwise.
SET SERVEROUTPUT ON;
-- Calculates size for given index
CREATE OR REPLACE PROCEDURE calc_index_size(
    index_name IN VARCHAR2)
AS
  OBJECT_OWNER_in     VARCHAR2(30) := USER;
  OBJECT_NAME_in      VARCHAR2(30) := index_name;
  OBJECT_TYPE_in      VARCHAR2(30) := 'INDEX';
  SAMPLE_CONTROL_in   NUMBER       := NULL;
  SPACE_USED_out      NUMBER;
  SPACE_ALLOCATED_out NUMBER;
  CHAIN_PCENT_out     NUMBER;
  SUM_SEGMENT         NUMBER;
BEGIN
  dbms_space.object_space_usage ( OBJECT_OWNER => OBJECT_OWNER_in , OBJECT_NAME => OBJECT_NAME_in , OBJECT_TYPE => OBJECT_TYPE_in , SAMPLE_CONTROL => SAMPLE_CONTROL_in , SPACE_USED => SPACE_USED_out , SPACE_ALLOCATED => SPACE_ALLOCATED_out , CHAIN_PCENT => CHAIN_PCENT_out );
  SELECT SUM(bytes)
  INTO SUM_SEGMENT
  FROM user_segments
  WHERE segment_name = OBJECT_NAME_in;
  dbms_output.put_line('Space Used:      ' || ROUND(SPACE_USED_out     /1024/1024, 2) || 'MB');
  dbms_output.put_line('Space Allocated: ' || ROUND(SPACE_ALLOCATED_out/1024/1024) || 'MB');
  dbms_output.put_line('Segment:         ' || ROUND(SUM_SEGMENT        /1024/1024) || 'MB');
END;
/
-- Shows index size for all indexes in a schema
DECLARE
BEGIN
  FOR user_indexes_sorted_by_size IN
  (SELECT idx.index_name,
    SUM(bytes)/1024/1024 AS "Size(MB)"
  FROM user_segments seg,
    user_indexes idx
  WHERE idx.index_name = seg.segment_name
  GROUP BY idx.index_name
  ORDER BY "Size(MB)" DESC
  )
  LOOP
    dbms_output.put_line( user_indexes_sorted_by_size.index_name );
    dbms_output.put_line( '-------------------------------------' );
    calc_index_size(user_indexes_sorted_by_size.index_name);
    dbms_output.put_line( '' );
  END LOOP;
END;
--==========================================
--==========================================
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.