Programing

SQL에서 테이블 크기를 찾는 방법은 무엇입니까?

lottogame 2020. 11. 16. 07:45
반응형

SQL에서 테이블 크기를 찾는 방법은 무엇입니까?


SQL에서 테이블 크기를 찾는 방법은 무엇입니까?


SQL 서버 :-

sp_spaceused 'TableName'

또는 관리 스튜디오에서 : 테이블-> 속성-> 스토리지를 마우스 오른쪽 버튼으로 클릭합니다.

MySQL :-

SELECT table_schema, table_name, data_length, index_length FROM information_schema.tables

Sybase :-

sp_spaceused 'TableName'

오라클 :-오라클 내 에서 테이블 크기를 계산하는 방법


ratty와 Haim의 게시물 (댓글 포함)의 답변을 결합하여 SQL Server의 경우 지금까지 가장 우아한 것 같습니다.

-- DROP TABLE #tmpTableSizes
CREATE TABLE #tmpTableSizes
(
    tableName varchar(100),
    numberofRows varchar(100),
    reservedSize varchar(50),
    dataSize varchar(50),
    indexSize varchar(50),
    unusedSize varchar(50)
)
insert #tmpTableSizes
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"


select  * from #tmpTableSizes
order by cast(LEFT(reservedSize, LEN(reservedSize) - 4) as int)  desc

이렇게하면 예약 된 크기 순으로 모든 테이블 목록이 가장 큰 것에서 가장 작은 것 순으로 제공됩니다.


사용자 지정 이름 테이블 크기 (GB)를 찾기 위한 쿼리 ( https://stackoverflow.com/a/7892349/1737819 수정 ). 이것을 시도하고 'YourTableName'을 테이블 이름으로 바꾸십시오.

SELECT 
    t.NAME AS TableName,    
    p.rows AS RowCounts,
    CONVERT(DECIMAL,SUM(a.total_pages)) * 8 / 1024 / 1024 AS TotalSpaceGB, 
    SUM(a.used_pages)  * 8 / 1024 / 1024 AS UsedSpaceGB , 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024 / 1024 AS UnusedSpaceGB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME = 'YourTable'
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    UsedSpaceGB DESC, t.Name

SQL Server는 인덱스 크기를 포함하여 테이블 크기를 쉽게 표시하기 위해 실행할 수있는 기본 제공 저장 프로 시저를 제공합니다.

sp_spaceused ‘Tablename’

SQL Server provides a built-in stored procedure that you can run to easily show the size of a table, including the size of the indexes… which might surprise you.

Syntax:

 sp_spaceused ‘Tablename’

see in :

http://www.howtogeek.com/howto/database/determine-size-of-a-table-in-sql-server/


Do you by size mean the number of records in the table, by any chance? In that case:

SELECT COUNT(*) FROM your_table_name

I know that in SQL 2012 (may work in other versions) you can do the following:

  1. Right click on the database name in the Object Explorer.
  2. Select Reports > Standard Reports > Disk Usage by Top Tables.

That will give you a list of the top 1000 tables and then you can order it by data size etc.


And in PostgreSQL:

SELECT pg_size_pretty(pg_relation_size('tablename'));

Here's a simple query, if you are just trying to find the largest tables.

  -- Find largest table partitions
 SELECT top 20 obj.name, LTRIM (STR ( sz.in_row_data_page_count * 8, 15, 0) + ' KB') as Size, * FROM sys.dm_db_partition_stats sz
inner join sys.objects obj on obj.object_id = sz.object_id
  order by sz.in_row_data_page_count desc

SQL Server, nicely formatted table for all tables in KB/MB:

SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
    CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    t.Name

You may refer the answer by Marc_s in another thread, Very useful.

Get size of all tables in database

참고URL : https://stackoverflow.com/questions/3606366/how-to-find-the-size-of-a-table-in-sql

반응형