---
title: "Get SQLite table and index sizes with dbstat in C#"
url: https://recursiveai.org/onelite/cookbook/measure-sqlite-table-and-index-sizes/
description: "Get SQLite table and index sizes with dbstat in C#. Rank B-trees by allocated bytes, see unused bytes in each one, and explain the gap to total file size."
version: "1.0.0"
---

# Get SQLite table and index sizes with dbstat in C#

**OneLite** is a free, open-source SQLite-compatible database for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLite) · [GitHub](https://github.com/recursive-ai-oss/onelite-csharp) · [Docs](https://recursiveai.org/onelite/docs/) · [Download example (.zip)](https://recursiveai.org/onelite/cookbook/measure-sqlite-table-and-index-sizes/measure-sqlite-table-and-index-sizes.zip)

Use `dbstat` to see how much space each table and index has been allocated, and how much of that space is unused.

```shell
dotnet add package RecursiveAI.OneLite
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneLite;

// notes.sqlite3 is in the download.
using var db =
    Database.OpenReadOnly("notes.sqlite3");
using var conn = db.Connect();
using var query = conn.Prepare("""
    SELECT name, pgsize, unused
    FROM dbstat('main', 1)
    ORDER BY pgsize DESC, name
    """);
while (query.Step())
    Console.WriteLine($"{query.GetString(0)} | " +
        $"{query.GetInt64(1)} | {query.GetInt64(2)}");
```

[The example](https://recursiveai.org/onelite/cookbook/measure-sqlite-table-and-index-sizes/measure-sqlite-table-and-index-sizes.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Prints the name, allocated bytes, and unused bytes, in that order:

```text
notes | 12288 | 234
notes_title | 4096 | 4065
sqlite_schema | 4096 | 3806
```

The sample has 4,096-byte pages, two notes with 6,000-byte bodies, and an index named `notes_title` on their short titles, `Draft` and `Final`.

## The full program

Pass another SQLite file with `dotnet run -- path/to/database.sqlite3`. The program adds column headings to the report.

```csharp
using RecursiveAI.OneLite;

using var db = Database.OpenReadOnly(args.FirstOrDefault() ?? "notes.sqlite3");
using var connection = db.Connect();
using var query = connection.Prepare("""
    SELECT name, pgsize, unused
    FROM dbstat('main', 1)
    ORDER BY pgsize DESC, name
    """);
Console.WriteLine("Name | Allocated bytes | Unused bytes");
while (query.Step())
{
    string? name = query.GetString(0);
    long allocated = query.GetInt64(1);
    long unused = query.GetInt64(2);
    Console.WriteLine($"{name} | {allocated} | {unused}");
}
```

```text
Name | Allocated bytes | Unused bytes
notes | 12288 | 234
notes_title | 4096 | 4065
sqlite_schema | 4096 | 3806
```

If the file can’t be opened read-only, the program fails before it reports any sizes.

## Variations

**Report only one index.** Replace the full program’s query text with this SQL:

```sql
SELECT name, pgsize, unused
FROM dbstat('main', 1)
WHERE name = 'notes_title'
```

```text
Name | Allocated bytes | Unused bytes
notes_title | 4096 | 4065
```

## Related recipes

- [Inspect a SQLite query plan in C#](https://recursiveai.org/onelite/cookbook/inspect-sqlite-query-plan/)
- [Shrink a SQLite file with incremental vacuum in C#](https://recursiveai.org/onelite/cookbook/shrink-sqlite-file-incremental-vacuum-csharp/)

## The short answer

Query `dbstat('main', 1)`. `main` selects the main database, and `1` selects aggregate mode: one row for each table or index B-tree, the page structure that stores its data. Use `pgsize` for allocated bytes, `unused` for the unused bytes inside those allocated pages, and `ORDER BY pgsize DESC` to see the biggest allocations first. OneLite provides `dbstat` directly; there’s no `CREATE VIRTUAL TABLE` step.

## What does SQLite dbstat count for a table or index?

[In aggregate mode](https://sqlite.org/dbstat.html#aggregated_data), `pageno` is the number of pages in the tree and `pgsize` is their combined size. The sample’s `notes` tree occupies three pages, including the overflow pages that hold body bytes that don’t fit in the table page. The `notes_title` index occupies a separate page, so its bytes don’t count toward the `notes` row. The `sqlite_schema` row is the space used by the database’s own schema table.

Use `unused` to see room inside a tree’s pages, including freeblocks and gaps. It isn’t a count of bytes you can immediately remove from the file: in the sample, most of the index page is unused, but the index still occupies a whole page. `payload` is a separate measurement of bytes used for cell payload, not the same as allocated bytes minus unused bytes.

## Why doesn’t dbstat add up to the database file size?

[`dbstat` reports B-tree pages](https://sqlite.org/dbstat.html), not every page in a database file. For example, pages on the freelist are available for reuse but aren’t assigned to any table or index. They can make the file bigger than the sum from `dbstat`. A separate write-ahead log also isn’t part of that sum. Use `dbstat` for allocations by object, not for the total bytes occupied by a database and its sidecar files.
