OLD | NEW |
1 # Run this TCL script using "testfixture" in order get a report that shows | 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 | 2 # how much disk space is used by a particular data to actually store data |
3 # versus how much space is unused. | 3 # versus how much space is unused. |
4 # | 4 # |
5 | 5 |
6 if {[catch { | 6 if {[catch { |
7 | 7 |
8 # Get the name of the database to analyze | 8 # Get the name of the database to analyze |
9 # | 9 # |
10 #set argv $argv0 | 10 #set argv $argv0 |
11 if {[llength $argv]!=1} { | 11 if {[llength $argv]!=1} { |
12 puts stderr "Usage: $argv0 database-name" | 12 puts stderr "Usage: $argv0 database-name" |
13 exit 1 | 13 exit 1 |
14 } | 14 } |
15 set file_to_analyze [lindex $argv 0] | 15 set file_to_analyze [lindex $argv 0] |
16 if {![file exists $file_to_analyze]} { | 16 if {![file exists $file_to_analyze]} { |
17 puts stderr "No such file: $file_to_analyze" | 17 puts stderr "No such file: $file_to_analyze" |
18 exit 1 | 18 exit 1 |
19 } | 19 } |
20 if {![file readable $file_to_analyze]} { | 20 if {![file readable $file_to_analyze]} { |
21 puts stderr "File is not readable: $file_to_analyze" | 21 puts stderr "File is not readable: $file_to_analyze" |
22 exit 1 | 22 exit 1 |
23 } | 23 } |
24 if {[file size $file_to_analyze]<512} { | 24 if {[file size $file_to_analyze]<512} { |
25 puts stderr "Empty or malformed database: $file_to_analyze" | 25 puts stderr "Empty or malformed database: $file_to_analyze" |
26 exit 1 | 26 exit 1 |
27 } | 27 } |
28 | 28 |
29 # Maximum distance between pages before we consider it a "gap" | |
30 # | |
31 set MAXGAP 3 | |
32 | |
33 # Open the database | 29 # Open the database |
34 # | 30 # |
35 sqlite3 db [lindex $argv 0] | 31 sqlite3 db [lindex $argv 0] |
36 set DB [btree_open [lindex $argv 0] 1000 0] | 32 register_dbstat_vtab db |
| 33 |
| 34 set pageSize [db one {PRAGMA page_size}] |
| 35 |
| 36 #set DB [btree_open [lindex $argv 0] 1000 0] |
37 | 37 |
38 # In-memory database for collecting statistics. This script loops through | 38 # In-memory database for collecting statistics. This script loops through |
39 # the tables and indices in the database being analyzed, adding a row for each | 39 # the tables and indices in the database being analyzed, adding a row for each |
40 # to an in-memory database (for which the schema is shown below). It then | 40 # to an in-memory database (for which the schema is shown below). It then |
41 # queries the in-memory db to produce the space-analysis report. | 41 # queries the in-memory db to produce the space-analysis report. |
42 # | 42 # |
43 sqlite3 mem :memory: | 43 sqlite3 mem :memory: |
44 set tabledef\ | 44 set tabledef\ |
45 {CREATE TABLE space_used( | 45 {CREATE TABLE space_used( |
46 name clob, -- Name of a table or index in the database file | 46 name clob, -- Name of a table or index in the database file |
47 tblname clob, -- Name of associated table | 47 tblname clob, -- Name of associated table |
48 is_index boolean, -- TRUE if it is an index, false for a table | 48 is_index boolean, -- TRUE if it is an index, false for a table |
49 nentry int, -- Number of entries in the BTree | 49 nentry int, -- Number of entries in the BTree |
50 leaf_entries int, -- Number of leaf entries | 50 leaf_entries int, -- Number of leaf entries |
51 payload int, -- Total amount of data stored in this table or index | 51 payload int, -- Total amount of data stored in this table or index |
52 ovfl_payload int, -- Total amount of data stored on overflow pages | 52 ovfl_payload int, -- Total amount of data stored on overflow pages |
53 ovfl_cnt int, -- Number of entries that use overflow | 53 ovfl_cnt int, -- Number of entries that use overflow |
54 mx_payload int, -- Maximum payload size | 54 mx_payload int, -- Maximum payload size |
55 int_pages int, -- Number of interior pages used | 55 int_pages int, -- Number of interior pages used |
56 leaf_pages int, -- Number of leaf pages used | 56 leaf_pages int, -- Number of leaf pages used |
57 ovfl_pages int, -- Number of overflow pages used | 57 ovfl_pages int, -- Number of overflow pages used |
58 int_unused int, -- Number of unused bytes on interior pages | 58 int_unused int, -- Number of unused bytes on interior pages |
59 leaf_unused int, -- Number of unused bytes on primary pages | 59 leaf_unused int, -- Number of unused bytes on primary pages |
60 ovfl_unused int, -- Number of unused bytes on overflow pages | 60 ovfl_unused int, -- Number of unused bytes on overflow pages |
61 gap_cnt int -- Number of gaps in the page layout | 61 gap_cnt int -- Number of gaps in the page layout |
62 );} | 62 );} |
63 mem eval $tabledef | 63 mem eval $tabledef |
64 | 64 |
| 65 # Create a temporary "dbstat" virtual table. |
| 66 # |
| 67 db eval { |
| 68 CREATE VIRTUAL TABLE temp.stat USING dbstat; |
| 69 CREATE TEMP TABLE dbstat AS SELECT * FROM temp.stat ORDER BY name, path; |
| 70 DROP TABLE temp.stat; |
| 71 } |
| 72 |
| 73 proc isleaf {pagetype is_index} { |
| 74 return [expr {$pagetype == "leaf" || ($pagetype == "internal" && $is_index)}] |
| 75 } |
| 76 proc isoverflow {pagetype is_index} { |
| 77 return [expr {$pagetype == "overflow"}] |
| 78 } |
| 79 proc isinternal {pagetype is_index} { |
| 80 return [expr {$pagetype == "internal" && $is_index==0}] |
| 81 } |
| 82 |
| 83 db func isleaf isleaf |
| 84 db func isinternal isinternal |
| 85 db func isoverflow isoverflow |
| 86 |
| 87 set sql { SELECT name, tbl_name FROM sqlite_master WHERE rootpage>0 } |
| 88 foreach {name tblname} [concat sqlite_master sqlite_master [db eval $sql]] { |
| 89 |
| 90 set is_index [expr {$name!=$tblname}] |
| 91 db eval { |
| 92 SELECT |
| 93 sum(ncell) AS nentry, |
| 94 sum(isleaf(pagetype, $is_index) * ncell) AS leaf_entries, |
| 95 sum(payload) AS payload, |
| 96 sum(isoverflow(pagetype, $is_index) * payload) AS ovfl_payload, |
| 97 sum(path LIKE '%+000000') AS ovfl_cnt, |
| 98 max(mx_payload) AS mx_payload, |
| 99 sum(isinternal(pagetype, $is_index)) AS int_pages, |
| 100 sum(isleaf(pagetype, $is_index)) AS leaf_pages, |
| 101 sum(isoverflow(pagetype, $is_index)) AS ovfl_pages, |
| 102 sum(isinternal(pagetype, $is_index) * unused) AS int_unused, |
| 103 sum(isleaf(pagetype, $is_index) * unused) AS leaf_unused, |
| 104 sum(isoverflow(pagetype, $is_index) * unused) AS ovfl_unused |
| 105 FROM temp.dbstat WHERE name = $name |
| 106 } break |
| 107 |
| 108 # Column 'gap_cnt' is set to the number of non-contiguous entries in the |
| 109 # list of pages visited if the b-tree structure is traversed in a top-down |
| 110 # fashion (each node visited before its child-tree is passed). Any overflow |
| 111 # chains present are traversed from start to finish before any child-tree |
| 112 # is. |
| 113 # |
| 114 set gap_cnt 0 |
| 115 set pglist [db eval { |
| 116 SELECT pageno FROM temp.dbstat WHERE name = $name ORDER BY rowid |
| 117 }] |
| 118 set prev [lindex $pglist 0] |
| 119 foreach pgno [lrange $pglist 1 end] { |
| 120 if {$pgno != $prev+1} {incr gap_cnt} |
| 121 set prev $pgno |
| 122 } |
| 123 |
| 124 mem eval { |
| 125 INSERT INTO space_used VALUES( |
| 126 $name, |
| 127 $tblname, |
| 128 $is_index, |
| 129 $nentry, |
| 130 $leaf_entries, |
| 131 $payload, |
| 132 $ovfl_payload, |
| 133 $ovfl_cnt, |
| 134 $mx_payload, |
| 135 $int_pages, |
| 136 $leaf_pages, |
| 137 $ovfl_pages, |
| 138 $int_unused, |
| 139 $leaf_unused, |
| 140 $ovfl_unused, |
| 141 $gap_cnt |
| 142 ); |
| 143 } |
| 144 } |
| 145 |
65 proc integerify {real} { | 146 proc integerify {real} { |
66 if {[string is double -strict $real]} { | 147 if {[string is double -strict $real]} { |
67 return [expr {int($real)}] | 148 return [expr {int($real)}] |
68 } else { | 149 } else { |
69 return 0 | 150 return 0 |
70 } | 151 } |
71 } | 152 } |
72 mem function int integerify | 153 mem function int integerify |
73 | 154 |
74 # Quote a string for use in an SQL query. Examples: | 155 # Quote a string for use in an SQL query. Examples: |
75 # | 156 # |
76 # [quote {hello world}] == {'hello world'} | 157 # [quote {hello world}] == {'hello world'} |
77 # [quote {hello world's}] == {'hello world''s'} | 158 # [quote {hello world's}] == {'hello world''s'} |
78 # | 159 # |
79 proc quote {txt} { | 160 proc quote {txt} { |
80 regsub -all ' $txt '' q | 161 regsub -all ' $txt '' q |
81 return '$q' | 162 return '$q' |
82 } | 163 } |
83 | 164 |
84 # This proc is a wrapper around the btree_cursor_info command. The | |
85 # second argument is an open btree cursor returned by [btree_cursor]. | |
86 # The first argument is the name of an array variable that exists in | |
87 # the scope of the caller. If the third argument is non-zero, then | |
88 # info is returned for the page that lies $up entries upwards in the | |
89 # tree-structure. (i.e. $up==1 returns the parent page, $up==2 the | |
90 # grandparent etc.) | |
91 # | |
92 # The following entries in that array are filled in with information retrieved | |
93 # using [btree_cursor_info]: | |
94 # | |
95 # $arrayvar(page_no) = The page number | |
96 # $arrayvar(entry_no) = The entry number | |
97 # $arrayvar(page_entries) = Total number of entries on this page | |
98 # $arrayvar(cell_size) = Cell size (local payload + header) | |
99 # $arrayvar(page_freebytes) = Number of free bytes on this page | |
100 # $arrayvar(page_freeblocks) = Number of free blocks on the page | |
101 # $arrayvar(payload_bytes) = Total payload size (local + overflow) | |
102 # $arrayvar(header_bytes) = Header size in bytes | |
103 # $arrayvar(local_payload_bytes) = Local payload size | |
104 # $arrayvar(parent) = Parent page number | |
105 # | |
106 proc cursor_info {arrayvar csr {up 0}} { | |
107 upvar $arrayvar a | |
108 foreach [list a(page_no) \ | |
109 a(entry_no) \ | |
110 a(page_entries) \ | |
111 a(cell_size) \ | |
112 a(page_freebytes) \ | |
113 a(page_freeblocks) \ | |
114 a(payload_bytes) \ | |
115 a(header_bytes) \ | |
116 a(local_payload_bytes) \ | |
117 a(parent) \ | |
118 a(first_ovfl) ] [btree_cursor_info $csr $up] break | |
119 } | |
120 | |
121 # Determine the page-size of the database. This global variable is used | |
122 # throughout the script. | |
123 # | |
124 set pageSize [db eval {PRAGMA page_size}] | |
125 | |
126 # Analyze every table in the database, one at a time. | |
127 # | |
128 # The following query returns the name and root-page of each table in the | |
129 # database, including the sqlite_master table. | |
130 # | |
131 set sql { | |
132 SELECT name, rootpage FROM sqlite_master | |
133 WHERE type='table' AND rootpage>0 | |
134 UNION ALL | |
135 SELECT 'sqlite_master', 1 | |
136 ORDER BY 1 | |
137 } | |
138 set wideZero [expr {10000000000 - 10000000000}] | |
139 foreach {name rootpage} [db eval $sql] { | |
140 puts stderr "Analyzing table $name..." | |
141 | |
142 # Code below traverses the table being analyzed (table name $name), using the | |
143 # btree cursor $cursor. Statistics related to table $name are accumulated in | |
144 # the following variables: | |
145 # | |
146 set total_payload $wideZero ;# Payload space used by all entries | |
147 set total_ovfl $wideZero ;# Payload space on overflow pages | |
148 set unused_int $wideZero ;# Unused space on interior nodes | |
149 set unused_leaf $wideZero ;# Unused space on leaf nodes | |
150 set unused_ovfl $wideZero ;# Unused space on overflow pages | |
151 set cnt_ovfl $wideZero ;# Number of entries that use overflows | |
152 set cnt_leaf_entry $wideZero ;# Number of leaf entries | |
153 set cnt_int_entry $wideZero ;# Number of interor entries | |
154 set mx_payload $wideZero ;# Maximum payload size | |
155 set ovfl_pages $wideZero ;# Number of overflow pages used | |
156 set leaf_pages $wideZero ;# Number of leaf pages | |
157 set int_pages $wideZero ;# Number of interior pages | |
158 set gap_cnt 0 ;# Number of holes in the page sequence | |
159 set prev_pgno 0 ;# Last page number seen | |
160 | |
161 # As the btree is traversed, the array variable $seen($pgno) is set to 1 | |
162 # the first time page $pgno is encountered. | |
163 # | |
164 catch {unset seen} | |
165 | |
166 # The following loop runs once for each entry in table $name. The table | |
167 # is traversed using the btree cursor stored in variable $csr | |
168 # | |
169 set csr [btree_cursor $DB $rootpage 0] | |
170 for {btree_first $csr} {![btree_eof $csr]} {btree_next $csr} { | |
171 incr cnt_leaf_entry | |
172 | |
173 # Retrieve information about the entry the btree-cursor points to into | |
174 # the array variable $ci (cursor info). | |
175 # | |
176 cursor_info ci $csr | |
177 | |
178 # Check if the payload of this entry is greater than the current | |
179 # $mx_payload statistic for the table. Also increase the $total_payload | |
180 # statistic. | |
181 # | |
182 if {$ci(payload_bytes)>$mx_payload} {set mx_payload $ci(payload_bytes)} | |
183 incr total_payload $ci(payload_bytes) | |
184 | |
185 # If this entry uses overflow pages, then update the $cnt_ovfl, | |
186 # $total_ovfl, $ovfl_pages and $unused_ovfl statistics. | |
187 # | |
188 set ovfl [expr {$ci(payload_bytes)-$ci(local_payload_bytes)}] | |
189 if {$ovfl} { | |
190 incr cnt_ovfl | |
191 incr total_ovfl $ovfl | |
192 set n [expr {int(ceil($ovfl/($pageSize-4.0)))}] | |
193 incr ovfl_pages $n | |
194 incr unused_ovfl [expr {$n*($pageSize-4) - $ovfl}] | |
195 set pglist [btree_ovfl_info $DB $csr] | |
196 } else { | |
197 set pglist {} | |
198 } | |
199 | |
200 # If this is the first table entry analyzed for the page, then update | |
201 # the page-related statistics $leaf_pages and $unused_leaf. Also, if | |
202 # this page has a parent page that has not been analyzed, retrieve | |
203 # info for the parent and update statistics for it too. | |
204 # | |
205 if {![info exists seen($ci(page_no))]} { | |
206 set seen($ci(page_no)) 1 | |
207 incr leaf_pages | |
208 incr unused_leaf $ci(page_freebytes) | |
209 set pglist "$ci(page_no) $pglist" | |
210 | |
211 # Now check if the page has a parent that has not been analyzed. If | |
212 # so, update the $int_pages, $cnt_int_entry and $unused_int statistics | |
213 # accordingly. Then check if the parent page has a parent that has | |
214 # not yet been analyzed etc. | |
215 # | |
216 # set parent $ci(parent_page_no) | |
217 for {set up 1} \ | |
218 {$ci(parent)!=0 && ![info exists seen($ci(parent))]} {incr up} \ | |
219 { | |
220 # Mark the parent as seen. | |
221 # | |
222 set seen($ci(parent)) 1 | |
223 | |
224 # Retrieve info for the parent and update statistics. | |
225 cursor_info ci $csr $up | |
226 incr int_pages | |
227 incr cnt_int_entry $ci(page_entries) | |
228 incr unused_int $ci(page_freebytes) | |
229 | |
230 # parent pages come before their first child | |
231 set pglist "$ci(page_no) $pglist" | |
232 } | |
233 } | |
234 | |
235 # Check the page list for fragmentation | |
236 # | |
237 foreach pg $pglist { | |
238 if {$pg!=$prev_pgno+1 && $prev_pgno>0} { | |
239 incr gap_cnt | |
240 } | |
241 set prev_pgno $pg | |
242 } | |
243 } | |
244 btree_close_cursor $csr | |
245 | |
246 # Handle the special case where a table contains no data. In this case | |
247 # all statistics are zero, except for the number of leaf pages (1) and | |
248 # the unused bytes on leaf pages ($pageSize - 8). | |
249 # | |
250 # An exception to the above is the sqlite_master table. If it is empty | |
251 # then all statistics are zero except for the number of leaf pages (1), | |
252 # and the number of unused bytes on leaf pages ($pageSize - 112). | |
253 # | |
254 if {[llength [array names seen]]==0} { | |
255 set leaf_pages 1 | |
256 if {$rootpage==1} { | |
257 set unused_leaf [expr {$pageSize-112}] | |
258 } else { | |
259 set unused_leaf [expr {$pageSize-8}] | |
260 } | |
261 } | |
262 | |
263 # Insert the statistics for the table analyzed into the in-memory database. | |
264 # | |
265 set sql "INSERT INTO space_used VALUES(" | |
266 append sql [quote $name] | |
267 append sql ",[quote $name]" | |
268 append sql ",0" | |
269 append sql ",[expr {$cnt_leaf_entry+$cnt_int_entry}]" | |
270 append sql ",$cnt_leaf_entry" | |
271 append sql ",$total_payload" | |
272 append sql ",$total_ovfl" | |
273 append sql ",$cnt_ovfl" | |
274 append sql ",$mx_payload" | |
275 append sql ",$int_pages" | |
276 append sql ",$leaf_pages" | |
277 append sql ",$ovfl_pages" | |
278 append sql ",$unused_int" | |
279 append sql ",$unused_leaf" | |
280 append sql ",$unused_ovfl" | |
281 append sql ",$gap_cnt" | |
282 append sql ); | |
283 mem eval $sql | |
284 } | |
285 | |
286 # Analyze every index in the database, one at a time. | |
287 # | |
288 # The query below returns the name, associated table and root-page number | |
289 # for every index in the database. | |
290 # | |
291 set sql { | |
292 SELECT name, tbl_name, rootpage FROM sqlite_master WHERE type='index' | |
293 ORDER BY 2, 1 | |
294 } | |
295 foreach {name tbl_name rootpage} [db eval $sql] { | |
296 puts stderr "Analyzing index $name of table $tbl_name..." | |
297 | |
298 # Code below traverses the index being analyzed (index name $name), using the | |
299 # btree cursor $cursor. Statistics related to index $name are accumulated in | |
300 # the following variables: | |
301 # | |
302 set total_payload $wideZero ;# Payload space used by all entries | |
303 set total_ovfl $wideZero ;# Payload space on overflow pages | |
304 set unused_leaf $wideZero ;# Unused space on leaf nodes | |
305 set unused_ovfl $wideZero ;# Unused space on overflow pages | |
306 set cnt_ovfl $wideZero ;# Number of entries that use overflows | |
307 set cnt_leaf_entry $wideZero ;# Number of leaf entries | |
308 set mx_payload $wideZero ;# Maximum payload size | |
309 set ovfl_pages $wideZero ;# Number of overflow pages used | |
310 set leaf_pages $wideZero ;# Number of leaf pages | |
311 set gap_cnt 0 ;# Number of holes in the page sequence | |
312 set prev_pgno 0 ;# Last page number seen | |
313 | |
314 # As the btree is traversed, the array variable $seen($pgno) is set to 1 | |
315 # the first time page $pgno is encountered. | |
316 # | |
317 catch {unset seen} | |
318 | |
319 # The following loop runs once for each entry in index $name. The index | |
320 # is traversed using the btree cursor stored in variable $csr | |
321 # | |
322 set csr [btree_cursor $DB $rootpage 0] | |
323 for {btree_first $csr} {![btree_eof $csr]} {btree_next $csr} { | |
324 incr cnt_leaf_entry | |
325 | |
326 # Retrieve information about the entry the btree-cursor points to into | |
327 # the array variable $ci (cursor info). | |
328 # | |
329 cursor_info ci $csr | |
330 | |
331 # Check if the payload of this entry is greater than the current | |
332 # $mx_payload statistic for the table. Also increase the $total_payload | |
333 # statistic. | |
334 # | |
335 set payload [btree_keysize $csr] | |
336 if {$payload>$mx_payload} {set mx_payload $payload} | |
337 incr total_payload $payload | |
338 | |
339 # If this entry uses overflow pages, then update the $cnt_ovfl, | |
340 # $total_ovfl, $ovfl_pages and $unused_ovfl statistics. | |
341 # | |
342 set ovfl [expr {$payload-$ci(local_payload_bytes)}] | |
343 if {$ovfl} { | |
344 incr cnt_ovfl | |
345 incr total_ovfl $ovfl | |
346 set n [expr {int(ceil($ovfl/($pageSize-4.0)))}] | |
347 incr ovfl_pages $n | |
348 incr unused_ovfl [expr {$n*($pageSize-4) - $ovfl}] | |
349 } | |
350 | |
351 # If this is the first table entry analyzed for the page, then update | |
352 # the page-related statistics $leaf_pages and $unused_leaf. | |
353 # | |
354 if {![info exists seen($ci(page_no))]} { | |
355 set seen($ci(page_no)) 1 | |
356 incr leaf_pages | |
357 incr unused_leaf $ci(page_freebytes) | |
358 set pg $ci(page_no) | |
359 if {$prev_pgno>0 && $pg!=$prev_pgno+1} { | |
360 incr gap_cnt | |
361 } | |
362 set prev_pgno $ci(page_no) | |
363 } | |
364 } | |
365 btree_close_cursor $csr | |
366 | |
367 # Handle the special case where a index contains no data. In this case | |
368 # all statistics are zero, except for the number of leaf pages (1) and | |
369 # the unused bytes on leaf pages ($pageSize - 8). | |
370 # | |
371 if {[llength [array names seen]]==0} { | |
372 set leaf_pages 1 | |
373 set unused_leaf [expr {$pageSize-8}] | |
374 } | |
375 | |
376 # Insert the statistics for the index analyzed into the in-memory database. | |
377 # | |
378 set sql "INSERT INTO space_used VALUES(" | |
379 append sql [quote $name] | |
380 append sql ",[quote $tbl_name]" | |
381 append sql ",1" | |
382 append sql ",$cnt_leaf_entry" | |
383 append sql ",$cnt_leaf_entry" | |
384 append sql ",$total_payload" | |
385 append sql ",$total_ovfl" | |
386 append sql ",$cnt_ovfl" | |
387 append sql ",$mx_payload" | |
388 append sql ",0" | |
389 append sql ",$leaf_pages" | |
390 append sql ",$ovfl_pages" | |
391 append sql ",0" | |
392 append sql ",$unused_leaf" | |
393 append sql ",$unused_ovfl" | |
394 append sql ",$gap_cnt" | |
395 append sql ); | |
396 mem eval $sql | |
397 } | |
398 | |
399 # Generate a single line of output in the statistics section of the | 165 # Generate a single line of output in the statistics section of the |
400 # report. | 166 # report. |
401 # | 167 # |
402 proc statline {title value {extra {}}} { | 168 proc statline {title value {extra {}}} { |
403 set len [string length $title] | 169 set len [string length $title] |
404 set dots [string range {......................................} $len end] | 170 set dots [string range {......................................} $len end] |
405 set len [string length $value] | 171 set len [string length $value] |
406 set sp2 [string range { } $len end] | 172 set sp2 [string range { } $len end] |
407 if {$extra ne ""} { | 173 if {$extra ne ""} { |
408 set extra " $extra" | 174 set extra " $extra" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 } | 307 } |
542 | 308 |
543 # Calculate the overhead in pages caused by auto-vacuum. | 309 # Calculate the overhead in pages caused by auto-vacuum. |
544 # | 310 # |
545 # This procedure calculates and returns the number of pages used by the | 311 # This procedure calculates and returns the number of pages used by the |
546 # auto-vacuum 'pointer-map'. If the database does not support auto-vacuum, | 312 # auto-vacuum 'pointer-map'. If the database does not support auto-vacuum, |
547 # then 0 is returned. The two arguments are the size of the database file in | 313 # then 0 is returned. The two arguments are the size of the database file in |
548 # pages and the page size used by the database (in bytes). | 314 # pages and the page size used by the database (in bytes). |
549 proc autovacuum_overhead {filePages pageSize} { | 315 proc autovacuum_overhead {filePages pageSize} { |
550 | 316 |
551 # Read the value of meta 4. If non-zero, then the database supports | 317 # Set $autovacuum to non-zero for databases that support auto-vacuum. |
552 # auto-vacuum. It would be possible to use "PRAGMA auto_vacuum" instead, | 318 set autovacuum [db one {PRAGMA auto_vacuum}] |
553 # but that would not work if the SQLITE_OMIT_PRAGMA macro was defined | |
554 # when the library was built. | |
555 set meta4 [lindex [btree_get_meta $::DB] 4] | |
556 | 319 |
557 # If the database is not an auto-vacuum database or the file consists | 320 # If the database is not an auto-vacuum database or the file consists |
558 # of one page only then there is no overhead for auto-vacuum. Return zero. | 321 # of one page only then there is no overhead for auto-vacuum. Return zero. |
559 if {0==$meta4 || $filePages==1} { | 322 if {0==$autovacuum || $filePages==1} { |
560 return 0 | 323 return 0 |
561 } | 324 } |
562 | 325 |
563 # The number of entries on each pointer map page. The layout of the | 326 # The number of entries on each pointer map page. The layout of the |
564 # database file is one pointer-map page, followed by $ptrsPerPage other | 327 # database file is one pointer-map page, followed by $ptrsPerPage other |
565 # pages, followed by a pointer-map page etc. The first pointer-map page | 328 # pages, followed by a pointer-map page etc. The first pointer-map page |
566 # is the second page of the file overall. | 329 # is the second page of the file overall. |
567 set ptrsPerPage [expr double($pageSize/5)] | 330 set ptrsPerPage [expr double($pageSize/5)] |
568 | 331 |
569 # Return the number of pointer map pages in the database. | 332 # Return the number of pointer map pages in the database. |
(...skipping 29 matching lines...) Expand all Loading... |
599 | 362 |
600 set av_pgcnt [autovacuum_overhead $file_pgcnt $pageSize] | 363 set av_pgcnt [autovacuum_overhead $file_pgcnt $pageSize] |
601 set av_percent [percent $av_pgcnt $file_pgcnt] | 364 set av_percent [percent $av_pgcnt $file_pgcnt] |
602 | 365 |
603 set sql {SELECT sum(leaf_pages+int_pages+ovfl_pages) FROM space_used} | 366 set sql {SELECT sum(leaf_pages+int_pages+ovfl_pages) FROM space_used} |
604 set inuse_pgcnt [expr int([mem eval $sql])] | 367 set inuse_pgcnt [expr int([mem eval $sql])] |
605 set inuse_percent [percent $inuse_pgcnt $file_pgcnt] | 368 set inuse_percent [percent $inuse_pgcnt $file_pgcnt] |
606 | 369 |
607 set free_pgcnt [expr $file_pgcnt-$inuse_pgcnt-$av_pgcnt] | 370 set free_pgcnt [expr $file_pgcnt-$inuse_pgcnt-$av_pgcnt] |
608 set free_percent [percent $free_pgcnt $file_pgcnt] | 371 set free_percent [percent $free_pgcnt $file_pgcnt] |
609 set free_pgcnt2 [lindex [btree_get_meta $DB] 0] | 372 set free_pgcnt2 [db one {PRAGMA freelist_count}] |
610 set free_percent2 [percent $free_pgcnt2 $file_pgcnt] | 373 set free_percent2 [percent $free_pgcnt2 $file_pgcnt] |
611 | 374 |
612 set file_pgcnt2 [expr {$inuse_pgcnt+$free_pgcnt2+$av_pgcnt}] | 375 set file_pgcnt2 [expr {$inuse_pgcnt+$free_pgcnt2+$av_pgcnt}] |
613 | 376 |
614 set ntable [db eval {SELECT count(*)+1 FROM sqlite_master WHERE type='table'}] | 377 set ntable [db eval {SELECT count(*)+1 FROM sqlite_master WHERE type='table'}] |
615 set nindex [db eval {SELECT count(*) FROM sqlite_master WHERE type='index'}] | 378 set nindex [db eval {SELECT count(*) FROM sqlite_master WHERE type='index'}] |
616 set sql {SELECT count(*) FROM sqlite_master WHERE name LIKE 'sqlite_autoindex%'} | 379 set sql {SELECT count(*) FROM sqlite_master WHERE name LIKE 'sqlite_autoindex%'} |
617 set nautoindex [db eval $sql] | 380 set nautoindex [db eval $sql] |
618 set nmanindex [expr {$nindex-$nautoindex}] | 381 set nmanindex [expr {$nindex-$nautoindex}] |
619 | 382 |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 } | 617 } |
855 puts ");" | 618 puts ");" |
856 } | 619 } |
857 puts "COMMIT;" | 620 puts "COMMIT;" |
858 | 621 |
859 } err]} { | 622 } err]} { |
860 puts "ERROR: $err" | 623 puts "ERROR: $err" |
861 puts $errorInfo | 624 puts $errorInfo |
862 exit 1 | 625 exit 1 |
863 } | 626 } |
OLD | NEW |