Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

Determine Size of a Table in SQL Server Xác định Kích thước của một bàn trong SQL Server

Ever wonder how big a table really is in your database? Đã bao giờ tự hỏi làm thế nào lớn thực sự là một bảng trong cơ sở dữ liệu của bạn? You know there are a million rows in the table, but how much space is that really taking? Bạn biết có một triệu hàng trong bảng, nhưng không gian bao nhiêu là thực sự tham gia?

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. SQL Server cung cấp được xây dựng trong thủ tục được lưu trữ mà bạn có thể chạy một cách dễ dàng để hiển thị kích thước của một bảng, bao gồm kích thước của chỉ số ... mà có thể làm bạn ngạc nhiên.

Syntax: Cú pháp:

sp_spaceused 'Tablename' sp_spaceused 'tablename'

Here's an example of it in action: Dưới đây là một ví dụ về nó trong hành động:

Highly useful stuff. Rất hữu dụng cụ. It's always interesting to see that the indexes are bigger than the actual data. Nó luôn luôn thú vị để thấy rằng các chỉ số lớn hơn là các dữ liệu thực tế.

This article was originally written on 03/27/07 Tagged with: Bài viết này ban đầu được viết ngày 03/27/07 Tagged với: Database Cơ sở dữ liệu , , SQL Server SQL Server

Daily Email Updates Email cập nhật hàng ngày

You can get our how-to articles in your inbox each day for free. Bạn có thể làm thế nào chúng tôi vào các bài viết trong hộp thư của bạn mỗi ngày miễn phí. Just enter your name and email below: Chỉ cần nhập tên và email của bạn dưới đây:


Name: Tên:
Email: Email:

