| 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 |
<code class="methodname">DB->set_flags()</code> |
| 76 |
|
| 77 |
method. |
| 78 |
</span> |
| 79 |
|
| 80 |
</p> |
| 81 |
<p> |
| 82 |
For example: |
| 83 |
</p> |
| 84 |
<pre class="programlisting">#include <stdio.h> |
| 85 |
#include <stdlib.h> |
| 86 |
|
| 87 |
#include "db.h" |
| 88 |
|
| 89 |
int |
| 90 |
main(void) |
| 91 |
{ |
| 92 |
int ret, ret_c; |
| 93 |
u_int32_t db_flags, env_flags; |
| 94 |
DB *dbp; |
| 95 |
DB_ENV *envp; |
| 96 |
const char *db_home_dir = "/tmp/myEnvironment"; |
| 97 |
const char *file_name = "mydb.db"; |
| 98 |
|
| 99 |
dbp = NULL; |
| 100 |
envp = NULL; |
| 101 |
|
| 102 |
/* Open the environment */ |
| 103 |
ret = db_env_create(&envp, 0); |
| 104 |
if (ret != 0) { |
| 105 |
fprintf(stderr, "Error creating environment handle: %s\n", |
| 106 |
db_strerror(ret)); |
| 107 |
return (EXIT_FAILURE); |
| 108 |
} |
| 109 |
|
| 110 |
env_flags = DB_CREATE | |
| 111 |
DB_INIT_LOCK | |
| 112 |
DB_INIT_LOG | |
| 113 |
DB_INIT_TXN | |
| 114 |
DB_THREAD | |
| 115 |
DB_INIT_MPOOL; |
| 116 |
|
| 117 |
ret = envp->open(envp, db_home_dir, env_flags, 0); |
| 118 |
if (ret != 0) { |
| 119 |
fprintf(stderr, "Error opening environment: %s\n", |
| 120 |
db_strerror(ret)); |
| 121 |
goto err; |
| 122 |
} |
| 123 |
|
| 124 |
/* Initialize the DB handle */ |
| 125 |
ret = db_create(&dbp, envp, 0); |
| 126 |
if (ret != 0) { |
| 127 |
envp->err(envp, ret, "Database creation failed"); |
| 128 |
goto err; |
| 129 |
} |
| 130 |
|
| 131 |
/* Set btree reverse split to off */ |
| 132 |
ret = db->set_flags(&db, DB_REVSPLITOFF); |
| 133 |
if (ret != 0) { |
| 134 |
envp->err(envp, ret, "Turning off Btree reverse split failed"); |
| 135 |
goto err; |
| 136 |
} |
| 137 |
|
| 138 |
db_flags = DB_CREATE | DB_AUTO_COMMIT; |
| 139 |
ret = dbp->open(dbp, /* Pointer to the database */ |
| 140 |
NULL, /* Txn pointer */ |
| 141 |
file_name, /* File name */ |
| 142 |
NULL, /* Logical db name */ |
| 143 |
DB_BTREE, /* Database type (using btree) */ |
| 144 |
db_flags, /* Open flags */ |
| 145 |
0); /* File mode. Using defaults */ |
| 146 |
if (ret != 0) { |
| 147 |
envp->err(envp, ret, "Database '%s' open failed", |
| 148 |
file_name); |
| 149 |
goto err; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
err: |
| 154 |
/* Close the database */ |
| 155 |
if (dbp != NULL) { |
| 156 |
ret_c = dbp->close(dbp, 0); |
| 157 |
if (ret_c != 0) { |
| 158 |
envp->err(envp, ret_c, "Database close failed."); |
| 159 |
ret = ret_c |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/* Close the environment */ |
| 165 |
if (envp != NULL) { |
| 166 |
ret_c = envp->close(envp, 0); |
| 167 |
if (ret_c != 0) { |
| 168 |
fprintf(stderr, "environment close failed: %s\n", |
| 169 |
db_strerror(ret_c)); |
| 170 |
ret = ret_c; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 175 |
} </pre> |
| 176 |
</div> |
| 177 |
<div class="navfooter"> |
| 178 |
<hr /> |
| 179 |
<table width="100%" summary="Navigation footer"> |
| 180 |
<tr> |
| 181 |
<td width="40%" align="left"><a accesskey="p" href="txnnowait.html">Prev</a> </td> |
| 182 |
<td width="20%" align="center"> |
| 183 |
<a accesskey="u" href="txnconcurrency.html">Up</a> |
| 184 |
</td> |
| 185 |
<td width="40%" align="right"> <a accesskey="n" href="filemanagement.html">Next</a></td> |
| 186 |
</tr> |
| 187 |
<tr> |
| 188 |
<td width="40%" align="left" valign="top">No Wait on Blocks </td> |
| 189 |
<td width="20%" align="center"> |
| 190 |
<a accesskey="h" href="index.html">Home</a> |
| 191 |
</td> |
| 192 |
<td width="40%" align="right" valign="top"> Chapter 5. Managing DB Files</td> |
| 193 |
</tr> |
| 194 |
</table> |
| 195 |
</div> |
| 196 |
</body> |
| 197 |
</html> |