1 # Run this TCL script using "testfixture" in order get a report that shows
2 # how much disk space is used by a particular data to actually store data
3 # versus how much space is unused.
6 # Get the name of the database to analyze
8 if {[llength $argv]!=1} {
9 puts stderr "Usage: $argv0 database-name"
12 set file_to_analyze [lindex $argv 0]
16 sqlite db [lindex $argv 0]
17 set DB [btree_open [lindex $argv 0]]
19 # Output the schema for the generated report
23 CREATE TABLE space_used(
24 name clob, -- Name of a table or index in the database file
25 is_index boolean, -- TRUE if it is an index, false for a table
26 payload int, -- Total amount of data stored in this table or index
27 pri_pages int, -- Number of primary pages used
28 ovfl_pages int, -- Number of overflow pages used
29 pri_unused int, -- Number of unused bytes on primary pages
30 ovfl_unused int -- Number of unused bytes on overflow pages
33 # This query will be used to find the root page number for every index and
34 # table in the database.
37 SELECT name, type, rootpage FROM sqlite_master
39 SELECT 'sqlite_master', 'table', 2
43 # Initialize variables used for summary statistics.
48 set total_unused_primary 0
49 set total_unused_ovfl 0
51 # Analyze every table in the database, one at a time.
53 foreach {name type rootpage} [db eval $sql] {
54 set cursor [btree_cursor $DB $rootpage 0]
55 set go [btree_first $cursor]
61 set payload [btree_payload_size $cursor]
63 set stat [btree_cursor_dump $cursor]
64 set pgno [lindex $stat 0]
65 set freebytes [lindex $stat 4]
66 set pg_used($pgno) $freebytes
68 set n [expr {($payload-238+1019)/1020}]
70 incr unused_ovfl [expr {$n*1020+238-$payload}]
72 set go [btree_next $cursor]
74 btree_close_cursor $cursor
75 set n_primary [llength [array names pg_used]]
77 foreach x [array names pg_used] {incr unused_primary $pg_used($x)}
78 regsub -all ' $name '' name
79 puts -nonewline "INSERT INTO space_used VALUES('$name'"
80 puts -nonewline ",[expr {$type=="index"}]"
81 puts ",$size,$n_primary,$n_overflow,$unused_primary,$unused_ovfl);"
83 incr total_primary $n_primary
84 incr total_overflow $n_overflow
85 incr total_unused_primary $unused_primary
86 incr total_unused_ovfl $unused_ovfl
89 # Output summary statistics:
91 puts "-- Total payload size: $total_size"
92 puts "-- Total pages used: $total_primary primary and $total_overflow overflow"
93 set file_pgcnt [expr {[file size [lindex $argv 0]]/1024}]
94 puts -nonewline "-- Total unused bytes on primary pages: $total_unused_primary"
95 if {$total_primary>0} {
96 set upp [expr {$total_unused_primary/$total_primary}]
97 puts " (avg $upp bytes/page)"
101 puts -nonewline "-- Total unused bytes on overflow pages: $total_unused_ovfl"
102 if {$total_overflow>0} {
103 set upp [expr {$total_unused_ovfl/$total_overflow}]
104 puts " (avg $upp bytes/page)"
108 set n_free [expr {$file_pgcnt-$total_primary-$total_overflow}]
109 if {$n_free>0} {incr n_free -1}
110 puts "-- Total pages on freelist: $n_free"