Query table size and tablespace size in Oracle

There are two meanings of table size. One is the amount of physical space allocated to a table, regardless of whether the space is used. You can query to get the number of bytes:

select segment_name, bytes 
from user_segments 
where segment_type = 'TABLE'; 
or
   Select Segment_Name, Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name

The actual space used by another table. Query like this:

analyze table emp compute statistics; 
select num_rows * avg_row_len 
from user_tables 
where table_name = 'EMP';

view the size of each tablespace
Select Tablespace_Name,Sum(bytes)/1024/1024 From Dba_Segments Group By Tablespace_Name

 

 

 

1. Check the remaining table space size
 
SELECT tablespace_name tablespace, sum(blocks*8192/1000000) remaining space M FROM dba_free_space GROUP BY tablespace_name;
 
2. Check the overall space of all tablespaces in the system
select b.name,sum(a.bytes/1000000)总空间 from v$datafile a,v$tablespace b where a.ts#=b.ts# group by b.name;

  1. Tools and methods to view tablespace information in Oracle database:

  Use the oracle enterprise manager console tool, which is the client tool of Oracle. This tool will be installed automatically when the Oracle server or client is installed. After the Oracle installation is completed on the Windows operating system, log in to the tool through the following method: Start menu— - Program - Oracle-OraHome92 - Enterprise Manager Console (click) - oracle enterprise manager console login - select the 'independent startup' radio box - 'OK' - 'oracle enterprise manager console, independent' - Select the 'instance name' to log in - pop up the 'database connection information' - enter the 'username/password' (usually use the sys user), select SYSDBA for 'connection identity' - 'OK', then you have successfully logged in In this tool, select 'storage' - table space, you will see the following interface, which displays the table space name, table space type, area management type, table space size in "mega", and used tables Space size and table space utilization.

  Figure 1 Table space size and usage

  2. The command method to view the tablespace information in the Oracle database:

  Obtain the relevant information of the table space by querying the data dictionary tables in the database system. First, use the client tool to connect to the database. These tools can be SQLPLUS character tools, TOAD, PL/SQL, etc., and execute after connecting to the database. The following query statement:

  select

  a.a1 tablespace name,

  c.c2 type,

  c.c3 District Management,

  b.b2/1024/1024 table space size M,

  (b.b2-a.a2)/1024/1024 used M,

  substr((b.b2-a.a2)/b.b2*100,1,5) 利用率

  from

  (select tablespace_name a1, sum(nvl(bytes,0)) a2 from dba_free_space group by tablespace_name) a,

  (select tablespace_name b1,sum(bytes) b2 from dba_data_files group by tablespace_name) b,

  (select tablespace_name c1,contents c2,extent_management c3 from dba_tablespaces) c

  where a.a1=b.b1 and c.c1=b.b1;

  该语句通过查询dba_free_space,dba_data_files,dba_tablespaces这三个数据字典表,得到了表空间名称,表空间类型,区管理类型,以”兆”为单位的表空间大小,已使用的表空间大小及表空间利用率。dba_free_space表描述了表空间的空闲大小,dba_data_files表描述了数据库中的数据文件,dba_tablespaces表描述了数据库中的表空间。

  上面语句中from子句后有三个select语句,每个select语句相当于一个视图,视图的名称分别为a、b、c,通过它们之间的关联关系,我们得到了表空间的相关信息。

  语句执行结果如下:

  

表空间名称 类型 区管理 表空间大小M 已使用M 利用率
CWMLITE PERMANENT LOCAL 20 9.375 46.87
DRSYS PERMANENT LOCAL 20 9.6875 48.43
EXAMPLE PERMANENT LOCAL 149.375 149.25 99.91
INDX PERMANENT LOCAL 25 0.0625 0.25
ODM PERMANENT LOCAL 20 9.375 46.87
SYSTEM PERMANENT LOCAL 400 397.375 99.34
TOOLS PERMANENT LOCAL 10 6.0625 60.62
UNDOTBS1 UNDO LOCAL 200 5.9375 2.968
USERS PERMANENT LOCAL 25 0.0625 0.25
XDB PERMANENT LOCAL 38.125 37.9375 99.5
  The tool method and command method for viewing tablespace information in the Oracle database are introduced in the above description.

 

 

  1. Tools and methods to view data file information in Oracle database:

  Use the method described above to log in to the oracle enterprise manager console tool, select 'storage' - data file, and you will see the following interface, which displays the data file name, table space name, and data files in "mega" units size, used data file size, and data file utilization.

  

  Figure 2 Data file size and usage

        2. The command method to view the data file information in the Oracle database:

  Obtain relevant information of data files by querying the data dictionary tables in the database system. First, use client tools to connect to the database. These tools can be SQLPLUS character tools, TOAD, PL/SQL, etc., and execute after connecting to the database. The following query statement:

  select

  b.file_name physical file name,

  b.tablespace_name tablespace,

  b.bytes/1024/1024 size M,

  (b.bytes-sum(nvl(a.bytes,0)))/1024/1024 M used,

  substr((b.bytes-sum(nvl(a.bytes,0)))/(b.bytes)*100,1,5) 利用率

  from dba_free_space a,dba_data_files b

  where a.file_id=b.file_id

  group by b.tablespace_name,b.file_name,b.bytes

  order by b.tablespace_name

  The statement execution result is as follows:

  

