2 #pragma ident "%Z%%M% %I% %E% SMI"
4 # Run this TCL script using "testfixture" in order get a report that shows
5 # how much disk space is used by a particular data to actually store data
6 # versus how much space is unused.
9 # Get the name of the database to analyze
11 if {[llength $argv]!=1} {
12 puts stderr
"Usage: $argv0 database-name"
15 set file_to_analyze
[lindex $argv 0]
19 sqlite db
[lindex $argv 0]
20 set DB
[btree_open
[lindex $argv 0]]
22 # Output the schema for the generated report
26 CREATE TABLE space_used
(
27 name clob
, -- Name of a table or index in the database
file
28 is_index boolean
, -- TRUE
if it is an index
, false
for a table
29 payload int
, -- Total amount of data stored in this table or index
30 pri_pages int
, -- Number of primary pages used
31 ovfl_pages int
, -- Number of overflow pages used
32 pri_unused int
, -- Number of unused bytes on primary pages
33 ovfl_unused int
-- Number of unused bytes on overflow pages
36 # This query will be used to find the root page number for every index and
37 # table in the database.
40 SELECT name
, type
, rootpage FROM sqlite_master
42 SELECT 'sqlite_master'
, 'table'
, 2
46 # Initialize variables used for summary statistics.
51 set total_unused_primary
0
52 set total_unused_ovfl
0
54 # Analyze every table in the database, one at a time.
56 foreach {name type rootpage
} [db
eval $sql] {
57 set cursor
[btree_cursor
$DB $rootpage 0]
58 set go
[btree_first
$cursor]
64 set payload
[btree_payload_size
$cursor]
66 set stat
[btree_cursor_dump
$cursor]
67 set pgno
[lindex $stat 0]
68 set freebytes
[lindex $stat 4]
69 set pg_used
($pgno) $freebytes
71 set n
[expr {($payload-238+1019)/1020}]
73 incr unused_ovfl
[expr {$n*1020+238-$payload}]
75 set go
[btree_next
$cursor]
77 btree_close_cursor
$cursor
78 set n_primary
[llength [array names pg_used
]]
80 foreach x
[array names pg_used
] {incr unused_primary
$pg_used($x)}
81 regsub -all '
$name '' name
82 puts -nonewline "INSERT INTO space_used VALUES('$name'"
83 puts -nonewline ",[expr {$type=="index
"}]"
84 puts ",$size,$n_primary,$n_overflow,$unused_primary,$unused_ovfl);"
86 incr total_primary
$n_primary
87 incr total_overflow
$n_overflow
88 incr total_unused_primary
$unused_primary
89 incr total_unused_ovfl
$unused_ovfl
92 # Output summary statistics:
94 puts "-- Total payload size: $total_size"
95 puts "-- Total pages used: $total_primary primary and $total_overflow overflow"
96 set file_pgcnt
[expr {[file size
[lindex $argv 0]]/1024}]
97 puts -nonewline "-- Total unused bytes on primary pages: $total_unused_primary"
98 if {$total_primary>0} {
99 set upp
[expr {$total_unused_primary/$total_primary}]
100 puts " (avg $upp bytes/page)"
104 puts -nonewline "-- Total unused bytes on overflow pages: $total_unused_ovfl"
105 if {$total_overflow>0} {
106 set upp
[expr {$total_unused_ovfl/$total_overflow}]
107 puts " (avg $upp bytes/page)"
111 set n_free
[expr {$file_pgcnt-$total_primary-$total_overflow}]
112 if {$n_free>0} {incr n_free
-1}
113 puts "-- Total pages on freelist: $n_free"