| 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>Reverse BTree Splits</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 Transaction Processing" /> |
| 10 |
<link rel="up" href="txnconcurrency.html" title="Chapter 4. Concurrency" /> |
| 11 |
<link rel="prev" href="txnnowait.html" title="No Wait on Blocks" /> |
| 12 |
<link rel="next" href="filemanagement.html" title="Chapter 5. Managing DB Files" /> |
| 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">Reverse BTree Splits</th> |
| 22 |
</tr> |
| 23 |
<tr> |
| 24 |
<td width="20%" align="left"><a accesskey="p" href="txnnowait.html">Prev</a> </td> |
| 25 |
<th width="60%" align="center">Chapter 4. Concurrency</th> |
| 26 |
<td width="20%" align="right"> <a accesskey="n" href="filemanagement.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="reversesplit"></a>Reverse BTree Splits</h2> |
| 36 |
</div> |
| 37 |
</div> |
| 38 |
</div> |
| 39 |
<p> |
| 40 |
If your application is using the Btree access method, and your |
| 41 |
application is repeatedly deleting then adding records to your |
| 42 |
database, then you might be able to reduce lock contention by |
| 43 |
turning off reverse Btree splits. |
| 44 |
</p> |
| 45 |
<p> |
| 46 |
As pages are emptied in a database, DB attempts to |
| 47 |
delete empty pages in order to keep |
| 48 |
the database as small as possible and minimize search time. |
| 49 |
Moreover, when a page in the database fills |
| 50 |
up, DB, of course, adds additional pages |
| 51 |
to make room for more data. |
| 52 |
</p> |
| 53 |
<p> |
| 54 |
Adding and deleting pages in the database requires that the |
| 55 |
writing thread lock the parent page. Consequently, as the |
| 56 |
number of pages in your database diminishes, your application |
| 57 |
will see increasingly more lock contention; the maximum level |
| 58 |
of concurrency in a database of two pages is far smaller than |
| 59 |
that in a database of 100 pages, because there are fewer pages |
| 60 |
that can be locked. |
| 61 |
</p> |
| 62 |
<p> |
| 63 |
Therefore, if you prevent the database from being reduced to a |
| 64 |
minimum number of pages, you can improve your application's |
| 65 |
concurrency throughput. Note, however, that you should do so |
| 66 |
only if your application tends to delete and then add the same |
| 67 |
data. If this is not the case, then preventing reverse Btree |
| 68 |
splits can harm your database search time. |
| 69 |
</p> |
| 70 |
<p> |
| 71 |
To turn off reverse Btree splits, |
| 72 |
<span> |
| 73 |
provide the |
| 74 |
<code class="literal">DB_REVSPLITOFF</code> flag to the |
| 75 |
|
| 76 |
<code class="methodname">Db::set_flags()</code> |
| 77 |
method. |
| 78 |
</span> |
| 79 |
|
| 80 |
</p> |
| 81 |
<p> |
| 82 |
For example: |
| 83 |
</p> |
| 84 |
<pre class="programlisting">#include "db_cxx.h" |
| 85 |
|
| 86 |
... |
| 87 |
|
| 88 |
int main(void) |
| 89 |
{ |
| 90 |
u_int32_t env_flags = DB_CREATE | // If the environment does not |
| 91 |
// exist, create it. |
| 92 |
DB_INIT_LOCK | // Initialize locking |
| 93 |
DB_INIT_LOG | // Initialize locking |
| 94 |
DB_INIT_MPOOL | // Initialize the cache |
| 95 |
DB_THREAD | // Free-thread the env handle |
| 96 |
DB_INIT_TXN; // Initialize transactions |
| 97 |
|
| 98 |
u_int32_t db_flags = DB_CREATE | DB_AUTO_COMMIT; |
| 99 |
Db *dbp = NULL; |
| 100 |
const char *file_name = "mydb.db"; |
| 101 |
|
| 102 |
std::string envHome("/export1/testEnv"); |
| 103 |
DbEnv myEnv(0); |
| 104 |
|
| 105 |
try { |
| 106 |
|
| 107 |
myEnv.open(envHome.c_str(), env_flags, 0); |
| 108 |
dbp = new Db(&myEnv, 0); |
| 109 |
|
| 110 |
// Turn off BTree reverse split. |
| 111 |
dbp=>set_flags(DB_REVSPLITOFF); |
| 112 |
|
| 113 |
dbp->open(dbp, // Pointer to the database |
| 114 |
NULL, // Txn pointer |
| 115 |
file_name, // File name |
| 116 |
NULL, // Logical db name |
| 117 |
DB_BTREE, // Database type (using btree) |
| 118 |
db_flags, // Open flags |
| 119 |
0); // File mode. Using defaults |
| 120 |
|
| 121 |
} catch(DbException &e) { |
| 122 |
std::cerr << "Error opening database and environment: " |
| 123 |
<< file_name << ", " << envHome << std::endl; |
| 124 |
std::cerr << e.what() << std::endl; |
| 125 |
} |
| 126 |
|
| 127 |
try { |
| 128 |
dbp->close(dbp, 0); |
| 129 |
myEnv.close(0); |
| 130 |
} catch(DbException &e) { |
| 131 |
std::cerr << "Error closing database and environment: " |
| 132 |
<< file_name << ", " << envHome << std::endl; |
| 133 |
std::cerr << e.what() << std::endl; |
| 134 |
return (EXIT_FAILURE); |
| 135 |
} |
| 136 |
|
| 137 |
return (EXIT_SUCCESS); |
| 138 |
} </pre> |
| 139 |
</div> |
| 140 |
<div class="navfooter"> |
| 141 |
<hr /> |
| 142 |
<table width="100%" summary="Navigation footer"> |
| 143 |
<tr> |
| 144 |
<td width="40%" align="left"><a accesskey="p" href="txnnowait.html">Prev</a> </td> |
| 145 |
<td width="20%" align="center"> |
| 146 |
<a accesskey="u" href="txnconcurrency.html">Up</a> |
| 147 |
</td> |
| 148 |
<td width="40%" align="right"> <a accesskey="n" href="filemanagement.html">Next</a></td> |
| 149 |
</tr> |
| 150 |
<tr> |
| 151 |
<td width="40%" align="left" valign="top">No Wait on Blocks </td> |
| 152 |
<td width="20%" align="center"> |
| 153 |
<a accesskey="h" href="index.html">Home</a> |
| 154 |
</td> |
| 155 |
<td width="40%" align="right" valign="top"> Chapter 5. Managing DB Files</td> |
| 156 |
</tr> |
| 157 |
</table> |
| 158 |
</div> |
| 159 |
</body> |
| 160 |
</html> |