To get a table's size in SQL, query the database's system catalog or information schema. For MySQL, use SELECT table_name, ROUND((data_length + index_length) / 1024 / 1024, 2) AS size_mb FROM information_schema.tables WHERE table_schema = 'your_database' AND table_name = 'your_table';. For PostgreSQL, use SELECT pg_size_pretty(pg_total_relation_size('your_table'));. For SQL Server, use sp_spaceused 'your_table';. For SQLite, use SELECT page_count * page_size AS size_bytes FROM pragma_page_count(), pragma_page_size(); after opening the database file. These queries return the total disk space used by the table, including indexes and overhead, which is what you need for capacity planning and troubleshooting.
What You'll Need
- Access to a SQL client (e.g., MySQL Workbench, pgAdmin, SQL Server Management Studio, or the sqlite3 CLI).
- Credentials with permission to read system catalogs or information schema (most users have this by default).
- The exact name of the database and table you want to measure.
- For SQLite, the path to the database file.
Step-by-Step
- Connect to your database. Open your SQL client and log in with a user that has read access to the target database. Menu labels vary by client and version, but the connection dialog is usually under File > Connect or a toolbar button.
- Identify the database and table names. If you are unsure, list all tables first. In MySQL:
SHOW TABLES FROM your_database;. In PostgreSQL:\dtin psql, orSELECT table_name FROM information_schema.tables WHERE table_schema = 'public';. In SQL Server:SELECT name FROM sys.tables;. In SQLite:.tables. - Run the size query for your database system. Choose the one that matches your platform:
- MySQL / MariaDB:
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' AND table_name = 'your_table';This sums data and index length. For InnoDB, data_length includes the clustered index, so the total is accurate. - PostgreSQL:
SELECT pg_size_pretty(pg_total_relation_size('your_table'));This returns a human-readable string like '12 MB'. For just the table without indexes, usepg_relation_size('your_table'). To include TOAST data (large values),pg_total_relation_sizealready covers it. - SQL Server:
EXEC sp_spaceused 'your_table';This returns rows, reserved, data, index_size, and unused space in KB. Alternatively, querysys.dm_db_partition_statsfor more control. - SQLite: Open the database file with
sqlite3 your_database.db, then runSELECT page_count * page_size AS size_bytes FROM pragma_page_count(), pragma_page_size();. This gives the total file size, not per-table. SQLite does not track per-table size directly; you can estimate by summing the sizes of all pages belonging to the table using the dbstat virtual table if enabled:SELECT SUM(pgsize) FROM dbstat WHERE name = 'your_table';.
- MySQL / MariaDB:
- Interpret the result. The number you get is the disk space consumed, not the logical data size. It includes fragmentation, free space within pages, and index overhead. For capacity planning, compare this to the disk quota or growth trends. If the size is unexpectedly large, check for bloat (especially in PostgreSQL) or unoptimized indexes.
- Automate if needed. To monitor size over time, save the query as a view or schedule it with a cron job or SQL Agent job. For example, in MySQL create a view:
CREATE VIEW table_sizes AS SELECT table_schema, table_name, ROUND((data_length + index_length) / 1024 / 1024, 2) AS size_mb FROM information_schema.tables;.
Common Mistakes
- Forgetting to include indexes. Many beginners query only data length and miss index size, which can be larger than the data itself. Always sum data and index lengths for a true total.
- Using the wrong schema. In MySQL,
table_schemais the database name. If you omit it, you may get sizes from other databases or no results. Always specify the schema. - Confusing logical and physical size. The size returned is physical disk usage. The logical size (sum of column lengths) is usually smaller. Do not use physical size to estimate backup size or export size without accounting for compression.
- Assuming SQLite has per-table size. SQLite stores all tables in one file. The
dbstatvirtual table must be compiled in (it usually is) and queried correctly; otherwise, you only get total file size. - Ignoring TOAST or overflow pages. In PostgreSQL, large text or bytea columns are stored in TOAST tables.
pg_total_relation_sizeincludes TOAST, butpg_relation_sizedoes not. Use the total function for accurate sizing.
Tips
- Use
ROUND(..., 2)to keep results readable. For very large tables, divide by 1024 three times to get GB. - In PostgreSQL,
pg_size_prettyautomatically formats bytes to KB, MB, GB. Use it for quick reads. - To compare multiple tables, remove the
table_namefilter and sort by size descending. This helps identify the largest tables for cleanup. - For SQL Server,
sp_spaceusedwith no parameter returns size for the entire database. Usesp_helpdbfor database-level details. - Regularly monitor table sizes to predict growth and avoid disk-full errors. Set up alerts if your database supports it.
Troubleshooting
- Query returns no rows. Check that the database and table names are spelled correctly and that you are connected to the right server. In MySQL, ensure the schema name is correct; in PostgreSQL, check the search_path or qualify the table with schema.
- Permission denied. Your user may lack SELECT permission on
information_schemaorpg_catalog. Ask an administrator to grant read access or use a user with higher privileges. - Size seems too small. You may be querying only data length without indexes, or the table is empty. Double-check the query includes index_length (MySQL) or uses
pg_total_relation_size(PostgreSQL). - SQLite dbstat not available. If
dbstatis not compiled in, you cannot get per-table size. Use the total file size as an upper bound, or export the table to a new database and measure that file. - Result differs from disk usage. File system block size and fragmentation can cause the on-disk size to be larger than the sum of table sizes. This is normal; the query reports allocated extents, not actual file system usage.
Frequently Asked Questions
Can I get the size of all tables at once?
Yes. Remove the table_name filter from the query. For MySQL: SELECT table_name, ROUND((data_length + index_length) / 1024 / 1024, 2) AS size_mb FROM information_schema.tables WHERE table_schema = 'your_database' ORDER BY size_mb DESC;. Similar adjustments work for other systems.
Does table size include indexes?
It depends on the query. In MySQL, data_length + index_length includes indexes. In PostgreSQL, pg_total_relation_size includes indexes and TOAST; pg_relation_size does not. In SQL Server, sp_spaceused reports index_size separately, so add it to data for total.
Why is my table size larger than the sum of its rows?
Because of storage overhead: page headers, free space, fragmentation, and index structures. Databases allocate space in fixed-size pages (e.g., 8KB or 16KB), and not all space is used by row data. Regular maintenance like OPTIMIZE TABLE (MySQL) or VACUUM (PostgreSQL) can reclaim some space.
How do I find the size of a table in SQLite?
SQLite stores all tables in a single file, so per-table size is not directly available unless you use the dbstat virtual table: SELECT SUM(pgsize) FROM dbstat WHERE name = 'your_table';. If dbstat is not enabled, you can only get the total database file size.
Can I query table size without special permissions?
Most databases allow regular users to read information_schema or pg_catalog. If you get a permission error, ask your DBA to grant SELECT on the relevant system views. In SQL Server, sp_spaceused requires membership in the db_owner or db_datareader role, or explicit permission.