drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 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. |
| 4 | # |
| 5 | |
drh | a7531c6 | 2006-01-24 02:19:53 +0000 | [diff] [blame] | 6 | if {[catch { |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 7 | # Get the name of the database to analyze |
| 8 | # |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 9 | proc usage {} { |
| 10 | set argv0 [file rootname [file tail [info nameofexecutable]]] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 11 | puts stderr "Usage: $argv0 database-name" |
| 12 | exit 1 |
| 13 | } |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 14 | set file_to_analyze {} |
| 15 | set flags(-pageinfo) 0 |
drh | a624fd5 | 2011-10-05 19:46:03 +0000 | [diff] [blame] | 16 | set flags(-stats) 0 |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 17 | append argv {} |
| 18 | foreach arg $argv { |
| 19 | if {[regexp {^-+pageinfo$} $arg]} { |
| 20 | set flags(-pageinfo) 1 |
drh | a624fd5 | 2011-10-05 19:46:03 +0000 | [diff] [blame] | 21 | } elseif {[regexp {^-+stats$} $arg]} { |
| 22 | set flags(-stats) 1 |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 23 | } elseif {[regexp {^-} $arg]} { |
| 24 | puts stderr "Unknown option: $arg" |
| 25 | usage |
| 26 | } elseif {$file_to_analyze!=""} { |
| 27 | usage |
| 28 | } else { |
| 29 | set file_to_analyze $arg |
| 30 | } |
| 31 | } |
| 32 | if {$file_to_analyze==""} usage |
drh | 453a312 | 2012-10-10 10:52:46 +0000 | [diff] [blame] | 33 | set root_filename $file_to_analyze |
| 34 | regexp {^file:(//)?([^?]*)} $file_to_analyze all x1 root_filename |
| 35 | if {![file exists $root_filename]} { |
| 36 | puts stderr "No such file: $root_filename" |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 37 | exit 1 |
| 38 | } |
drh | 453a312 | 2012-10-10 10:52:46 +0000 | [diff] [blame] | 39 | if {![file readable $root_filename]} { |
| 40 | puts stderr "File is not readable: $root_filename" |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 41 | exit 1 |
| 42 | } |
drh | 453a312 | 2012-10-10 10:52:46 +0000 | [diff] [blame] | 43 | set true_file_size [file size $root_filename] |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 44 | if {$true_file_size<512} { |
drh | 453a312 | 2012-10-10 10:52:46 +0000 | [diff] [blame] | 45 | puts stderr "Empty or malformed database: $root_filename" |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 46 | exit 1 |
| 47 | } |
| 48 | |
drh | 36c0632 | 2011-10-10 16:06:35 +0000 | [diff] [blame] | 49 | # Compute the total file size assuming test_multiplexor is being used. |
| 50 | # Assume that SQLITE_ENABLE_8_3_NAMES might be enabled |
| 51 | # |
drh | 453a312 | 2012-10-10 10:52:46 +0000 | [diff] [blame] | 52 | set extension [file extension $root_filename] |
| 53 | set pattern $root_filename |
drh | 3700262 | 2012-04-06 00:09:27 +0000 | [diff] [blame] | 54 | append pattern {[0-3][0-9][0-9]} |
drh | 36c0632 | 2011-10-10 16:06:35 +0000 | [diff] [blame] | 55 | foreach f [glob -nocomplain $pattern] { |
| 56 | incr true_file_size [file size $f] |
| 57 | set extension {} |
| 58 | } |
| 59 | if {[string length $extension]>=2 && [string length $extension]<=4} { |
drh | 453a312 | 2012-10-10 10:52:46 +0000 | [diff] [blame] | 60 | set pattern [file rootname $root_filename] |
drh | 3700262 | 2012-04-06 00:09:27 +0000 | [diff] [blame] | 61 | append pattern {.[0-3][0-9][0-9]} |
drh | 36c0632 | 2011-10-10 16:06:35 +0000 | [diff] [blame] | 62 | foreach f [glob -nocomplain $pattern] { |
| 63 | incr true_file_size [file size $f] |
| 64 | } |
| 65 | } |
| 66 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 67 | # Open the database |
| 68 | # |
drh | 453a312 | 2012-10-10 10:52:46 +0000 | [diff] [blame] | 69 | if {[catch {sqlite3 db $file_to_analyze -uri 1} msg]} { |
| 70 | puts stderr "error trying to open $file_to_analyze: $msg" |
| 71 | exit 1 |
| 72 | } |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 73 | register_dbstat_vtab db |
| 74 | |
drh | 565621a | 2011-09-21 20:10:42 +0000 | [diff] [blame] | 75 | db eval {SELECT count(*) FROM sqlite_master} |
drh | f08f384 | 2011-09-27 13:40:26 +0000 | [diff] [blame] | 76 | set pageSize [expr {wide([db one {PRAGMA page_size}])}] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 77 | |
drh | a624fd5 | 2011-10-05 19:46:03 +0000 | [diff] [blame] | 78 | if {$flags(-pageinfo)} { |
| 79 | db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat} |
| 80 | db eval {SELECT name, path, pageno FROM temp.stat ORDER BY pageno} { |
| 81 | puts "$pageno $name $path" |
| 82 | } |
| 83 | exit 0 |
| 84 | } |
| 85 | if {$flags(-stats)} { |
| 86 | db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat} |
| 87 | puts "BEGIN;" |
| 88 | puts "CREATE TABLE stats(" |
| 89 | puts " name STRING, /* Name of table or index */" |
| 90 | puts " path INTEGER, /* Path to page from root */" |
| 91 | puts " pageno INTEGER, /* Page number */" |
| 92 | puts " pagetype STRING, /* 'internal', 'leaf' or 'overflow' */" |
| 93 | puts " ncell INTEGER, /* Cells on page (0 for overflow) */" |
| 94 | puts " payload INTEGER, /* Bytes of payload on this page */" |
| 95 | puts " unused INTEGER, /* Bytes of unused space on this page */" |
| 96 | puts " mx_payload INTEGER, /* Largest payload size of all cells */" |
| 97 | puts " pgoffset INTEGER, /* Offset of page in file */" |
| 98 | puts " pgsize INTEGER /* Size of the page */" |
| 99 | puts ");" |
| 100 | db eval {SELECT quote(name) || ',' || |
| 101 | quote(path) || ',' || |
| 102 | quote(pageno) || ',' || |
| 103 | quote(pagetype) || ',' || |
| 104 | quote(ncell) || ',' || |
| 105 | quote(payload) || ',' || |
| 106 | quote(unused) || ',' || |
| 107 | quote(mx_payload) || ',' || |
| 108 | quote(pgoffset) || ',' || |
| 109 | quote(pgsize) AS x FROM stat} { |
| 110 | puts "INSERT INTO stats VALUES($x);" |
| 111 | } |
| 112 | puts "COMMIT;" |
| 113 | exit 0 |
| 114 | } |
| 115 | |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 116 | # In-memory database for collecting statistics. This script loops through |
| 117 | # the tables and indices in the database being analyzed, adding a row for each |
| 118 | # to an in-memory database (for which the schema is shown below). It then |
| 119 | # queries the in-memory db to produce the space-analysis report. |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 120 | # |
| 121 | sqlite3 mem :memory: |
drh | 4515a45 | 2011-08-31 17:46:50 +0000 | [diff] [blame] | 122 | set tabledef {CREATE TABLE space_used( |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 123 | name clob, -- Name of a table or index in the database file |
| 124 | tblname clob, -- Name of associated table |
| 125 | is_index boolean, -- TRUE if it is an index, false for a table |
| 126 | nentry int, -- Number of entries in the BTree |
| 127 | leaf_entries int, -- Number of leaf entries |
| 128 | payload int, -- Total amount of data stored in this table or index |
| 129 | ovfl_payload int, -- Total amount of data stored on overflow pages |
| 130 | ovfl_cnt int, -- Number of entries that use overflow |
| 131 | mx_payload int, -- Maximum payload size |
| 132 | int_pages int, -- Number of interior pages used |
| 133 | leaf_pages int, -- Number of leaf pages used |
| 134 | ovfl_pages int, -- Number of overflow pages used |
| 135 | int_unused int, -- Number of unused bytes on interior pages |
| 136 | leaf_unused int, -- Number of unused bytes on primary pages |
drh | 50c6706 | 2007-02-10 19:22:35 +0000 | [diff] [blame] | 137 | ovfl_unused int, -- Number of unused bytes on overflow pages |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 138 | gap_cnt int, -- Number of gaps in the page layout |
| 139 | compressed_size int -- Total bytes stored on disk |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 140 | );} |
| 141 | mem eval $tabledef |
| 142 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 143 | # Create a temporary "dbstat" virtual table. |
| 144 | # |
drh | 565621a | 2011-09-21 20:10:42 +0000 | [diff] [blame] | 145 | db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat} |
| 146 | db eval {CREATE TEMP TABLE dbstat AS SELECT * FROM temp.stat |
| 147 | ORDER BY name, path} |
| 148 | db eval {DROP TABLE temp.stat} |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 149 | |
| 150 | proc isleaf {pagetype is_index} { |
| 151 | return [expr {$pagetype == "leaf" || ($pagetype == "internal" && $is_index)}] |
| 152 | } |
| 153 | proc isoverflow {pagetype is_index} { |
| 154 | return [expr {$pagetype == "overflow"}] |
| 155 | } |
| 156 | proc isinternal {pagetype is_index} { |
| 157 | return [expr {$pagetype == "internal" && $is_index==0}] |
| 158 | } |
| 159 | |
| 160 | db func isleaf isleaf |
| 161 | db func isinternal isinternal |
| 162 | db func isoverflow isoverflow |
| 163 | |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 164 | set isCompressed 0 |
| 165 | set compressOverhead 0 |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 166 | set sql { SELECT name, tbl_name FROM sqlite_master WHERE rootpage>0 } |
| 167 | foreach {name tblname} [concat sqlite_master sqlite_master [db eval $sql]] { |
| 168 | |
| 169 | set is_index [expr {$name!=$tblname}] |
| 170 | db eval { |
| 171 | SELECT |
| 172 | sum(ncell) AS nentry, |
| 173 | sum(isleaf(pagetype, $is_index) * ncell) AS leaf_entries, |
| 174 | sum(payload) AS payload, |
| 175 | sum(isoverflow(pagetype, $is_index) * payload) AS ovfl_payload, |
| 176 | sum(path LIKE '%+000000') AS ovfl_cnt, |
| 177 | max(mx_payload) AS mx_payload, |
| 178 | sum(isinternal(pagetype, $is_index)) AS int_pages, |
| 179 | sum(isleaf(pagetype, $is_index)) AS leaf_pages, |
| 180 | sum(isoverflow(pagetype, $is_index)) AS ovfl_pages, |
| 181 | sum(isinternal(pagetype, $is_index) * unused) AS int_unused, |
| 182 | sum(isleaf(pagetype, $is_index) * unused) AS leaf_unused, |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 183 | sum(isoverflow(pagetype, $is_index) * unused) AS ovfl_unused, |
| 184 | sum(pgsize) AS compressed_size |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 185 | FROM temp.dbstat WHERE name = $name |
| 186 | } break |
| 187 | |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 188 | set total_pages [expr {$leaf_pages+$int_pages+$ovfl_pages}] |
| 189 | set storage [expr {$total_pages*$pageSize}] |
| 190 | if {!$isCompressed && $storage>$compressed_size} { |
| 191 | set isCompressed 1 |
| 192 | set compressOverhead 14 |
| 193 | } |
| 194 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 195 | # Column 'gap_cnt' is set to the number of non-contiguous entries in the |
| 196 | # list of pages visited if the b-tree structure is traversed in a top-down |
| 197 | # fashion (each node visited before its child-tree is passed). Any overflow |
| 198 | # chains present are traversed from start to finish before any child-tree |
| 199 | # is. |
| 200 | # |
| 201 | set gap_cnt 0 |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 202 | set prev 0 |
| 203 | db eval { |
| 204 | SELECT pageno, pagetype FROM temp.dbstat |
| 205 | WHERE name=$name |
| 206 | ORDER BY pageno |
| 207 | } { |
| 208 | if {$prev>0 && $pagetype=="leaf" && $pageno!=$prev+1} { |
| 209 | incr gap_cnt |
| 210 | } |
| 211 | set prev $pageno |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 212 | } |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 213 | mem eval { |
| 214 | INSERT INTO space_used VALUES( |
| 215 | $name, |
| 216 | $tblname, |
| 217 | $is_index, |
| 218 | $nentry, |
| 219 | $leaf_entries, |
| 220 | $payload, |
| 221 | $ovfl_payload, |
| 222 | $ovfl_cnt, |
| 223 | $mx_payload, |
| 224 | $int_pages, |
| 225 | $leaf_pages, |
| 226 | $ovfl_pages, |
| 227 | $int_unused, |
| 228 | $leaf_unused, |
| 229 | $ovfl_unused, |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 230 | $gap_cnt, |
| 231 | $compressed_size |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 232 | ); |
| 233 | } |
| 234 | } |
| 235 | |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 236 | proc integerify {real} { |
drh | 0349688 | 2007-12-04 13:41:51 +0000 | [diff] [blame] | 237 | if {[string is double -strict $real]} { |
drh | f08f384 | 2011-09-27 13:40:26 +0000 | [diff] [blame] | 238 | return [expr {wide($real)}] |
drh | 0349688 | 2007-12-04 13:41:51 +0000 | [diff] [blame] | 239 | } else { |
| 240 | return 0 |
| 241 | } |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 242 | } |
| 243 | mem function int integerify |
| 244 | |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 245 | # Quote a string for use in an SQL query. Examples: |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 246 | # |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 247 | # [quote {hello world}] == {'hello world'} |
| 248 | # [quote {hello world's}] == {'hello world''s'} |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 249 | # |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 250 | proc quote {txt} { |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 251 | return [string map {' ''} $txt] |
| 252 | } |
| 253 | |
| 254 | # Output a title line |
| 255 | # |
| 256 | proc titleline {title} { |
| 257 | if {$title==""} { |
| 258 | puts [string repeat * 79] |
| 259 | } else { |
| 260 | set len [string length $title] |
| 261 | set stars [string repeat * [expr 79-$len-5]] |
| 262 | puts "*** $title $stars" |
| 263 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 264 | } |
| 265 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 266 | # Generate a single line of output in the statistics section of the |
| 267 | # report. |
| 268 | # |
| 269 | proc statline {title value {extra {}}} { |
| 270 | set len [string length $title] |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 271 | set dots [string repeat . [expr 50-$len]] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 272 | set len [string length $value] |
| 273 | set sp2 [string range { } $len end] |
| 274 | if {$extra ne ""} { |
| 275 | set extra " $extra" |
| 276 | } |
| 277 | puts "$title$dots $value$sp2$extra" |
| 278 | } |
| 279 | |
| 280 | # Generate a formatted percentage value for $num/$denom |
| 281 | # |
| 282 | proc percent {num denom {of {}}} { |
| 283 | if {$denom==0.0} {return ""} |
| 284 | set v [expr {$num*100.0/$denom}] |
| 285 | set of {} |
drh | faf60c7 | 2005-03-29 13:18:16 +0000 | [diff] [blame] | 286 | if {$v==100.0 || $v<0.001 || ($v>1.0 && $v<99.0)} { |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 287 | return [format {%5.1f%% %s} $v $of] |
| 288 | } elseif {$v<0.1 || $v>99.9} { |
| 289 | return [format {%7.3f%% %s} $v $of] |
| 290 | } else { |
| 291 | return [format {%6.2f%% %s} $v $of] |
| 292 | } |
| 293 | } |
| 294 | |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 295 | proc divide {num denom} { |
| 296 | if {$denom==0} {return 0.0} |
| 297 | return [format %.2f [expr double($num)/double($denom)]] |
| 298 | } |
| 299 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 300 | # Generate a subreport that covers some subset of the database. |
| 301 | # the $where clause determines which subset to analyze. |
| 302 | # |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 303 | proc subreport {title where showFrag} { |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 304 | global pageSize file_pgcnt compressOverhead |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 305 | |
| 306 | # Query the in-memory database for the sum of various statistics |
| 307 | # for the subset of tables/indices identified by the WHERE clause in |
| 308 | # $where. Note that even if the WHERE clause matches no rows, the |
| 309 | # following query returns exactly one row (because it is an aggregate). |
| 310 | # |
| 311 | # The results of the query are stored directly by SQLite into local |
| 312 | # variables (i.e. $nentry, $nleaf etc.). |
| 313 | # |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 314 | mem eval " |
| 315 | SELECT |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 316 | int(sum(nentry)) AS nentry, |
| 317 | int(sum(leaf_entries)) AS nleaf, |
| 318 | int(sum(payload)) AS payload, |
| 319 | int(sum(ovfl_payload)) AS ovfl_payload, |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 320 | max(mx_payload) AS mx_payload, |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 321 | int(sum(ovfl_cnt)) as ovfl_cnt, |
| 322 | int(sum(leaf_pages)) AS leaf_pages, |
| 323 | int(sum(int_pages)) AS int_pages, |
| 324 | int(sum(ovfl_pages)) AS ovfl_pages, |
| 325 | int(sum(leaf_unused)) AS leaf_unused, |
| 326 | int(sum(int_unused)) AS int_unused, |
drh | 50c6706 | 2007-02-10 19:22:35 +0000 | [diff] [blame] | 327 | int(sum(ovfl_unused)) AS ovfl_unused, |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 328 | int(sum(gap_cnt)) AS gap_cnt, |
| 329 | int(sum(compressed_size)) AS compressed_size |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 330 | FROM space_used WHERE $where" {} {} |
| 331 | |
| 332 | # Output the sub-report title, nicely decorated with * characters. |
| 333 | # |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 334 | puts "" |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 335 | titleline $title |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 336 | puts "" |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 337 | |
| 338 | # Calculate statistics and store the results in TCL variables, as follows: |
| 339 | # |
| 340 | # total_pages: Database pages consumed. |
| 341 | # total_pages_percent: Pages consumed as a percentage of the file. |
| 342 | # storage: Bytes consumed. |
| 343 | # payload_percent: Payload bytes used as a percentage of $storage. |
| 344 | # total_unused: Unused bytes on pages. |
| 345 | # avg_payload: Average payload per btree entry. |
| 346 | # avg_fanout: Average fanout for internal pages. |
| 347 | # avg_unused: Average unused bytes per btree entry. |
| 348 | # ovfl_cnt_percent: Percentage of btree entries that use overflow pages. |
| 349 | # |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 350 | set total_pages [expr {$leaf_pages+$int_pages+$ovfl_pages}] |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 351 | set total_pages_percent [percent $total_pages $file_pgcnt] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 352 | set storage [expr {$total_pages*$pageSize}] |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 353 | set payload_percent [percent $payload $storage {of storage consumed}] |
| 354 | set total_unused [expr {$ovfl_unused+$int_unused+$leaf_unused}] |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 355 | set avg_payload [divide $payload $nleaf] |
| 356 | set avg_unused [divide $total_unused $nleaf] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 357 | if {$int_pages>0} { |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 358 | # TODO: Is this formula correct? |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 359 | set nTab [mem eval " |
| 360 | SELECT count(*) FROM ( |
| 361 | SELECT DISTINCT tblname FROM space_used WHERE $where AND is_index=0 |
| 362 | ) |
| 363 | "] |
| 364 | set avg_fanout [mem eval " |
| 365 | SELECT (sum(leaf_pages+int_pages)-$nTab)/sum(int_pages) FROM space_used |
| 366 | WHERE $where AND is_index = 0 |
| 367 | "] |
| 368 | set avg_fanout [format %.2f $avg_fanout] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 369 | } |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 370 | set ovfl_cnt_percent [percent $ovfl_cnt $nleaf {of all entries}] |
| 371 | |
| 372 | # Print out the sub-report statistics. |
| 373 | # |
| 374 | statline {Percentage of total database} $total_pages_percent |
| 375 | statline {Number of entries} $nleaf |
| 376 | statline {Bytes of storage consumed} $storage |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 377 | if {$compressed_size!=$storage} { |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 378 | set compressed_size [expr {$compressed_size+$compressOverhead*$total_pages}] |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame] | 379 | set pct [expr {$compressed_size*100.0/$storage}] |
| 380 | set pct [format {%5.1f%%} $pct] |
| 381 | statline {Bytes used after compression} $compressed_size $pct |
| 382 | } |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 383 | statline {Bytes of payload} $payload $payload_percent |
| 384 | statline {Average payload per entry} $avg_payload |
| 385 | statline {Average unused bytes per entry} $avg_unused |
| 386 | if {[info exists avg_fanout]} { |
| 387 | statline {Average fanout} $avg_fanout |
| 388 | } |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 389 | if {$showFrag && $total_pages>1} { |
| 390 | set fragmentation [percent $gap_cnt [expr {$total_pages-1}]] |
| 391 | statline {Non-sequential pages} $gap_cnt $fragmentation |
drh | 50c6706 | 2007-02-10 19:22:35 +0000 | [diff] [blame] | 392 | } |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 393 | statline {Maximum payload per entry} $mx_payload |
| 394 | statline {Entries that use overflow} $ovfl_cnt $ovfl_cnt_percent |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 395 | if {$int_pages>0} { |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 396 | statline {Index pages used} $int_pages |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 397 | } |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 398 | statline {Primary pages used} $leaf_pages |
| 399 | statline {Overflow pages used} $ovfl_pages |
| 400 | statline {Total pages used} $total_pages |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 401 | if {$int_unused>0} { |
drh | 4515a45 | 2011-08-31 17:46:50 +0000 | [diff] [blame] | 402 | set int_unused_percent [ |
| 403 | percent $int_unused [expr {$int_pages*$pageSize}] {of index space}] |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 404 | statline "Unused bytes on index pages" $int_unused $int_unused_percent |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 405 | } |
drh | 4515a45 | 2011-08-31 17:46:50 +0000 | [diff] [blame] | 406 | statline "Unused bytes on primary pages" $leaf_unused [ |
| 407 | percent $leaf_unused [expr {$leaf_pages*$pageSize}] {of primary space}] |
| 408 | statline "Unused bytes on overflow pages" $ovfl_unused [ |
| 409 | percent $ovfl_unused [expr {$ovfl_pages*$pageSize}] {of overflow space}] |
| 410 | statline "Unused bytes on all pages" $total_unused [ |
| 411 | percent $total_unused $storage {of all space}] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 412 | return 1 |
| 413 | } |
| 414 | |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 415 | # Calculate the overhead in pages caused by auto-vacuum. |
| 416 | # |
| 417 | # This procedure calculates and returns the number of pages used by the |
| 418 | # auto-vacuum 'pointer-map'. If the database does not support auto-vacuum, |
| 419 | # then 0 is returned. The two arguments are the size of the database file in |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 420 | # pages and the page size used by the database (in bytes). |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 421 | proc autovacuum_overhead {filePages pageSize} { |
| 422 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 423 | # Set $autovacuum to non-zero for databases that support auto-vacuum. |
| 424 | set autovacuum [db one {PRAGMA auto_vacuum}] |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 425 | |
| 426 | # If the database is not an auto-vacuum database or the file consists |
| 427 | # of one page only then there is no overhead for auto-vacuum. Return zero. |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 428 | if {0==$autovacuum || $filePages==1} { |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 429 | return 0 |
| 430 | } |
| 431 | |
| 432 | # The number of entries on each pointer map page. The layout of the |
| 433 | # database file is one pointer-map page, followed by $ptrsPerPage other |
| 434 | # pages, followed by a pointer-map page etc. The first pointer-map page |
| 435 | # is the second page of the file overall. |
| 436 | set ptrsPerPage [expr double($pageSize/5)] |
| 437 | |
| 438 | # Return the number of pointer map pages in the database. |
drh | f08f384 | 2011-09-27 13:40:26 +0000 | [diff] [blame] | 439 | return [expr wide(ceil( ($filePages-1.0)/($ptrsPerPage+1.0) ))] |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 440 | } |
| 441 | |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 442 | |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 443 | # Calculate the summary statistics for the database and store the results |
| 444 | # in TCL variables. They are output below. Variables are as follows: |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 445 | # |
| 446 | # pageSize: Size of each page in bytes. |
| 447 | # file_bytes: File size in bytes. |
| 448 | # file_pgcnt: Number of pages in the file. |
| 449 | # file_pgcnt2: Number of pages in the file (calculated). |
| 450 | # av_pgcnt: Pages consumed by the auto-vacuum pointer-map. |
| 451 | # av_percent: Percentage of the file consumed by auto-vacuum pointer-map. |
| 452 | # inuse_pgcnt: Data pages in the file. |
| 453 | # inuse_percent: Percentage of pages used to store data. |
| 454 | # free_pgcnt: Free pages calculated as (<total pages> - <in-use pages>) |
| 455 | # free_pgcnt2: Free pages in the file according to the file header. |
| 456 | # free_percent: Percentage of file consumed by free pages (calculated). |
| 457 | # free_percent2: Percentage of file consumed by free pages (header). |
| 458 | # ntable: Number of tables in the db. |
| 459 | # nindex: Number of indices in the db. |
| 460 | # nautoindex: Number of indices created automatically. |
| 461 | # nmanindex: Number of indices created manually. |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 462 | # user_payload: Number of bytes of payload in table btrees |
| 463 | # (not including sqlite_master) |
| 464 | # user_percent: $user_payload as a percentage of total file size. |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 465 | |
dan | 64b41c7 | 2011-09-26 19:32:47 +0000 | [diff] [blame] | 466 | ### The following, setting $file_bytes based on the actual size of the file |
| 467 | ### on disk, causes this tool to choke on zipvfs databases. So set it based |
| 468 | ### on the return of [PRAGMA page_count] instead. |
| 469 | if 0 { |
| 470 | set file_bytes [file size $file_to_analyze] |
| 471 | set file_pgcnt [expr {$file_bytes/$pageSize}] |
| 472 | } |
| 473 | set file_pgcnt [db one {PRAGMA page_count}] |
drh | f08f384 | 2011-09-27 13:40:26 +0000 | [diff] [blame] | 474 | set file_bytes [expr {$file_pgcnt * $pageSize}] |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 475 | |
| 476 | set av_pgcnt [autovacuum_overhead $file_pgcnt $pageSize] |
| 477 | set av_percent [percent $av_pgcnt $file_pgcnt] |
| 478 | |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 479 | set sql {SELECT sum(leaf_pages+int_pages+ovfl_pages) FROM space_used} |
drh | f08f384 | 2011-09-27 13:40:26 +0000 | [diff] [blame] | 480 | set inuse_pgcnt [expr wide([mem eval $sql])] |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 481 | set inuse_percent [percent $inuse_pgcnt $file_pgcnt] |
| 482 | |
drh | f08f384 | 2011-09-27 13:40:26 +0000 | [diff] [blame] | 483 | set free_pgcnt [expr {$file_pgcnt-$inuse_pgcnt-$av_pgcnt}] |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 484 | set free_percent [percent $free_pgcnt $file_pgcnt] |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 485 | set free_pgcnt2 [db one {PRAGMA freelist_count}] |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 486 | set free_percent2 [percent $free_pgcnt2 $file_pgcnt] |
| 487 | |
| 488 | set file_pgcnt2 [expr {$inuse_pgcnt+$free_pgcnt2+$av_pgcnt}] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 489 | |
| 490 | set ntable [db eval {SELECT count(*)+1 FROM sqlite_master WHERE type='table'}] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 491 | set nindex [db eval {SELECT count(*) FROM sqlite_master WHERE type='index'}] |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 492 | set sql {SELECT count(*) FROM sqlite_master WHERE name LIKE 'sqlite_autoindex%'} |
| 493 | set nautoindex [db eval $sql] |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 494 | set nmanindex [expr {$nindex-$nautoindex}] |
| 495 | |
| 496 | # set total_payload [mem eval "SELECT sum(payload) FROM space_used"] |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 497 | set user_payload [mem one {SELECT int(sum(payload)) FROM space_used |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 498 | WHERE NOT is_index AND name NOT LIKE 'sqlite_master'}] |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 499 | set user_percent [percent $user_payload $file_bytes] |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 500 | |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 501 | # Output the summary statistics calculated above. |
| 502 | # |
drh | 453a312 | 2012-10-10 10:52:46 +0000 | [diff] [blame] | 503 | puts "/** Disk-Space Utilization Report For $root_filename" |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 504 | puts "" |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 505 | statline {Page size in bytes} $pageSize |
| 506 | statline {Pages in the whole file (measured)} $file_pgcnt |
| 507 | statline {Pages in the whole file (calculated)} $file_pgcnt2 |
| 508 | statline {Pages that store data} $inuse_pgcnt $inuse_percent |
| 509 | statline {Pages on the freelist (per header)} $free_pgcnt2 $free_percent2 |
| 510 | statline {Pages on the freelist (calculated)} $free_pgcnt $free_percent |
| 511 | statline {Pages of auto-vacuum overhead} $av_pgcnt $av_percent |
| 512 | statline {Number of tables in the database} $ntable |
| 513 | statline {Number of indices} $nindex |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 514 | statline {Number of defined indices} $nmanindex |
| 515 | statline {Number of implied indices} $nautoindex |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 516 | if {$isCompressed} { |
| 517 | statline {Size of uncompressed content in bytes} $file_bytes |
| 518 | set efficiency [percent $true_file_size $file_bytes] |
| 519 | statline {Size of compressed file on disk} $true_file_size $efficiency |
| 520 | } else { |
| 521 | statline {Size of the file in bytes} $file_bytes |
| 522 | } |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 523 | statline {Bytes of user payload stored} $user_payload $user_percent |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 524 | |
| 525 | # Output table rankings |
| 526 | # |
| 527 | puts "" |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 528 | titleline "Page counts for all tables with their indices" |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 529 | puts "" |
| 530 | mem eval {SELECT tblname, count(*) AS cnt, |
danielk1977 | 24c9253 | 2005-02-01 10:36:40 +0000 | [diff] [blame] | 531 | int(sum(int_pages+leaf_pages+ovfl_pages)) AS size |
drh | faf60c7 | 2005-03-29 13:18:16 +0000 | [diff] [blame] | 532 | FROM space_used GROUP BY tblname ORDER BY size+0 DESC, tblname} {} { |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 533 | statline [string toupper $tblname] $size [percent $size $file_pgcnt] |
| 534 | } |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 535 | puts "" |
| 536 | titleline "Page counts for all tables and indices separately" |
| 537 | puts "" |
| 538 | mem eval { |
| 539 | SELECT |
| 540 | upper(name) AS nm, |
| 541 | int(int_pages+leaf_pages+ovfl_pages) AS size |
| 542 | FROM space_used |
| 543 | ORDER BY size+0 DESC, name} {} { |
| 544 | statline $nm $size [percent $size $file_pgcnt] |
| 545 | } |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 546 | if {$isCompressed} { |
| 547 | puts "" |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 548 | titleline "Bytes of disk space used after compression" |
drh | b770894 | 2011-10-05 18:18:13 +0000 | [diff] [blame] | 549 | puts "" |
| 550 | set csum 0 |
| 551 | mem eval {SELECT tblname, |
| 552 | int(sum(compressed_size)) + |
| 553 | $compressOverhead*sum(int_pages+leaf_pages+ovfl_pages) |
| 554 | AS csize |
| 555 | FROM space_used GROUP BY tblname ORDER BY csize+0 DESC, tblname} {} { |
| 556 | incr csum $csize |
| 557 | statline [string toupper $tblname] $csize [percent $csize $true_file_size] |
| 558 | } |
| 559 | set overhead [expr {$true_file_size - $csum}] |
| 560 | if {$overhead>0} { |
| 561 | statline {Header and free space} $overhead [percent $overhead $true_file_size] |
| 562 | } |
| 563 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 564 | |
| 565 | # Output subreports |
| 566 | # |
| 567 | if {$nindex>0} { |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 568 | subreport {All tables and indices} 1 0 |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 569 | } |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 570 | subreport {All tables} {NOT is_index} 0 |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 571 | if {$nindex>0} { |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 572 | subreport {All indices} {is_index} 0 |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 573 | } |
drh | 7913e41 | 2013-11-01 20:30:36 +0000 | [diff] [blame] | 574 | foreach tbl [mem eval {SELECT DISTINCT tblname name FROM space_used |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 575 | ORDER BY name}] { |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 576 | set qn [quote $tbl] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 577 | set name [string toupper $tbl] |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 578 | set n [mem eval {SELECT count(*) FROM space_used WHERE tblname=$tbl}] |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 579 | if {$n>1} { |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 580 | set idxlist [mem eval "SELECT name FROM space_used |
| 581 | WHERE tblname='$qn' AND is_index |
| 582 | ORDER BY 1"] |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 583 | subreport "Table $name and all its indices" "tblname='$qn'" 0 |
| 584 | subreport "Table $name w/o any indices" "name='$qn'" 1 |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 585 | if {[llength $idxlist]>1} { |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 586 | subreport "Indices of table $name" "tblname='$qn' AND is_index" 0 |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 587 | } |
| 588 | foreach idx $idxlist { |
| 589 | set qidx [quote $idx] |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 590 | subreport "Index [string toupper $idx] of table $name" "name='$qidx'" 1 |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 591 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 592 | } else { |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 593 | subreport "Table $name" "name='$qn'" 1 |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
| 597 | # Output instructions on what the numbers above mean. |
| 598 | # |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 599 | puts "" |
| 600 | titleline Definitions |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 601 | puts { |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 602 | Page size in bytes |
| 603 | |
| 604 | The number of bytes in a single page of the database file. |
| 605 | Usually 1024. |
| 606 | |
| 607 | Number of pages in the whole file |
| 608 | } |
drh | 4515a45 | 2011-08-31 17:46:50 +0000 | [diff] [blame] | 609 | puts " The number of $pageSize-byte pages that go into forming the complete |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 610 | database" |
drh | 4515a45 | 2011-08-31 17:46:50 +0000 | [diff] [blame] | 611 | puts { |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 612 | Pages that store data |
| 613 | |
| 614 | The number of pages that store data, either as primary B*Tree pages or |
| 615 | as overflow pages. The number at the right is the data pages divided by |
| 616 | the total number of pages in the file. |
| 617 | |
| 618 | Pages on the freelist |
| 619 | |
| 620 | The number of pages that are not currently in use but are reserved for |
| 621 | future use. The percentage at the right is the number of freelist pages |
| 622 | divided by the total number of pages in the file. |
| 623 | |
danielk1977 | 1625445 | 2004-11-08 16:15:09 +0000 | [diff] [blame] | 624 | Pages of auto-vacuum overhead |
| 625 | |
| 626 | The number of pages that store data used by the database to facilitate |
| 627 | auto-vacuum. This is zero for databases that do not support auto-vacuum. |
| 628 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 629 | Number of tables in the database |
| 630 | |
| 631 | The number of tables in the database, including the SQLITE_MASTER table |
| 632 | used to store schema information. |
| 633 | |
| 634 | Number of indices |
| 635 | |
| 636 | The total number of indices in the database. |
| 637 | |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 638 | Number of defined indices |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 639 | |
| 640 | The number of indices created using an explicit CREATE INDEX statement. |
| 641 | |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 642 | Number of implied indices |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 643 | |
| 644 | The number of indices used to implement PRIMARY KEY or UNIQUE constraints |
| 645 | on tables. |
| 646 | |
| 647 | Size of the file in bytes |
| 648 | |
| 649 | The total amount of disk space used by the entire database files. |
| 650 | |
| 651 | Bytes of user payload stored |
| 652 | |
| 653 | The total number of bytes of user payload stored in the database. The |
| 654 | schema information in the SQLITE_MASTER table is not counted when |
| 655 | computing this number. The percentage at the right shows the payload |
| 656 | divided by the total file size. |
| 657 | |
| 658 | Percentage of total database |
| 659 | |
| 660 | The amount of the complete database file that is devoted to storing |
| 661 | information described by this category. |
| 662 | |
| 663 | Number of entries |
| 664 | |
| 665 | The total number of B-Tree key/value pairs stored under this category. |
| 666 | |
| 667 | Bytes of storage consumed |
| 668 | |
| 669 | The total amount of disk space required to store all B-Tree entries |
| 670 | under this category. The is the total number of pages used times |
| 671 | the pages size. |
| 672 | |
| 673 | Bytes of payload |
| 674 | |
| 675 | The amount of payload stored under this category. Payload is the data |
| 676 | part of table entries and the key part of index entries. The percentage |
| 677 | at the right is the bytes of payload divided by the bytes of storage |
| 678 | consumed. |
| 679 | |
| 680 | Average payload per entry |
| 681 | |
| 682 | The average amount of payload on each entry. This is just the bytes of |
| 683 | payload divided by the number of entries. |
| 684 | |
| 685 | Average unused bytes per entry |
| 686 | |
| 687 | The average amount of free space remaining on all pages under this |
| 688 | category on a per-entry basis. This is the number of unused bytes on |
| 689 | all pages divided by the number of entries. |
| 690 | |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 691 | Non-sequential pages |
drh | fc6e0c9 | 2007-02-13 01:41:52 +0000 | [diff] [blame] | 692 | |
drh | 2f312ee | 2013-09-28 12:40:55 +0000 | [diff] [blame] | 693 | The number of pages in the table or index that are out of sequence. |
| 694 | Many filesystems are optimized for sequential file access so a small |
| 695 | number of non-sequential pages might result in faster queries, |
| 696 | especially for larger database files that do not fit in the disk cache. |
| 697 | Note that after running VACUUM, the root page of each table or index is |
| 698 | at the beginning of the database file and all other pages are in a |
| 699 | separate part of the database file, resulting in a single non- |
| 700 | sequential page. |
drh | fc6e0c9 | 2007-02-13 01:41:52 +0000 | [diff] [blame] | 701 | |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 702 | Maximum payload per entry |
| 703 | |
| 704 | The largest payload size of any entry. |
| 705 | |
| 706 | Entries that use overflow |
| 707 | |
| 708 | The number of entries that user one or more overflow pages. |
| 709 | |
| 710 | Total pages used |
| 711 | |
| 712 | This is the number of pages used to hold all information in the current |
| 713 | category. This is the sum of index, primary, and overflow pages. |
| 714 | |
| 715 | Index pages used |
| 716 | |
| 717 | This is the number of pages in a table B-tree that hold only key (rowid) |
| 718 | information and no data. |
| 719 | |
| 720 | Primary pages used |
| 721 | |
| 722 | This is the number of B-tree pages that hold both key and data. |
| 723 | |
| 724 | Overflow pages used |
| 725 | |
| 726 | The total number of overflow pages used for this category. |
| 727 | |
| 728 | Unused bytes on index pages |
| 729 | |
| 730 | The total number of bytes of unused space on all index pages. The |
| 731 | percentage at the right is the number of unused bytes divided by the |
| 732 | total number of bytes on index pages. |
| 733 | |
| 734 | Unused bytes on primary pages |
| 735 | |
| 736 | The total number of bytes of unused space on all primary pages. The |
| 737 | percentage at the right is the number of unused bytes divided by the |
| 738 | total number of bytes on primary pages. |
| 739 | |
| 740 | Unused bytes on overflow pages |
| 741 | |
| 742 | The total number of bytes of unused space on all overflow pages. The |
| 743 | percentage at the right is the number of unused bytes divided by the |
| 744 | total number of bytes on overflow pages. |
| 745 | |
| 746 | Unused bytes on all pages |
| 747 | |
| 748 | The total number of bytes of unused space on all primary and overflow |
| 749 | pages. The percentage at the right is the number of unused bytes |
| 750 | divided by the total number of bytes. |
| 751 | } |
| 752 | |
danielk1977 | 0ba87cb | 2004-11-09 07:42:11 +0000 | [diff] [blame] | 753 | # Output a dump of the in-memory database. This can be used for more |
| 754 | # complex offline analysis. |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 755 | # |
drh | cc07130 | 2013-07-17 18:12:15 +0000 | [diff] [blame] | 756 | titleline {} |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 757 | puts "The entire text of this report can be sourced into any SQL database" |
| 758 | puts "engine for further analysis. All of the text above is an SQL comment." |
| 759 | puts "The data used to generate this report follows:" |
| 760 | puts "*/" |
| 761 | puts "BEGIN;" |
| 762 | puts $tabledef |
| 763 | unset -nocomplain x |
| 764 | mem eval {SELECT * FROM space_used} x { |
| 765 | puts -nonewline "INSERT INTO space_used VALUES" |
| 766 | set sep ( |
| 767 | foreach col $x(*) { |
| 768 | set v $x($col) |
drh | a464171 | 2013-11-02 11:34:58 +0000 | [diff] [blame] | 769 | if {$v=="" || ![string is double $v]} {set v '[quote $v]'} |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 770 | puts -nonewline $sep$v |
| 771 | set sep , |
| 772 | } |
| 773 | puts ");" |
| 774 | } |
| 775 | puts "COMMIT;" |
drh | a7531c6 | 2006-01-24 02:19:53 +0000 | [diff] [blame] | 776 | |
| 777 | } err]} { |
| 778 | puts "ERROR: $err" |
| 779 | puts $errorInfo |
| 780 | exit 1 |
| 781 | } |