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">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Access Methods</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="Getting Started with Berkeley DB" />
10 <link rel="up" href="introduction.html" title="Chapter 1. Introduction to Berkeley DB" />
11 <link rel="prev" href="javadplconcepts.html" title="Berkeley DB Concepts" />
12 <link rel="next" href="databaseLimits.html" title="Database Limits and Portability" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Access Methods</th>
24 <td width="20%" align="left"><a accesskey="p" href="javadplconcepts.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 1. Introduction to Berkeley DB </th>
26 <td width="20%" align="right"> <a accesskey="n" href="databaseLimits.html">Next</a></td>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title" style="clear: both"><a id="accessmethods"></a>Access Methods</h2>
43 <a href="accessmethods.html#selectAM">Selecting Access Methods</a>
48 <a href="accessmethods.html#BTreeVSHash">Choosing between BTree and Hash</a>
53 <a href="accessmethods.html#QueueVSRecno">Choosing between Queue and Recno</a>
59 While this manual will focus primarily on the BTree access method, it is
60 still useful to briefly describe all of the access methods that DB
63 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
64 <h3 class="title">Note</h3>
66 If you are using the DPL, be aware that it only
67 supports the BTree access method. For that reason, you
68 can skip this section.
72 Note that an access method can be selected only when the database is
73 created. Once selected, actual API usage is generally
74 identical across all access methods. That is, while some
75 exceptions exist, mechanically you interact with the library in the same
76 way regardless of which access method you have selected.
79 The access method that you should choose is gated first by what you want
80 to use as a key, and then secondly by the performance that you see
81 for a given access method.
84 The following are the available access methods:
86 <div class="informaltable">
87 <table border="1" width="80%">
94 <th align="center">Access Method</th>
95 <th align="center">Description</th>
100 <td align="left">BTree</td>
101 <td align="left" valign="top">
103 Data is stored in a sorted, balanced tree structure.
104 Both the key and the data for BTree records can be
105 arbitrarily complex. That is, they can contain single values
106 such as an integer or a string, or complex types such as a
107 structure. Also, although not the default
108 behavior, it is possible for two records to
109 use keys that compare as equals. When this occurs, the
110 records are considered to be duplicates of one another.
115 <td align="left">Hash</td>
116 <td align="left" valign="top">
118 Data is stored in an extended linear hash table. Like
119 BTree, the key and the data used for Hash records can be of
120 arbitrarily complex data. Also, like BTree, duplicate
121 records are optionally supported.
126 <td align="left">Queue</td>
127 <td align="left" valign="top">
129 Data is stored in a queue as fixed-length records. Each
130 record uses a logical record number as its key. This access
131 method is designed for fast inserts at the tail of the
132 queue, and it has a special operation that deletes and
133 returns a record from the head of the queue.
136 This access method is unusual in that it provides record
137 level locking. This can provide
138 beneficial performance improvements in applications
139 requiring concurrent access to the queue.
144 <td align="left">Recno</td>
145 <td align="left" valign="top">
147 Data is stored in either fixed or variable-length records.
148 Like Queue, Recno records use logical record numbers as keys.
155 <div class="sect2" lang="en" xml:lang="en">
156 <div class="titlepage">
159 <h3 class="title"><a id="selectAM"></a>Selecting Access Methods</h3>
164 To select an access method, you should first consider what you want
165 to use as a key for you database records. If you want to use
166 arbitrary data (even strings), then you should use either BTree or
167 Hash. If you want to use logical record numbers (essentially
168 integers) then you should use Queue or Recno.
171 Once you have made this decision, you must choose between either
172 BTree or Hash, or Queue or Recno. This decision is described next.
175 <div class="sect2" lang="en" xml:lang="en">
176 <div class="titlepage">
179 <h3 class="title"><a id="BTreeVSHash"></a>Choosing between BTree and Hash</h3>
184 For small working datasets that fit entirely in memory, there is no
185 difference between BTree and Hash. Both will perform just as well
186 as the other. In this situation, you might just as well use BTree,
187 if for no other reason than the majority of DB applications use
191 Note that the main concern here is your
192 working dataset, not your entire dataset. Many applications maintain
193 large amounts of information but only need to access some small
194 portion of that data with any frequency. So what you want to
195 consider is the data that you will routinely use, not the sum total
196 of all the data managed by your application.
199 However, as your working dataset grows to the point
200 where you cannot fit it all into memory, then you need to take more
201 care when choosing your access method. Specifically, choose:
203 <div class="itemizedlist">
207 BTree if your keys have some locality of reference. That is,
208 if they sort well and you can expect that a query for a
209 given key will likely be followed by a query for one of its
215 Hash if your dataset is extremely large. For any given
216 access method, DB must maintain a certain amount of internal
217 information. However, the amount of information that DB
218 must maintain for BTree is much greater than for Hash. The
219 result is that as your dataset grows, this internal
220 information can dominate the cache to the point where there
221 is relatively little space left for application data.
222 As a result, BTree can be forced to perform disk I/O much more
223 frequently than would Hash given the same amount of data.
226 Moreover, if your dataset becomes so large that DB will
227 almost certainly have to perform disk I/O to satisfy a
228 random request, then Hash will definitely out perform BTree
229 because it has fewer internal records to search through than
236 <div class="sect2" lang="en" xml:lang="en">
237 <div class="titlepage">
240 <h3 class="title"><a id="QueueVSRecno"></a>Choosing between Queue and Recno</h3>
245 Queue or Recno are used when the application wants to use logical
246 record numbers for the primary database key. Logical record numbers
247 are essentially integers that uniquely identify the database
248 record. They can be either mutable or fixed, where a mutable record
249 number is one that might change as database records are stored or
250 deleted. Fixed logical record numbers never change regardless of
251 what database operations are performed.
254 When deciding between Queue and Recno, choose:
256 <div class="itemizedlist">
260 Queue if your application requires high degrees of
261 concurrency. Queue provides record-level locking (as opposed
262 to the page-level locking that the other access methods
263 use), and this can result in significantly faster throughput
264 for highly concurrent applications.
267 Note, however, that Queue provides support only for fixed
268 length records. So if the size of the data that you want to
269 store varies widely from record to record, you should
270 probably choose an access method other than Queue.
275 Recno if you want mutable record numbers. Queue is only
276 capable of providing fixed record numbers. Also, Recno
277 provides support for databases whose permanent storage is a
278 flat text file. This is useful for applications looking for
279 fast, temporary storage while the data is being read or
287 <div class="navfooter">
289 <table width="100%" summary="Navigation footer">
291 <td width="40%" align="left"><a accesskey="p" href="javadplconcepts.html">Prev</a> </td>
292 <td width="20%" align="center">
293 <a accesskey="u" href="introduction.html">Up</a>
295 <td width="40%" align="right"> <a accesskey="n" href="databaseLimits.html">Next</a></td>
298 <td width="40%" align="left" valign="top">Berkeley DB Concepts </td>
299 <td width="20%" align="center">
300 <a accesskey="h" href="index.html">Home</a>
302 <td width="40%" align="right" valign="top"> Database Limits and Portability</td>