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