English 中文(简体)
True tablespace size in oracle
原标题:

I need to know true tablespace size in Oracle. I have some tablespace and I need to know how many space it uses now and how many space is free (and maybe percent of free space). I found in web some sqls but all of them showed size based on water mark... which is not true space allocated now but as far as I know the highest value which has ever been reached... So my real need is to know if I have enough space for my data which constantly are written and I must know how much of them I can store before having to delete some of them.

Thanks

问题回答

Try this:

-- Available space, by tablespace

SELECT * FROM
  (SELECT tablespace_name FROM dba_tablespaces)
LEFT OUTER JOIN
  (SELECT tablespace_name, SUM(bytes) AS total_bytes
     FROM dba_data_files
     GROUP BY tablespace_name)
  USING (tablespace_name)
LEFT OUTER JOIN
  (SELECT tablespace_name, sum(bytes) AS used_bytes
     from dba_segments
     GROUP BY tablespace_name)
  USING (tablespace_name)
LEFT OUTER JOIN
  (SELECT tablespace_name, SUM(bytes) AS free_bytes
     FROM dba_free_space
     GROUP BY tablespace_name)
  USING (tablespace_name);

If you want to get an idea of data file size including those files that can auto extend then try:

SELECT DISTINCT a.tablespace_name,
            sum(a.bytes)/1024/1024 CurMb,
            sum(decode(b.maxextend, null, a.bytes/1024/1024, b.maxextend*(SELECT value FROM v$parameter WHERE name= db_block_size )/1024/1024)) MaxMb,
            round(100*(sum(a.bytes)/1024/1024 - round(c.free/1024/1024))/(sum(decode(b.maxextend, null, a.bytes/1024/1024, b.maxextend*(SELECT value FROM v$parameter WHERE name= db_block_size )/1024/1024)))) UPercent,
            (sum(a.bytes)/1024/1024 - round(c.free/1024/1024)) TotalUsed,
            (sum(decode(b.maxextend, null, a.bytes/1024/1024, b.maxextend*(SELECT value FROM v$parameter WHERE name= db_block_size )/1024/1024)) - (sum(a.bytes)/1024/1024 - round(c.Free/1024/1024))) TotalFree 
FROM dba_data_files a,
   sys.filext$ b,
   (SELECT d.tablespace_name , sum(nvl(c.bytes,0)) free
     FROM dba_tablespaces d,dba_free_space c
     WHERE d.tablespace_name = c.tablespace_name(+) 
     GROUP BY d.tablespace_name) c
WHERE a.file_id = b.file#(+)
    AND a.tablespace_name = c.tablespace_name  
GROUP BY a.tablespace_name,
       c.free/1024

Hopefully, this would assist you,

 SELECT a.tablespace_name, a.file_name, a.bytes allocated_bytes,b.free_bytes FROM dba_data_files a,
(SELECT file_id, SUM(bytes) free_bytes FROM dba_free_space b GROUP BY file_id) b WHERE a.file_id=b.file_id
ORDER BY a.tablespace_name;




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签