| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Disk space requirements</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="am_misc.html" title="Chapter 4. Access Method Wrapup" /> |
| 11 | <link rel="prev" href="am_misc_dbsizes.html" title="Database limits" /> |
| 12 | <link rel="next" href="am_misc_db_sql.html" title="Specifying a Berkeley DB schema using SQL DDL" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Disk space requirements</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="am_misc_dbsizes.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 4. |
| 26 | Access Method Wrapup |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="am_misc_db_sql.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="am_misc_diskspace"></a>Disk space requirements</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <div class="toc"> |
| 42 | <dl> |
| 43 | <dt> |
| 44 | <span class="sect2"> |
| 45 | <a href="am_misc_diskspace.html#idp51253016">Btree</a> |
| 46 | </span> |
| 47 | </dt> |
| 48 | <dt> |
| 49 | <span class="sect2"> |
| 50 | <a href="am_misc_diskspace.html#idp51253080">Hash</a> |
| 51 | </span> |
| 52 | </dt> |
| 53 | </dl> |
| 54 | </div> |
| 55 | <p>It is possible to estimate the total database size based on the size of |
| 56 | the data. The following calculations are an estimate of how many bytes |
| 57 | you will need to hold a set of data and then how many pages it will take |
| 58 | to actually store it on disk.</p> |
| 59 | <p>Space freed by deleting key/data pairs from a Btree or Hash database is |
| 60 | never returned to the filesystem, although it is reused where possible. |
| 61 | This means that the Btree and Hash databases are grow-only. If enough |
| 62 | keys are deleted from a database that shrinking the underlying file is |
| 63 | desirable, you should use the <a href="../api_reference/C/dbcompact.html" class="olink">DB->compact()</a> method to reclaim disk space. Alternatively, |
| 64 | you can create a new database and copy the records from |
| 65 | the old one into it.</p> |
| 66 | <p>These are rough estimates at best. For example, they do not take into |
| 67 | account overflow records, filesystem metadata information, large sets |
| 68 | of duplicate data items (where the key is only stored once), or |
| 69 | real-life situations where the sizes of key and data items are wildly |
| 70 | variable, and the page-fill factor changes over time.</p> |
| 71 | <div class="sect2" lang="en" xml:lang="en"> |
| 72 | <div class="titlepage"> |
| 73 | <div> |
| 74 | <div> |
| 75 | <h3 class="title"><a id="idp51253016"></a>Btree</h3> |
| 76 | </div> |
| 77 | </div> |
| 78 | </div> |
| 79 | <p>The formulas for the Btree access method are as follows:</p> |
| 80 | <pre class="programlisting">useful-bytes-per-page = (page-size - page-overhead) * page-fill-factor |
| 81 | <p></p> |
| 82 | bytes-of-data = n-records * |
| 83 | (bytes-per-entry + page-overhead-for-two-entries) |
| 84 | <p></p> |
| 85 | n-pages-of-data = bytes-of-data / useful-bytes-per-page |
| 86 | <p></p> |
| 87 | total-bytes-on-disk = n-pages-of-data * page-size |
| 88 | </pre> |
| 89 | <p>The <span class="bold"><strong>useful-bytes-per-page</strong></span> is a measure of the bytes on each page |
| 90 | that will actually hold the application data. It is computed as the total |
| 91 | number of bytes on the page that are available to hold application data, |
| 92 | corrected by the percentage of the page that is likely to contain data. |
| 93 | The reason for this correction is that the percentage of a page that |
| 94 | contains application data can vary from close to 50% after a page split |
| 95 | to almost 100% if the entries in the database were inserted in sorted |
| 96 | order. Obviously, the <span class="bold"><strong>page-fill-factor</strong></span> can drastically alter |
| 97 | the amount of disk space required to hold any particular data set. The |
| 98 | page-fill factor of any existing database can be displayed using the |
| 99 | <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility.</p> |
| 100 | <p>The page-overhead for Btree databases is 26 bytes. As an example, using |
| 101 | an 8K page size, with an 85% page-fill factor, there are 6941 bytes of |
| 102 | useful space on each page:</p> |
| 103 | <pre class="programlisting">6941 = (8192 - 26) * .85</pre> |
| 104 | <p>The total <span class="bold"><strong>bytes-of-data</strong></span> is an easy calculation: It is the |
| 105 | number of key or data items plus the overhead required to store each |
| 106 | item on a page. The overhead to store a key or data item on a Btree |
| 107 | page is 5 bytes. So, it would take 1560000000 bytes, or roughly 1.34GB |
| 108 | of total data to store 60,000,000 key/data pairs, assuming each key or |
| 109 | data item was 8 bytes long:</p> |
| 110 | <pre class="programlisting">1560000000 = 60000000 * ((8 + 5) * 2)</pre> |
| 111 | <p>The total pages of data, <span class="bold"><strong>n-pages-of-data</strong></span>, is the |
| 112 | <span class="bold"><strong>bytes-of-data</strong></span> divided by the <span class="bold"><strong>useful-bytes-per-page</strong></span>. In |
| 113 | the example, there are 224751 pages of data.</p> |
| 114 | <pre class="programlisting">224751 = 1560000000 / 6941</pre> |
| 115 | <p>The total bytes of disk space for the database is <span class="bold"><strong>n-pages-of-data</strong></span> |
| 116 | multiplied by the <span class="bold"><strong>page-size</strong></span>. In the example, the result is |
| 117 | 1841160192 bytes, or roughly 1.71GB.</p> |
| 118 | <pre class="programlisting">1841160192 = 224751 * 8192</pre> |
| 119 | </div> |
| 120 | <div class="sect2" lang="en" xml:lang="en"> |
| 121 | <div class="titlepage"> |
| 122 | <div> |
| 123 | <div> |
| 124 | <h3 class="title"><a id="idp51253080"></a>Hash</h3> |
| 125 | </div> |
| 126 | </div> |
| 127 | </div> |
| 128 | <p>The formulas for the Hash access method are as follows:</p> |
| 129 | <pre class="programlisting">useful-bytes-per-page = (page-size - page-overhead) |
| 130 | <p></p> |
| 131 | bytes-of-data = n-records * |
| 132 | (bytes-per-entry + page-overhead-for-two-entries) |
| 133 | <p></p> |
| 134 | n-pages-of-data = bytes-of-data / useful-bytes-per-page |
| 135 | <p></p> |
| 136 | total-bytes-on-disk = n-pages-of-data * page-size |
| 137 | </pre> |
| 138 | <p>The <span class="bold"><strong>useful-bytes-per-page</strong></span> is a measure of the bytes on each page |
| 139 | that will actually hold the application data. It is computed as the total |
| 140 | number of bytes on the page that are available to hold application data. |
| 141 | If the application has explicitly set a page-fill factor, pages will |
| 142 | not necessarily be kept full. For databases with a preset fill factor, |
| 143 | see the calculation below. The page-overhead for Hash databases is 26 |
| 144 | bytes and the page-overhead-for-two-entries is 6 bytes.</p> |
| 145 | <p>As an example, using an 8K page size, there are 8166 bytes of useful space |
| 146 | on each page:</p> |
| 147 | <pre class="programlisting">8166 = (8192 - 26)</pre> |
| 148 | <p>The total <span class="bold"><strong>bytes-of-data</strong></span> is an easy calculation: it is the number |
| 149 | of key/data pairs plus the overhead required to store each pair on a page. |
| 150 | In this case that's 6 bytes per pair. So, assuming 60,000,000 key/data |
| 151 | pairs, each of which is 8 bytes long, there are 1320000000 bytes, or |
| 152 | roughly 1.23GB of total data:</p> |
| 153 | <pre class="programlisting">1320000000 = 60000000 * (16 + 6)</pre> |
| 154 | <p>The total pages of data, <span class="bold"><strong>n-pages-of-data</strong></span>, is the |
| 155 | <span class="bold"><strong>bytes-of-data</strong></span> divided by the <span class="bold"><strong>useful-bytes-per-page</strong></span>. In |
| 156 | this example, there are 161646 pages of data.</p> |
| 157 | <pre class="programlisting">161646 = 1320000000 / 8166</pre> |
| 158 | <p>The total bytes of disk space for the database is <span class="bold"><strong>n-pages-of-data</strong></span> |
| 159 | multiplied by the <span class="bold"><strong>page-size</strong></span>. In the example, the result is |
| 160 | 1324204032 bytes, or roughly 1.23GB.</p> |
| 161 | <pre class="programlisting">1324204032 = 161646 * 8192</pre> |
| 162 | <p>Now, let's assume that the application specified a fill factor explicitly. |
| 163 | The fill factor indicates the target number of items to place on a single |
| 164 | page (a fill factor might reduce the utilization of each page, but it can |
| 165 | be useful in avoiding splits and preventing buckets from becoming too |
| 166 | large). Using our estimates above, each item is 22 bytes (16 + 6), and |
| 167 | there are 8166 useful bytes on a page (8192 - 26). That means that, on |
| 168 | average, you can fit 371 pairs per page.</p> |
| 169 | <pre class="programlisting">371 = 8166 / 22</pre> |
| 170 | <p>However, let's assume that the application designer knows that although |
| 171 | most items are 8 bytes, they can sometimes be as large as 10, and it's |
| 172 | very important to avoid overflowing buckets and splitting. Then, the |
| 173 | application might specify a fill factor of 314.</p> |
| 174 | <pre class="programlisting">314 = 8166 / 26</pre> |
| 175 | <p>With a fill factor of 314, then the formula for computing database size |
| 176 | is</p> |
| 177 | <pre class="programlisting">n-pages-of-data = npairs / pairs-per-page</pre> |
| 178 | <p>or 191082.</p> |
| 179 | <pre class="programlisting">191082 = 60000000 / 314</pre> |
| 180 | <p>At 191082 pages, the total database size would be 1565343744, or 1.46GB.</p> |
| 181 | <pre class="programlisting">1565343744 = 191082 * 8192</pre> |
| 182 | <p>There are a few additional caveats with respect to Hash databases. This |
| 183 | discussion assumes that the hash function does a good job of evenly |
| 184 | distributing keys among hash buckets. If the function does not do this, |
| 185 | you may find your table growing significantly larger than you expected. |
| 186 | Secondly, in order to provide support for Hash databases coexisting with |
| 187 | other databases in a single file, pages within a Hash database are |
| 188 | allocated in power-of-two chunks. That means that a Hash database with 65 |
| 189 | buckets will take up as much space as a Hash database with 128 buckets; |
| 190 | each time the Hash database grows beyond its current power-of-two number |
| 191 | of buckets, it allocates space for the next power-of-two buckets. This |
| 192 | space may be sparsely allocated in the file system, but the files will |
| 193 | appear to be their full size. Finally, because of this need for |
| 194 | contiguous allocation, overflow pages and duplicate pages can be allocated |
| 195 | only at specific points in the file, and this too can lead to sparse hash |
| 196 | tables.</p> |
| 197 | </div> |
| 198 | </div> |
| 199 | <div class="navfooter"> |
| 200 | <hr /> |
| 201 | <table width="100%" summary="Navigation footer"> |
| 202 | <tr> |
| 203 | <td width="40%" align="left"><a accesskey="p" href="am_misc_dbsizes.html">Prev</a> </td> |
| 204 | <td width="20%" align="center"> |
| 205 | <a accesskey="u" href="am_misc.html">Up</a> |
| 206 | </td> |
| 207 | <td width="40%" align="right"> <a accesskey="n" href="am_misc_db_sql.html">Next</a></td> |
| 208 | </tr> |
| 209 | <tr> |
| 210 | <td width="40%" align="left" valign="top">Database limits </td> |
| 211 | <td width="20%" align="center"> |
| 212 | <a accesskey="h" href="index.html">Home</a> |
| 213 | </td> |
| 214 | <td width="40%" align="right" valign="top"> Specifying a Berkeley DB schema using SQL DDL</td> |
| 215 | </tr> |
| 216 | </table> |
| 217 | </div> |
| 218 | </body> |
| 219 | </html> |