physical file name tablespace size M M used Utilization
G:/ORACLE/ORADATA/ORA92/CWMLITE01.DBF CWMLITE 20 9.375 46.87
G:/ORACLE/ORADATA/ORA92/DRSYS01.DBF DRSYS 20 9.6875 48.43
G:/ORACLE/ORADATA/ORA92/EXAMPLE01.DBF EXAMPLE 149.375 149.25 99.91
G:/ORACLE/ORADATA/ORA92/INDX01.DBF INDX 25 0.0625 0.25
G:/ORACLE/ORADATA/ORA92/ODM01.DBF ODM 20 9.375 46.87
G:/ORACLE/ORADATA/ORA92/SYSTEM01.DBF SYSTEM 400 397.375 99.34
G:/ORACLE/ORADATA/ORA92/TOOLS01.DBF TOOLS 10 6.0625 60.62
G:/ORACLE/ORADATA/ORA92/UNDOTBS01.DBF UNDOTBS1 200 5.9375 2.968
G:/ORACLE/ORADATA/ORA92/USERS01.DBF USERS 25 0.0625 0.25
G:/ORACLE/ORADATA/ORA92/XDB01.DBF XDB 38.125 37.9375 99.5
  The above description respectively introduces the tool method and command method for viewing data file information in the Oracle database.

 

 

  In the Oracle database, the temporary table space is mainly used for the temporary work space that users need when sorting and summarizing using order by and group by statements. To query the name, size and data files of the temporary tablespace in the database, you can query the data dictionary dba_tablespaces and dba_data_files. The command is as follows:

  select

  a.talbespace_name tablespace name,

  b.bytes Large and small bytes,

  b.file_name data file name

  from dba_tablespaces a, dba_data_files b

  Where a.talbespace_name=b.talbespace_name and a.contents=’TEMPORARY’;

  The query results are as follows:

  tablespace name size bytes data file name

  TEMPONLINEG:/ORACLE/ORADATA/ORA92/TEMP01.DBF

  Starting from oracle 9i, it is possible to create a Temporary tablespace class table space, that is, a "temporary" table space, which uses temporary files. Information about temporary files is stored in the data dictionary V$tempfile. The command is as follows:

  Select file#,status,name from V$tempfile;

  The result of querying the data dictionary V$tempfile is as follows:

  

FILE# status NAME
1 ONLINE G:/ORACLE/ORADATA/ORA92/TEMP01.DBF
  Among the methods described above, it is recommended to master the command method, because your environment may not have graphical tools, and SQLPLUS is generally available. With the command script, it is easy to get the relevant information of the tablespace and data files. In addition, database administrators should organize more command scripts and execute scripts directly when needed to improve work efficiency.

 

  In the daily work of the database administrator, the utilization rate of the table space should be frequently inquired, and the growth of the table space should be estimated according to the specific conditions of the database system. When the utilization rate of the table space exceeds 90%, measures should be taken in time, such as clearing the history Tables and historical data to release space, add new data files to the table space, expand the size of existing data files and other methods to reduce the utilization of the table space, to avoid the error of insufficient space when the utilization of the table space is close to 100% .

 

1. Query the usage of oracle tablespace

 select b.file_id  文件ID,
  b.tablespace_name  表空间,
  b.file_name     物理文件名,
  b.bytes       总字节数,
  (b.bytes-sum(nvl(a.bytes,0)))   已使用,
  sum(nvl(a.bytes,0))        剩余,
  sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
  from dba_free_space a,dba_data_files b
  where a.file_id=b.file_id
  group by b.tablespace_name,b.file_name,b.file_id,b.bytes
  order by b.tablespace_name

 

2.查询oracle系统用户的默认表空间和临时表空间

select default_tablespace,temporary_tablespace from dba_users

 

 3.查询单张表的使用情况

select segment_name,bytes from dba_segments where segment_name = 'RE_STDEVT_FACT_DAY' and owner = USER

RE_STDEVT_FACT_DAY是您要查询的表名称

 

4.查询所有用户表使用大小的前三十名

select * from (select segment_name,bytes from dba_segments where owner = USER order by bytes desc ) where rownum <= 30

 

5.查询当前用户默认表空间的使用情况

select tablespacename,sum(totalContent),sum(usecontent),sum(sparecontent),avg(sparepercent) 
from 
(
SELECT b.file_id as id,b.tablespace_name as tablespacename,b.bytes as totalContent,(b.bytes-sum(nvl(a.bytes,0))) as usecontent,sum(nvl(a.bytes,0)) as sparecontent,sum(nvl(a.bytes,0))/(b.bytes)*100  as sparepercent 
FROM dba_free_space a,dba_data_files b
WHERE a.file_id=b.file_id and b.tablespace_name = (select default_tablespace from dba_users where username = user)  
group by b.tablespace_name,b.file_name,b.file_id,b.bytes

GROUP BY tablespacename

 

6.查询用户表空间的表

select   *  from user_tables

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484342&siteId=291194637