| 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>General access method configuration</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_conf.html" title="Chapter 2. Access Method Configuration" /> |
| 11 | <link rel="prev" href="am_conf_logrec.html" title="Logical record numbers" /> |
| 12 | <link rel="next" href="bt_conf.html" title="Btree access method specific configuration" /> |
| 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">General access method configuration</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="am_conf_logrec.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 2. |
| 26 | Access Method Configuration |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="bt_conf.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="general_am_conf"></a>General access method configuration</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <div class="toc"> |
| 42 | <dl> |
| 43 | <dt> |
| 44 | <span class="sect2"> |
| 45 | <a href="general_am_conf.html#am_conf_pagesize">Selecting a page size</a> |
| 46 | </span> |
| 47 | </dt> |
| 48 | <dt> |
| 49 | <span class="sect2"> |
| 50 | <a href="general_am_conf.html#am_conf_cachesize">Selecting a cache size</a> |
| 51 | </span> |
| 52 | </dt> |
| 53 | <dt> |
| 54 | <span class="sect2"> |
| 55 | <a href="general_am_conf.html#am_conf_byteorder">Selecting a byte order</a> |
| 56 | </span> |
| 57 | </dt> |
| 58 | <dt> |
| 59 | <span class="sect2"> |
| 60 | <a href="general_am_conf.html#am_conf_dup">Duplicate data items</a> |
| 61 | </span> |
| 62 | </dt> |
| 63 | <dt> |
| 64 | <span class="sect2"> |
| 65 | <a href="general_am_conf.html#am_conf_malloc">Non-local memory allocation</a> |
| 66 | </span> |
| 67 | </dt> |
| 68 | </dl> |
| 69 | </div> |
| 70 | <p> |
| 71 | There are a series of configuration tasks which are common to all |
| 72 | access methods. They are described in the following sections. |
| 73 | </p> |
| 74 | <div class="sect2" lang="en" xml:lang="en"> |
| 75 | <div class="titlepage"> |
| 76 | <div> |
| 77 | <div> |
| 78 | <h3 class="title"><a id="am_conf_pagesize"></a>Selecting a page size</h3> |
| 79 | </div> |
| 80 | </div> |
| 81 | </div> |
| 82 | <p> |
| 83 | The size of the pages used in the underlying database can be specified |
| 84 | by calling the <a href="../api_reference/C/dbset_pagesize.html" class="olink">DB->set_pagesize()</a> method. The minimum page size is 512 |
| 85 | bytes and the maximum page size is 64K bytes, and must be a power of |
| 86 | two. If no page size is specified by the application, a page size is |
| 87 | selected based on the underlying filesystem I/O block size. (A page |
| 88 | size selected in this way has a lower limit of 512 bytes and an upper |
| 89 | limit of 16K bytes.) |
| 90 | </p> |
| 91 | <p> |
| 92 | There are several issues to consider when selecting a pagesize: overflow |
| 93 | record sizes, locking, I/O efficiency, and recoverability. |
| 94 | </p> |
| 95 | <p> |
| 96 | First, the page size implicitly sets the size of an overflow record. |
| 97 | Overflow records are key or data items that are too large to fit on a |
| 98 | normal database page because of their size, and are therefore stored in |
| 99 | overflow pages. Overflow pages are pages that exist outside of the |
| 100 | normal database structure. For this reason, there is often a |
| 101 | significant performance penalty associated with retrieving or modifying |
| 102 | overflow records. Selecting a page size that is too small, and which |
| 103 | forces the creation of large numbers of overflow pages, can seriously |
| 104 | impact the performance of an application. |
| 105 | </p> |
| 106 | <p> |
| 107 | Second, in the Btree, Hash and Recno access methods, the finest-grained |
| 108 | lock that Berkeley DB acquires is for a page. (The Queue access method |
| 109 | generally acquires record-level locks rather than page-level locks.) |
| 110 | Selecting a page size that is too large, and which causes threads or |
| 111 | processes to wait because other threads of control are accessing or |
| 112 | modifying records on the same page, can impact the performance of your |
| 113 | application. |
| 114 | </p> |
| 115 | <p> |
| 116 | Third, the page size specifies the granularity of I/O from the database |
| 117 | to the operating system. Berkeley DB will give a page-sized unit of |
| 118 | bytes to the operating system to be scheduled for reading/writing |
| 119 | from/to the disk. For many operating systems, there is an internal |
| 120 | <span class="bold"><strong>block size</strong></span> which is used as the |
| 121 | granularity of I/O from the operating system to the disk. Generally, |
| 122 | it will be more efficient for Berkeley DB to write filesystem-sized |
| 123 | blocks to the operating system and for the operating system to write |
| 124 | those same blocks to the disk. |
| 125 | </p> |
| 126 | <p> |
| 127 | Selecting a database page size smaller than the filesystem block size |
| 128 | may cause the operating system to coalesce or otherwise manipulate |
| 129 | Berkeley DB pages and can impact the performance of your application. |
| 130 | When the page size is smaller than the filesystem block size and a page |
| 131 | written by Berkeley DB is not found in the operating system's cache, |
| 132 | the operating system may be forced to read a block from the disk, copy |
| 133 | the page into the block it read, and then write out the block to disk, |
| 134 | rather than simply writing the page to disk. Additionally, as the |
| 135 | operating system is reading more data into its buffer cache than is |
| 136 | strictly necessary to satisfy each Berkeley DB request for a page, the |
| 137 | operating system buffer cache may be wasting memory. |
| 138 | </p> |
| 139 | <p> |
| 140 | Alternatively, selecting a page size larger than the filesystem block |
| 141 | size may cause the operating system to read more data than necessary. |
| 142 | On some systems, reading filesystem blocks sequentially may cause the |
| 143 | operating system to begin performing read-ahead. If requesting a |
| 144 | single database page implies reading enough filesystem blocks to |
| 145 | satisfy the operating system's criteria for read-ahead, the operating |
| 146 | system may do more I/O than is required. |
| 147 | </p> |
| 148 | <p> |
| 149 | Fourth, when using the Berkeley DB Transactional Data Store product, |
| 150 | the page size may affect the errors from which your database can |
| 151 | recover See <a class="xref" href="transapp_reclimit.html" title="Berkeley DB recoverability">Berkeley DB recoverability</a> for more information. |
| 152 | </p> |
| 153 | <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 154 | <h3 class="title">Note</h3> |
| 155 | <p> |
| 156 | The <a href="../api_reference/C/db_tuner.html" class="olink">db_tuner</a> utility suggests a page size for btree databases that optimizes cache |
| 157 | efficiency and storage space requirements. This utility works only when given a pre-populated database. |
| 158 | So, it is useful when tuning an existing application and not when first implementing an application. |
| 159 | </p> |
| 160 | </div> |
| 161 | </div> |
| 162 | <div class="sect2" lang="en" xml:lang="en"> |
| 163 | <div class="titlepage"> |
| 164 | <div> |
| 165 | <div> |
| 166 | <h3 class="title"><a id="am_conf_cachesize"></a>Selecting a cache size</h3> |
| 167 | </div> |
| 168 | </div> |
| 169 | </div> |
| 170 | <p>The size of the cache used for the underlying database can be specified |
| 171 | by calling the <a href="../api_reference/C/dbset_cachesize.html" class="olink">DB->set_cachesize()</a> method. |
| 172 | Choosing a cache size is, unfortunately, an art. Your cache must be at |
| 173 | least large enough for your working set plus some overlap for unexpected |
| 174 | situations.</p> |
| 175 | <p>When using the Btree access method, you must have a cache big enough for |
| 176 | the minimum working set for a single access. This will include a root |
| 177 | page, one or more internal pages (depending on the depth of your tree), |
| 178 | and a leaf page. If your cache is any smaller than that, each new page |
| 179 | will force out the least-recently-used page, and Berkeley DB will re-read the |
| 180 | root page of the tree anew on each database request.</p> |
| 181 | <p>If your keys are of moderate size (a few tens of bytes) and your pages |
| 182 | are on the order of 4KB to 8KB, most Btree applications will be only |
| 183 | three levels. For example, using 20 byte keys with 20 bytes of data |
| 184 | associated with each key, a 8KB page can hold roughly 400 keys (or 200 |
| 185 | key/data pairs), so a fully populated three-level Btree will hold 32 |
| 186 | million key/data pairs, and a tree with only a 50% page-fill factor will |
| 187 | still hold 16 million key/data pairs. We rarely expect trees to exceed |
| 188 | five levels, although Berkeley DB will support trees up to 255 levels.</p> |
| 189 | <p>The rule-of-thumb is that cache is good, and more cache is better. |
| 190 | Generally, applications benefit from increasing the cache size up to a |
| 191 | point, at which the performance will stop improving as the cache size |
| 192 | increases. When this point is reached, one of two things have happened: |
| 193 | either the cache is large enough that the application is almost never |
| 194 | having to retrieve information from disk, or, your application is doing |
| 195 | truly random accesses, and therefore increasing size of the cache doesn't |
| 196 | significantly increase the odds of finding the next requested information |
| 197 | in the cache. The latter is fairly rare -- almost all applications show |
| 198 | some form of locality of reference.</p> |
| 199 | <p>That said, it is important not to increase your cache size beyond the |
| 200 | capabilities of your system, as that will result in reduced performance. |
| 201 | Under many operating systems, tying down enough virtual memory will cause |
| 202 | your memory and potentially your program to be swapped. This is |
| 203 | especially likely on systems without unified OS buffer caches and virtual |
| 204 | memory spaces, as the buffer cache was allocated at boot time and so |
| 205 | cannot be adjusted based on application requests for large amounts of |
| 206 | virtual memory.</p> |
| 207 | <p>For example, even if accesses are truly random within a Btree, your |
| 208 | access pattern will favor internal pages to leaf pages, so your cache |
| 209 | should be large enough to hold all internal pages. In the steady state, |
| 210 | this requires at most one I/O per operation to retrieve the appropriate |
| 211 | leaf page.</p> |
| 212 | <p>You can use the <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility to monitor the effectiveness of |
| 213 | your cache. The following output is excerpted from the output of that |
| 214 | utility's <span class="bold"><strong>-m</strong></span> option:</p> |
| 215 | <pre class="programlisting">prompt: db_stat -m |
| 216 | 131072 Cache size (128K). |
| 217 | 4273 Requested pages found in the cache (97%). |
| 218 | 134 Requested pages not found in the cache. |
| 219 | 18 Pages created in the cache. |
| 220 | 116 Pages read into the cache. |
| 221 | 93 Pages written from the cache to the backing file. |
| 222 | 5 Clean pages forced from the cache. |
| 223 | 13 Dirty pages forced from the cache. |
| 224 | 0 Dirty buffers written by trickle-sync thread. |
| 225 | 130 Current clean buffer count. |
| 226 | 4 Current dirty buffer count. |
| 227 | </pre> |
| 228 | <p>The statistics for this cache say that there have been 4,273 requests of |
| 229 | the cache, and only 116 of those requests required an I/O from disk. This |
| 230 | means that the cache is working well, yielding a 97% cache hit rate. The |
| 231 | <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility will present these statistics both for the cache |
| 232 | as a whole and for each file within the cache separately.</p> |
| 233 | </div> |
| 234 | <div class="sect2" lang="en" xml:lang="en"> |
| 235 | <div class="titlepage"> |
| 236 | <div> |
| 237 | <div> |
| 238 | <h3 class="title"><a id="am_conf_byteorder"></a>Selecting a byte order</h3> |
| 239 | </div> |
| 240 | </div> |
| 241 | </div> |
| 242 | <p>Database files created by Berkeley DB can be created in either little- or |
| 243 | big-endian formats. The byte order used for the underlying database |
| 244 | is specified by calling the <a href="../api_reference/C/dbset_lorder.html" class="olink">DB->set_lorder()</a> method. If no order |
| 245 | is selected, the native format of the machine on which the database is |
| 246 | created will be used.</p> |
| 247 | <p>Berkeley DB databases are architecture independent, and any format database can |
| 248 | be used on a machine with a different native format. In this case, as |
| 249 | each page that is read into or written from the cache must be converted |
| 250 | to or from the host format, and databases with non-native formats will |
| 251 | incur a performance penalty for the run-time conversion.</p> |
| 252 | <p> |
| 253 | <span class="bold"> |
| 254 | <strong>It is important to note that the Berkeley DB access methods do no data |
| 255 | conversion for application specified data. Key/data pairs written on a |
| 256 | little-endian format architecture will be returned to the application |
| 257 | exactly as they were written when retrieved on a big-endian format |
| 258 | architecture.</strong> |
| 259 | </span> |
| 260 | </p> |
| 261 | </div> |
| 262 | <div class="sect2" lang="en" xml:lang="en"> |
| 263 | <div class="titlepage"> |
| 264 | <div> |
| 265 | <div> |
| 266 | <h3 class="title"><a id="am_conf_dup"></a>Duplicate data items</h3> |
| 267 | </div> |
| 268 | </div> |
| 269 | </div> |
| 270 | <p> |
| 271 | The Btree and Hash access methods support the creation of multiple data |
| 272 | items for a single key item. By default, multiple data items are not |
| 273 | permitted, and each database store operation will overwrite any |
| 274 | previous data item for that key. To configure Berkeley DB for |
| 275 | duplicate data items, call the <a href="../api_reference/C/dbset_flags.html" class="olink">DB->set_flags()</a> method with the <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_DUP" class="olink">DB_DUP</a> |
| 276 | flag. Only one copy of the key will be stored for each set of |
| 277 | duplicate data items. If the Btree access method comparison routine |
| 278 | returns that two keys compare equally, it is undefined which of the two |
| 279 | keys will be stored and returned from future database operations. |
| 280 | </p> |
| 281 | <p> |
| 282 | By default, Berkeley DB stores duplicates in the order in which they |
| 283 | were added, that is, each new duplicate data item will be stored after |
| 284 | any already existing data items. This default behavior can be |
| 285 | overridden by using the <a href="../api_reference/C/dbcput.html" class="olink">DBC->put()</a> method and one of the <a href="../api_reference/C/dbcput.html#dbcput_DB_AFTER" class="olink">DB_AFTER</a>, |
| 286 | <a href="../api_reference/C/dbcput.html#dbcput_DB_BEFORE" class="olink">DB_BEFORE</a>, <a href="../api_reference/C/dbcput.html#dbcput_DB_KEYFIRST" class="olink">DB_KEYFIRST</a> or <a href="../api_reference/C/dbcput.html#dbcput_DB_KEYLAST" class="olink">DB_KEYLAST</a> flags. Alternatively, |
| 287 | Berkeley DB may be configured to sort duplicate data items. |
| 288 | </p> |
| 289 | <p> |
| 290 | When stepping through the database sequentially, duplicate data items |
| 291 | will be returned individually, as a key/data pair, where the key item |
| 292 | only changes after the last duplicate data item has been returned. For |
| 293 | this reason, duplicate data items cannot be accessed using the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> |
| 294 | method, as it always returns the first of the duplicate data items. |
| 295 | Duplicate data items should be retrieved using a Berkeley DB cursor |
| 296 | interface such as the <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> method. |
| 297 | </p> |
| 298 | <p> |
| 299 | There is a flag that permits applications to request the following data |
| 300 | item only if it <span class="bold"><strong>is</strong></span> a duplicate data |
| 301 | item of the current entry, see <a href="../api_reference/C/dbcget.html#dbcget_DB_NEXT_DUP" class="olink">DB_NEXT_DUP</a> for more information. |
| 302 | There is a flag that permits applications to request the following data |
| 303 | item only if it <span class="bold"><strong>is not</strong></span> a duplicate |
| 304 | data item of the current entry, see <a href="../api_reference/C/dbcget.html#dbcget_DB_NEXT_NODUP" class="olink">DB_NEXT_NODUP</a> and <a href="../api_reference/C/dbcget.html#dbcget_DB_PREV_NODUP" class="olink">DB_PREV_NODUP</a> |
| 305 | for more information. |
| 306 | </p> |
| 307 | <p> |
| 308 | It is also possible to maintain duplicate records in sorted order. |
| 309 | Sorting duplicates will significantly increase performance when |
| 310 | searching them and performing equality joins — both of which are |
| 311 | common operations when using secondary indices. To configure Berkeley |
| 312 | DB to sort duplicate data items, the application must call the |
| 313 | <a href="../api_reference/C/dbset_flags.html" class="olink">DB->set_flags()</a> method with the <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_DUPSORT" class="olink">DB_DUPSORT</a> flag. Note that <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_DUPSORT" class="olink">DB_DUPSORT</a> |
| 314 | automatically turns on the <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_DUP" class="olink">DB_DUP</a> flag for you, so you do not |
| 315 | have to also set that flag; however, it is not an error to also set <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_DUP" class="olink">DB_DUP</a> |
| 316 | when configuring for sorted duplicate records. |
| 317 | </p> |
| 318 | <p> |
| 319 | When configuring sorted duplicate records, you can also specify a |
| 320 | custom comparison function using the <a href="../api_reference/C/dbset_dup_compare.html" class="olink">DB->set_dup_compare()</a> method. If |
| 321 | the <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_DUPSORT" class="olink">DB_DUPSORT</a> flag is given, but no comparison routine is specified, |
| 322 | then Berkeley DB defaults to the same lexicographical sorting used for |
| 323 | Btree keys, with shorter items collating before longer items. |
| 324 | </p> |
| 325 | <p> |
| 326 | If the duplicate data items are unsorted, applications may store |
| 327 | identical duplicate data items, or, for those that just like the way it |
| 328 | sounds, <span class="emphasis"><em>duplicate duplicates</em></span>. |
| 329 | </p> |
| 330 | <p> |
| 331 | <span class="bold"><strong>It is an error to attempt to store identical |
| 332 | duplicate data items when duplicates are being stored in a sorted |
| 333 | order.</strong></span> Any such attempt results in the |
| 334 | error message "Duplicate data items are not supported with sorted |
| 335 | data" with a <code class="literal">DB_KEYEXIST</code> return code. |
| 336 | </p> |
| 337 | <p> |
| 338 | Note that you can suppress the error message "Duplicate data items are |
| 339 | not supported with sorted data" by using the <a href="../api_reference/C/dbput.html#put_DB_NODUPDATA" class="olink">DB_NODUPDATA</a> flag. Use |
| 340 | of this flag does not change the database's basic behavior; storing |
| 341 | duplicate data items in a database configured for sorted duplicates is |
| 342 | still an error and so you will continue to receive the |
| 343 | <code class="literal">DB_KEYEXIST</code> return code if you try to do that. |
| 344 | </p> |
| 345 | <p> |
| 346 | For further information on how searching and insertion behaves in the |
| 347 | presence of duplicates (sorted or not), see the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> <a href="../api_reference/C/dbput.html" class="olink">DB->put()</a>, |
| 348 | <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> and <a href="../api_reference/C/dbcput.html" class="olink">DBC->put()</a> documentation. |
| 349 | </p> |
| 350 | </div> |
| 351 | <div class="sect2" lang="en" xml:lang="en"> |
| 352 | <div class="titlepage"> |
| 353 | <div> |
| 354 | <div> |
| 355 | <h3 class="title"><a id="am_conf_malloc"></a>Non-local memory allocation</h3> |
| 356 | </div> |
| 357 | </div> |
| 358 | </div> |
| 359 | <p>Berkeley DB allocates memory for returning key/data pairs and statistical |
| 360 | information which becomes the responsibility of the application. |
| 361 | There are also interfaces where an application will allocate memory |
| 362 | which becomes the responsibility of Berkeley DB.</p> |
| 363 | <p>On systems in which there may be multiple library versions of the |
| 364 | standard allocation routines (notably Windows NT), transferring memory |
| 365 | between the library and the application will fail because the Berkeley DB |
| 366 | library allocates memory from a different heap than the application |
| 367 | uses to free it, or vice versa. To avoid this problem, the |
| 368 | <a href="../api_reference/C/envset_alloc.html" class="olink">DB_ENV->set_alloc()</a> and <a href="../api_reference/C/dbset_alloc.html" class="olink">DB->set_alloc()</a> methods can be used to |
| 369 | give Berkeley DB references to the application's allocation routines.</p> |
| 370 | </div> |
| 371 | </div> |
| 372 | <div class="navfooter"> |
| 373 | <hr /> |
| 374 | <table width="100%" summary="Navigation footer"> |
| 375 | <tr> |
| 376 | <td width="40%" align="left"><a accesskey="p" href="am_conf_logrec.html">Prev</a> </td> |
| 377 | <td width="20%" align="center"> |
| 378 | <a accesskey="u" href="am_conf.html">Up</a> |
| 379 | </td> |
| 380 | <td width="40%" align="right"> <a accesskey="n" href="bt_conf.html">Next</a></td> |
| 381 | </tr> |
| 382 | <tr> |
| 383 | <td width="40%" align="left" valign="top">Logical record numbers </td> |
| 384 | <td width="20%" align="center"> |
| 385 | <a accesskey="h" href="index.html">Home</a> |
| 386 | </td> |
| 387 | <td width="40%" align="right" valign="top"> Btree access method specific configuration</td> |
| 388 | </tr> |
| 389 | </table> |
| 390 | </div> |
| 391 | </body> |
| 392 | </html> |