Comments (9) Bình luận (9)

  1. sMiGuMp sMiGuMp

    While this is a useful command for determining the space allocated to a table, keep in mind that sp_spaceused references the sysindexes system table to get the space attributes and sometimes what's stored there can get be out of synch with reality. Trong khi đây là một lệnh hữu ích cho việc xác định không gian được phân bổ cho một bảng, hãy nhớ rằng tài liệu tham khảo sp_spaceused hệ thống sysindexes bảng để có được những thuộc tính không gian và đôi khi là những gì được lưu trữ có thể lấy được trên đồng bộ với thực tế. If you see space numbers that are seemingly out of whack (especially for the 'unused' value), run a “DBCC UPDATEUSAGE” command against the table in question before running sp_spacesued (or against the entire database if you have time and aren't worried about stepping on anyone's toes). Nếu bạn thấy con số không gian mà dường như trên Whack (đặc biệt là cho các giá trị không dùng 'the'), chạy một "DBCC UPDATEUSAGE" chống lại lệnh của bảng trong câu hỏi trước khi chạy sp_spacesued (hay chống lại toàn bộ cơ sở dữ liệu nếu bạn có thời gian và không lo lắng về bước vào ngón chân của bất kỳ ai).

    This can automatically be done as an optional parameter within sp_spaceused… Điều này có thể tự động được thực hiện như một tham số tùy chọn trong sp_spaceused ...

    eg sp_spaceused 'Orders',true ví dụ như sp_spaceused 'Orders', true

    …or as separate commands as shown below. ... Hay như các lệnh riêng biệt như hình dưới đây.

    To update usage stats for the entire current database, the syntax is: Để cập nhật thống kê sử dụng cho toàn bộ cơ sở dữ liệu hiện tại, cú pháp là:
    DBCC UPDATEUSAGE (0) DBCC UPDATEUSAGE (0)

    For a specific, named database: Để có một cơ sở dữ liệu cụ thể được đặt tên,:
    DBCC UPDATEUSAGE ('InsertdDbNameHere') DBCC UPDATEUSAGE ( 'InsertdDbNameHere')

    For a specific table: Đối với một bảng cụ thể:
    DBCC UPDATEUSAGE ('InsertdDbNameHere','InsertdTableNameHere') DBCC UPDATEUSAGE ( 'InsertdDbNameHere', 'InsertdTableNameHere')

    You can always check BOL for all the details on DBCC UPDATEUSAGE Bạn luôn luôn có thể kiểm tra BOL cho tất cả các chi tiết trên DBCC UPDATEUSAGE

  2. Charles tucker Charles tucker

    Thank you, using query analyer and running procedure sp_spaceused is helpful, however, what if your database has close too 100 tables. Cảm ơn bạn, sử dụng analyer truy vấn và thủ tục chạy sp_spaceused là hữu ích, tuy nhiên, những gì nếu cơ sở dữ liệu của bạn đã đóng quá 100 bảng. Anyway to return the entire database size? Dù sao để trở về kích thước cơ sở dữ liệu toàn bộ?

  3. Rick Rick

    I just found this, it returns spaceused for each table: Tôi chỉ thấy điều này, nó trở spaceused cho mỗi bảng:

    EXEC sp_MSforeachtable @command1=”EXEC sp_spaceused '?'” EXEC sp_MSforeachtable @ command1 = "EXEC sp_spaceused '?"

  4. Sanjiv Sanjiv

    Charles sp_helpdb Charles sp_helpdb

  5. Brian Brian

    Rick – that is one of the most useful sql server commands of all time. Rick - đó là một trong những lệnh máy chủ sql hữu ích nhất mọi thời đại. You are the greatest. Bạn là lớn nhất.

  6. LNu76 LNu76

    I tried to use Ricks Tipp, but having 181 Tables in the Database didnt do it much good. Tôi cố gắng để sử dụng Ricks Tipp, nhưng có 181 Bàn trong didnt Cơ sở dữ liệu làm tốt hơn. Also, his Mechanic with sp_spaceused treated each Table as a standalone query, wich made it impossible to use in an automated Growth-of-Database Statistic. Ngoài ra, Cơ khí chế tạo của mình với sp_spaceused được điều trị mỗi bàn là một truy vấn độc lập, trong đó đã làm cho nó không thể sử dụng trong một tăng trưởng tự động-of-Cơ sở dữ liệu Thống kê.

    I stole/borrowed the logic of sp_spaceused and applied it to the following VIEW (wich I now add to a Statistics Table, wich can then be queried in several ways): Tôi lấy trộm / vay logic của sp_spaceused và áp dụng nó vào VIEW sau (mà tôi bây giờ thêm vào một Bảng kê, mà sau đó có thể được truy vấn theo một số cách):

    SELECT SELECT
    TableName, TableName,
    NumRows, Numrows,
    reservedpages *8192/1024 as TotalSpace, reservedpages * 8192/1024 như TotalSpace,
    pages * 8192/1024 as DataSpace, trang * 8192/1024 như DataSpace,
    (usedpages-pages)*8192/1024 as IndexSpace, (usedpages-trang) * 8192/1024 như IndexSpace,
    (reservedpages-usedpages)*8192/1024 as UnusedSpace (reservedpages-usedpages) * 8192/1024 như UnusedSpace

    FROM (SELECT FROM (SELECT
    t.[name] as tablename, t. [tên] như tablename,
    avg([rows]) as NumRows, avg ([hàng]) là numrows,
    sum(total_pages) as reservedpages, sum (total_pages) như reservedpages,
    sum(used_pages) as usedpages, sum (used_pages) như usedpages,
    sum( sum (
    CASE CASE
    When it.internal_type IN (202,204) Then 0 Khi it.internal_type NĂM (202.204) Sau đó, 0
    When a.type 1 Then a.used_pages Khi a.type 1 Sau đó a.used_pages
    When p.index_id Khi p.index_id

  7. LNu76 LNu76

    Bah, it got cut off, here is the rest: Bah, nó đã cắt bỏ, đây là phần còn lại:

    When p.index_id Khi p.index_id

  8. LNu76 LNu76

    When p.index_id Khi p.index_id

  9. LNu76 LNu76

    (smaller than sign – why doesnt it take it here?) 2 Then a.data_pages (nhỏ hơn so với đăng ký - tại sao doesnt nó mất nó ở đây?) 2 Sau đó a.data_pages
    Else 0 Else 0
    END) as pages END) như trang
    from sys.allocation_units as a Join sys.partitions as p on p.partition_id = a.container_id từ sys.allocation_units như là một sys.partitions Tham gia như trên p.partition_id p = a.container_id
    left join sys.internal_tables it on p.object_id = it.object_id còn tham gia sys.internal_tables nó trên p.object_id = it.object_id
    JOIN sys.tables as t on p.object_id=t.object_id JOIN sys.tables là t ngày p.object_id = t.object_id
    –WHERE t.name='mittra' -Mittra = 'WHERE t.name'
    group by t.[name]) as subselect nhóm bởi t. [tên]) là subselect


Leave a Comment Để lại một Thảo luận




Leave your Để lại của bạn friendly thân thiện comment here. bình luận ở đây.

If you have a computer help question, Nếu bạn có một câu hỏi giúp máy tính, click here to leave it on the forums bấm vào đây để lại nó trên các diễn đàn instead. thay vì.

Note: Your comment may not show up immediately on the site. Lưu ý: Ý kiến của bạn có thể không hiển thị ngay lập tức trên trang web.

Our Friends Bạn bè của chúng tôi
Getting Started Bắt đầu


About How-To Geek Giới How-To Geek
What Is That Process? Quá trình đó là gì?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
mDNSResponder.exe mDNSResponder.exe
wmpnscfg.exe wmpnscfg.exe
rundll32.exe rundll32.exe
wfcrun32.exe wfcrun32.exe
Ipoint.exe Ipoint.exe
Itype.exe Itype.exe
Wfica32.exe Wfica32.exe
Mobsync.exe Mobsync.exe
conhost.exe conhost.exe
Dpupdchk.exe Dpupdchk.exe Adobe_Updater.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. Bản quyền © 2006-2009 HowToGeek.com. All Rights Reserved. Tất cả các quyền.