Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / docs / gsg / JAVA / accessmethods.html
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 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">Access Methods</th>
22         </tr>
23         <tr>
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>
27         </tr>
28       </table>
29       <hr />
30     </div>
31     <div class="sect1" lang="en" xml:lang="en">
32       <div class="titlepage">
33         <div>
34           <div>
35             <h2 class="title" style="clear: both"><a id="accessmethods"></a>Access Methods</h2>
36           </div>
37         </div>
38       </div>
39       <div class="toc">
40         <dl>
41           <dt>
42             <span class="sect2">
43               <a href="accessmethods.html#selectAM">Selecting Access Methods</a>
44             </span>
45           </dt>
46           <dt>
47             <span class="sect2">
48               <a href="accessmethods.html#BTreeVSHash">Choosing between BTree and Hash</a>
49             </span>
50           </dt>
51           <dt>
52             <span class="sect2">
53               <a href="accessmethods.html#QueueVSRecno">Choosing between Queue and Recno</a>
54             </span>
55           </dt>
56         </dl>
57       </div>
58       <p>
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
61         makes available.
62     </p>
63       <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
64         <h3 class="title">Note</h3>
65         <p>
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.
69             </p>
70       </div>
71       <p>
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.
77     </p>
78       <p>
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. 
82     </p>
83       <p>
84         The following are the available access methods:
85     </p>
86       <div class="informaltable">
87         <table border="1" width="80%">
88           <colgroup>
89             <col align="left" />
90             <col align="left" />
91           </colgroup>
92           <thead>
93             <tr>
94               <th align="center">Access Method</th>
95               <th align="center">Description</th>
96             </tr>
97           </thead>
98           <tbody>
99             <tr>
100               <td align="left">BTree</td>
101               <td align="left" valign="top">
102                 <p>
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.
111                 </p>
112             </td>
113             </tr>
114             <tr>
115               <td align="left">Hash</td>
116               <td align="left" valign="top">
117                 <p>
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.
122                 </p>
123             </td>
124             </tr>
125             <tr>
126               <td align="left">Queue</td>
127               <td align="left" valign="top">
128                 <p>
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.
134                 </p>
135                 <p>
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.
140                 </p>
141             </td>
142             </tr>
143             <tr>
144               <td align="left">Recno</td>
145               <td align="left" valign="top">
146                 <p>
147                     Data is stored in either fixed or variable-length records.
148                     Like Queue, Recno records use logical record numbers as keys.
149                 </p>
150             </td>
151             </tr>
152           </tbody>
153         </table>
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="selectAM"></a>Selecting Access Methods</h3>
160             </div>
161           </div>
162         </div>
163         <p>
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.
169         </p>
170         <p>
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.
173         </p>
174       </div>
175       <div class="sect2" lang="en" xml:lang="en">
176         <div class="titlepage">
177           <div>
178             <div>
179               <h3 class="title"><a id="BTreeVSHash"></a>Choosing between BTree and Hash</h3>
180             </div>
181           </div>
182         </div>
183         <p>
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
188             BTree.
189          </p>
190         <p>
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.
197          </p>
198         <p>
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:
202         </p>
203         <div class="itemizedlist">
204           <ul type="disc">
205             <li>
206               <p>
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
210                     neighbors. 
211                 </p>
212             </li>
213             <li>
214               <p>
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. 
224                 </p>
225               <p>
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
230                     does BTree.
231                 </p>
232             </li>
233           </ul>
234         </div>
235       </div>
236       <div class="sect2" lang="en" xml:lang="en">
237         <div class="titlepage">
238           <div>
239             <div>
240               <h3 class="title"><a id="QueueVSRecno"></a>Choosing between Queue and Recno</h3>
241             </div>
242           </div>
243         </div>
244         <p>
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.
252         </p>
253         <p>
254             When deciding between Queue and Recno, choose:
255         </p>
256         <div class="itemizedlist">
257           <ul type="disc">
258             <li>
259               <p>
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.
265                 </p>
266               <p>
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.
271                 </p>
272             </li>
273             <li>
274               <p>
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
280                     modified.
281                 </p>
282             </li>
283           </ul>
284         </div>
285       </div>
286     </div>
287     <div class="navfooter">
288       <hr />
289       <table width="100%" summary="Navigation footer">
290         <tr>
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>
294           </td>
295           <td width="40%" align="right"> <a accesskey="n" href="databaseLimits.html">Next</a></td>
296         </tr>
297         <tr>
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>
301           </td>
302           <td width="40%" align="right" valign="top"> Database Limits and Portability</td>
303         </tr>
304       </table>
305     </div>
306   </body>
307 </html>