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