To get the size of a SQL table, you must query the database's system catalog or information schema, as there is no universal "SIZE" command across all SQL dialects. Depending on your system, you will typically query a metadata table (like information_schema.tables in MySQL or sys.allocation_units in SQL Server) to retrieve the data and index lengths in bytes or kilobytes.

What You'll Need

  • Access to a SQL database management tool (such as MySQL Workbench, pgAdmin, SQL Server Management Studio, or a command-line interface).
  • A user account with sufficient permissions to read system metadata or schema information.
  • The exact name of the table you wish to measure.

Step-by-Step

Because different database engines store metadata differently, follow the steps for the specific system you are using. Note that labels and system view names may vary slightly depending on the version or region of your installation.

For MySQL

  1. Open your SQL editor and connect to your database.
  2. Run the following query, replacing your_table_name and your_database_name with your actual names:
    SELECT 
        table_name AS "Table", 
        round(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)" 
        FROM information_schema.TABLES 
        WHERE table_schema = "your_database_name" 
        AND table_name = "your_table_name";
  3. Review the results. The data_length represents the actual row data, and index_length represents the space used by the table's indexes.

For PostgreSQL

  1. Open your query tool (such as pgAdmin).
  2. To get the size of the table only (excluding indexes), use:
    SELECT pg_size_pretty(pg_relation_size('your_table_name'));
  3. To get the total size including all indexes and toast data, use:
    SELECT pg_size_pretty(pg_total_relation_size('your_table_name'));
  4. The pg_size_pretty function automatically converts the byte count into a human-readable format (e.g., KB, MB, GB).

For Microsoft SQL Server

  1. Open SQL Server Management Studio (SSMS).
  2. The simplest method is to use the built-in stored procedure. Run:
    EXEC sp_spaceused 'your_table_name';
  3. Alternatively, for a more detailed view across multiple tables, query the system views:
    SELECT 
        t.name AS TableName, 
        s.Name AS SchemaName,
        p.rows AS RowCounts, 
        (SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB, 
        (SUM(a.used_pages) * 8) / 1024 AS UsedSpaceMB, 
        (SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024 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 = 'your_table_name'
        GROUP BY t.name, s.Name, p.rows;

Common Mistakes

  • Ignoring Indexes: A common error is checking only the data size. Indexes can often take up as much or more space than the actual data rows. Always check for "Total Size" if you are managing disk space.
  • Confusing Rows with Size: A table with 1 million rows of integers will be significantly smaller than a table with 1,000 rows of large text (BLOB/CLOB) fields. Never assume row count equals storage size.
  • Wrong Database Context: In MySQL, if you forget to specify the table_schema in the WHERE clause, you may get results for tables with the same name across different databases.
  • Using Outdated Metadata: Some systems cache statistics. If you have just deleted millions of rows, the system views might not update immediately.

Tips

  • Automate Monitoring: If you need to track growth, create a small "audit" table and schedule a job to insert the results of these size queries into it once a week.
  • Check for Bloat: In PostgreSQL, if the total size is vastly larger than the expected data size, you may have "bloat" (dead tuples). Running a VACUUM command can help reclaim this space.
  • Compare Tables: Remove the WHERE table_name = ... clause to see a list of all tables in your database sorted by size. This helps identify "space hogs" quickly.

Troubleshooting

If the query does not return the expected results or throws an error, try these steps in order:

  1. Verify Permissions: If you receive an "Access Denied" or "Permission Denied" error, ensure your user has SELECT permissions on the information_schema or sys views.
  2. Check Spelling and Case: SQL table names can be case-sensitive depending on the operating system and collation settings. Ensure the table name in your query matches the database exactly.
  3. Verify Database Selection: Ensure you are connected to the correct database. Use the USE database_name; command before running the size query.
  4. Update Statistics: If the size seems incorrect after a large data change, run ANALYZE TABLE table_name; (MySQL/Postgres) or UPDATE STATISTICS table_name; (SQL Server) to refresh the metadata.

Frequently Asked Questions

Why is my table size larger than the data I put in?

This is usually due to indexes, which create separate copies of certain columns to speed up searches. Additionally, databases often allocate space in "pages" or "extents," meaning they reserve more space than is currently used to allow for future growth.

Can I see the size of a specific column?

Standard SQL does not provide a simple command to see the size of a single column across all rows. You generally have to calculate the average length of the data in that column using functions like AVG(LENGTH(column_name)).

Does deleting rows immediately reduce the table size?

No. Most SQL databases do not shrink the physical file on disk immediately after a DELETE operation. They mark the space as "available" for new rows. To physically shrink the file, you typically need to run a command like OPTIMIZE TABLE (MySQL) or VACUUM FULL (PostgreSQL).

What is the difference between data size and total size?

Data size refers only to the actual records stored in the table. Total size includes the data plus all associated indexes and overhead (like TOAST tables in PostgreSQL), which provides a more accurate picture of disk usage.

Can I find the size of the entire database at once?

Yes. In MySQL, you can query information_schema.tables and SUM the data and index lengths for all tables within a specific schema. In SQL Server, you can right-click the database in SSMS and select Reports > Standard Reports > Disk Usage.