Oracle – SQL information and quick database health checks
by Frank4DD, @2004
Check for database space issues
The DBA_FREE_SPACE data dictionary view shows the remaining space of a tablespace. This example shows the amount of free space for extents in tablespace 'EDACS01':
SELECT TABLESPACE_NAME, FILE_ID, BYTES, BLOCKS FROM DBA_FREE_SPACE WHERE TABLESPACE_NAME = 'EDACS01'; TABLESPACE_NAME FILE_ID BYTES BLOCKS ------------------------------ ---------- ---------- ---------- EDACS01 5 396296192 48376 |
The following query returns the name, size and number of extends of each table in schema 'EDACSADMIN'. It shows which table is extending most heavily.
SET LINE 120 COL TABLENAME FORMAT a20 SELECT SEGMENT_NAME "TABLENAME", TABLESPACE_NAME, BYTES, BLOCKS, EXTENTS FROM DBA_SEGMENTS WHERE SEGMENT_TYPE = 'TABLE' AND OWNER='EDACSADMIN' ORDER BY EXTENTS DESC; TABLENAME TABLESPACE_NAME BYTES BLOCKS EXTENTS -------------------- ------------------------------ ---------- ---------- ---------- EDACS_MAINLOG EDACS01 22020096 2688 21 EDACS_USERS EDACS01 1048576 128 16 EDACS_REMOTE EDACS01 1048576 128 16 EDACS_ROUTER EDACS01 1048576 128 16 EDACS_DAYSTATS EDACS01 1048576 128 16 EDACS_VERSION EDACS01 1048576 128 16 EDACS_MONSTATS EDACS01 1048576 128 16 EDACS_SERVICE EDACS01 1048576 128 16 EDACS_TEMPLOG EDACS01 2097152 256 2 |
SET LINE 120 COL INDEX FORMAT a20 SELECT SEGMENT_NAME "INDEX", TABLESPACE_NAME, BYTES, BLOCKS, EXTENTS FROM DBA_SEGMENTS WHERE SEGMENT_TYPE = 'INDEX' AND OWNER='EDACSADMIN' ORDER BY EXTENTS DESC; INDEX TABLESPACE_NAME BYTES BLOCKS EXTENTS -------------------- ------------------------------ ---------- ---------- ---------- MAINLOG_PK EDACS01 20971520 2560 20 TEMPLOG_PK EDACS01 20971520 2560 20 MONSTATS_PK EDACS01 1048576 128 16 VERSION_PK EDACS01 1048576 128 16 D_02 EDACS01 11534336 1408 11 D_01 EDACS01 9437184 1152 9 D_03 EDACS01 8388608 1024 8 USERS_PK EDACS01 5242880 640 5 REMOTE_PK EDACS01 5242880 640 5 ROUTER_PK EDACS01 5242880 640 5 SERVICE_PK EDACS01 5242880 640 5 DAYSTATS_PK EDACS01 2097152 256 2 |
select |
SQL> @checksize.sql
FILE_ID TABLESPACE_NAM FREE_MB SIZE_MB MAXSIZE_MB AE FREE_PCT
---------- -------------- ---------- ---------- ---------- --- ----------
5 EDACS01 377 500 500 NO 75
3 SYSAUX 32636 150 32767 YES 99
1 SYSTEM 32550 300 32767 YES 99
2 UNDOTBS1 32761 125 32767 YES 99
4 USERS 32767 5 32767 YES 99 |
In order to reduce the number of extents, whe recreate the index by setting the initial extent to the current index size. The current index size is found by:
SELECT SUM(BYTES) FROM DBA_EXTENTS WHERE SEGMENT_NAME = 'D_01'; SUM(BYTES) ---------- 9437184 |
DROP INDEX D_01; |
create index d_01 on edacs_mainlog(service, start_date) tablespace edacs01 |
For reorganizing, it is best to use a new tablespace. Then using the MOVE clause in the ALTER TABLE allows to change the tablespace and/or storage parameters of an Oracle table. Add the word ONLINE to the syntax and the table can be moved even while users are updating it.
/* ******************************************************************* ** cr_tblspc.sql: creates tablespace for e-dacs ** ** example: ** create tablespace edacs01 datafile '<path-to-dbfile>' size 500 M; */ create tablespace edacs02 datafile '/u02/oradata/work2/edacsdb02.dbf' size 500 M; |
ALTER TABLE EDACS_USERS MOVE |
create table EDACS_USERS_NEW |
The oldfasioned way is to use export/import to rebuild the tables from scratch.
oracle@debbie:~$ exp edacsadmin/xxx@work2 file=edacs_user_tbl.dmp tables=edacs_users Export: Release 10.1.0.2.0 - Production on Sun Nov 25 16:46:59 2007 Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to: Oracle Database 10g Release 10.1.0.2.0 - Production
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8ISO8859P1 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table EDACS_USERS 740 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings. |