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>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" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Access Methods</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="javadplconcepts.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��1.��Introduction to Berkeley DB </th>
23          <td width="20%" align="right">��<a accesskey="n" href="databaseLimits.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="accessmethods"></a>Access Methods</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <dl>
38          <dt>
39            <span class="sect2">
40              <a href="accessmethods.html#selectAM">Selecting Access Methods</a>
41            </span>
42          </dt>
43          <dt>
44            <span class="sect2">
45              <a href="accessmethods.html#BTreeVSHash">Choosing between BTree and Hash</a>
46            </span>
47          </dt>
48          <dt>
49            <span class="sect2">
50              <a href="accessmethods.html#QueueVSRecno">Choosing between Queue and Recno</a>
51            </span>
52          </dt>
53        </dl>
54      </div>
55      <p>
56        While this manual will focus primarily on the BTree access method, it is
57        still useful to briefly describe all of the access methods that DB
58        makes available.
59    </p>
60      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
61        <h3 class="title">Note</h3>
62        <p>
63                    If you are using the DPL, be aware that it only
64                    supports the BTree access method. For that reason, you
65                    can skip this section.
66            </p>
67      </div>
68      <p>
69        Note that an access method can be selected only when the database is
70        created. Once selected,  actual API usage is generally
71        identical across all access methods. That is, while some 
72        exceptions exist, mechanically you interact with the library in the same
73        way regardless of which access method you have selected.
74    </p>
75      <p>
76        The access method that you should choose is gated first by what you want
77        to use as a key, and then secondly by the performance that you see
78        for a given access method. 
79    </p>
80      <p>
81        The following are the available access methods:
82    </p>
83      <div class="informaltable">
84        <table border="1" width="80%">
85          <colgroup>
86            <col align="left" />
87            <col align="left" />
88          </colgroup>
89          <thead>
90            <tr>
91              <th align="center">Access Method</th>
92              <th align="center">Description</th>
93            </tr>
94          </thead>
95          <tbody>
96            <tr>
97              <td align="left">BTree</td>
98              <td align="left" valign="top">
99                <p>
100                    Data is stored in a sorted, balanced tree structure. 
101                    Both the key and the data for BTree records can be
102                    arbitrarily complex. That is, they can contain single values
103                    such as an integer or a string, or complex types such as a
104                    structure. Also, although not the default
105                    behavior, it is possible for two records to
106                    use keys that compare as equals. When this occurs, the
107                    records are considered to be duplicates of one another.
108                </p>
109            </td>
110            </tr>
111            <tr>
112              <td align="left">Hash</td>
113              <td align="left" valign="top">
114                <p>
115                    Data is stored in an extended linear hash table.  Like
116                    BTree, the key and the data used for Hash records can be of
117                    arbitrarily complex data.  Also, like BTree, duplicate
118                    records are optionally supported.
119                </p>
120            </td>
121            </tr>
122            <tr>
123              <td align="left">Queue</td>
124              <td align="left" valign="top">
125                <p>
126                    Data is stored in a queue as fixed-length records. Each
127                    record uses a logical record number as its key. This access
128                    method is designed for fast inserts at the tail of the
129                    queue, and it has a special operation that deletes and
130                    returns a record from the head of the queue.
131                </p>
132                <p>
133                    This access method is unusual in that it provides record
134                    level locking. This can provide
135                    beneficial performance improvements in applications
136                    requiring concurrent access to the queue.
137                </p>
138            </td>
139            </tr>
140            <tr>
141              <td align="left">Recno</td>
142              <td align="left" valign="top">
143                <p>
144                    Data is stored in either fixed or variable-length records.
145                    Like Queue, Recno records use logical record numbers as keys.
146                </p>
147            </td>
148            </tr>
149          </tbody>
150        </table>
151      </div>
152      <div class="sect2" lang="en" xml:lang="en">
153        <div class="titlepage">
154          <div>
155            <div>
156              <h3 class="title"><a id="selectAM"></a>Selecting Access Methods</h3>
157            </div>
158          </div>
159        </div>
160        <p>
161            To select an access method, you should first consider what you want
162            to use as a key for you database records. If you want to use
163            arbitrary data (even strings), then you should use either BTree or
164            Hash. If you want to use logical record numbers (essentially
165            integers) then you should use Queue or Recno.
166        </p>
167        <p>
168            Once you have made this decision, you must choose between either
169            BTree or Hash, or Queue or Recno. This decision is described next.
170        </p>
171      </div>
172      <div class="sect2" lang="en" xml:lang="en">
173        <div class="titlepage">
174          <div>
175            <div>
176              <h3 class="title"><a id="BTreeVSHash"></a>Choosing between BTree and Hash</h3>
177            </div>
178          </div>
179        </div>
180        <p>
181            For small working datasets that fit entirely in memory, there is no
182            difference between BTree and Hash. Both will perform just as well
183            as the other. In this situation, you might just as well use BTree,
184            if for no other reason than the majority of DB applications use
185            BTree.
186         </p>
187        <p>
188            Note that the main concern here is your
189            working dataset, not your entire dataset. Many applications maintain
190            large amounts of information but only need to access some small
191            portion of that data with any frequency. So what you want to
192            consider is the data that you will routinely use, not the sum total
193            of all the data managed by your application.
194         </p>
195        <p>
196            However, as your working dataset grows to the point
197            where you cannot fit it all into memory, then you need to take more
198            care when choosing your access method. Specifically, choose:
199        </p>
200        <div class="itemizedlist">
201          <ul type="disc">
202            <li>
203              <p>
204                    BTree if your keys have some locality of reference. That is,
205                    if they sort well and you can expect that a query for a
206                    given key will likely be followed by a query for one of its
207                    neighbors. 
208                </p>
209            </li>
210            <li>
211              <p>
212                    Hash if your dataset is extremely large. For any given
213                    access method, DB must maintain a certain amount of internal
214                    information. However, the amount of information that DB
215                    must maintain for BTree is much greater than for Hash. The
216                    result is that as your dataset grows, this internal
217                    information can dominate the cache to the point where there
218                    is relatively little space left for application data. 
219                    As a result, BTree can be forced to perform disk I/O much more
220                    frequently than would Hash given the same amount of data. 
221                </p>
222              <p>
223                    Moreover, if your dataset becomes so large that DB will
224                    almost certainly have to perform disk I/O to satisfy a
225                    random request, then Hash will definitely out perform BTree
226                    because it has fewer internal records to search through than
227                    does BTree.
228                </p>
229            </li>
230          </ul>
231        </div>
232      </div>
233      <div class="sect2" lang="en" xml:lang="en">
234        <div class="titlepage">
235          <div>
236            <div>
237              <h3 class="title"><a id="QueueVSRecno"></a>Choosing between Queue and Recno</h3>
238            </div>
239          </div>
240        </div>
241        <p>
242            Queue or Recno are used when the application wants to use logical
243            record numbers for the primary database key. Logical record numbers
244            are essentially integers that uniquely identify the database
245            record. They can be either mutable or fixed, where a mutable record
246            number is one that might change as database records are stored or
247            deleted. Fixed logical record numbers never change regardless of
248            what database operations are performed.
249        </p>
250        <p>
251            When deciding between Queue and Recno, choose:
252        </p>
253        <div class="itemizedlist">
254          <ul type="disc">
255            <li>
256              <p>
257                    Queue if your application requires high degrees of
258                    concurrency. Queue provides record-level locking (as opposed
259                    to the page-level locking that the other access methods
260                    use), and this can result in significantly faster throughput
261                    for highly concurrent applications.
262                </p>
263              <p>
264                    Note, however, that Queue provides support only for fixed
265                    length records. So if the size of the data that you want to
266                    store varies widely from record to record, you should
267                    probably choose an access method other than Queue.
268                </p>
269            </li>
270            <li>
271              <p>
272                    Recno if you want mutable record numbers. Queue is only
273                    capable of providing fixed record numbers. Also, Recno
274                    provides support for databases whose permanent storage is a
275                    flat text file. This is useful for applications looking for
276                    fast, temporary storage while the data is being read or
277                    modified.
278                </p>
279            </li>
280          </ul>
281        </div>
282      </div>
283    </div>
284    <div class="navfooter">
285      <hr />
286      <table width="100%" summary="Navigation footer">
287        <tr>
288          <td width="40%" align="left"><a accesskey="p" href="javadplconcepts.html">Prev</a>��</td>
289          <td width="20%" align="center">
290            <a accesskey="u" href="introduction.html">Up</a>
291          </td>
292          <td width="40%" align="right">��<a accesskey="n" href="databaseLimits.html">Next</a></td>
293        </tr>
294        <tr>
295          <td width="40%" align="left" valign="top">Berkeley DB Concepts��</td>
296          <td width="20%" align="center">
297            <a accesskey="h" href="index.html">Home</a>
298          </td>
299          <td width="40%" align="right" valign="top">��Database Limits and Portability</td>
300        </tr>
301      </table>
302    </div>
303  </body>
304</html>
305