In SQLite the size of the database file is the overall size of the database, but this is no开发者_高级运维t necessarily a correct indication of the actual space used. If data is deleted, the freed space is not returned to the operating system. One has to execute the VACUUM command to defragment the database and release unused space back to the operating system.
I am using the Devart ADO.Net data provider for SQLite.
Is there a way to obtain the actual used space of a SQLite database?
I found a pragma freelist_count, which according to the SQLite documentation provides the number of unused pages in the database file. This is not precise as far as the total free space within the database file, as there can still be many pages that are partially filled.
Nonetheless, the below still provides us with an accurate count of free pages and suffices for my needs in getting a rough estimate of the used versus free space within the database file.
PRAGMA freelist_count;
PRAGMA page_size;
Multiplying the values returned by the page_count and page_size pragmas might give you a good estimate of the size of the DB:
PRAGMA page_count;
PRAGMA page_size;
精彩评论