| 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 method tuning</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="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="am_misc.html" title="Chapter 4. Access Method Wrapup" /> |
| 11 | <link rel="prev" href="am_misc_db_sql.html" title="Specifying a Berkeley DB schema using SQL DDL" /> |
| 12 | <link rel="next" href="am_misc_faq.html" title="Access method FAQ" /> |
| 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 method tuning</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="am_misc_db_sql.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 4. |
| 26 | Access Method Wrapup |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="am_misc_faq.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="am_misc_tune"></a>Access method tuning</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>There are a few different issues to consider when tuning the performance |
| 42 | of Berkeley DB access method applications.</p> |
| 43 | <div class="variablelist"> |
| 44 | <dl> |
| 45 | <dt> |
| 46 | <span class="term">access method</span> |
| 47 | </dt> |
| 48 | <dd>An application's choice of a database access method can significantly |
| 49 | affect performance. Applications using fixed-length records and integer |
| 50 | keys are likely to get better performance from the Queue access method. |
| 51 | Applications using variable-length records are likely to get better |
| 52 | performance from the Btree access method, as it tends to be faster for |
| 53 | most applications than either the Hash or Recno access methods. Because |
| 54 | the access method APIs are largely identical between the Berkeley DB access |
| 55 | methods, it is easy for applications to benchmark the different access |
| 56 | methods against each other. See <a class="xref" href="am_conf_select.html" title="Selecting an access method">Selecting an access method</a> for more information.</dd> |
| 57 | <dt> |
| 58 | <span class="term">cache size</span> |
| 59 | </dt> |
| 60 | <dd>The Berkeley DB database cache defaults to a fairly small size, and most |
| 61 | applications concerned with performance will want to set it explicitly. |
| 62 | Using a too-small cache will result in horrible performance. The first |
| 63 | step in tuning the cache size is to use the db_stat utility (or the |
| 64 | statistics returned by the <a href="../api_reference/C/dbstat.html" class="olink">DB->stat()</a> function) to measure the |
| 65 | effectiveness of the cache. The goal is to maximize the cache's hit |
| 66 | rate. Typically, increasing the size of the cache until the hit rate |
| 67 | reaches 100% or levels off will yield the best performance. However, |
| 68 | if your working set is sufficiently large, you will be limited by the |
| 69 | system's available physical memory. Depending on the virtual memory |
| 70 | and file system buffering policies of your system, and the requirements |
| 71 | of other applications, the maximum cache size will be some amount |
| 72 | smaller than the size of physical memory. If you find that |
| 73 | the <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility shows that increasing the cache size improves your hit |
| 74 | rate, but performance is not improving (or is getting worse), then it's |
| 75 | likely you've hit other system limitations. At this point, you should |
| 76 | review the system's swapping/paging activity and limit the size of the |
| 77 | cache to the maximum size possible without triggering paging activity. |
| 78 | Finally, always remember to make your measurements under conditions as |
| 79 | close as possible to the conditions your deployed application will run |
| 80 | under, and to test your final choices under worst-case conditions.</dd> |
| 81 | <dt> |
| 82 | <span class="term">shared memory</span> |
| 83 | </dt> |
| 84 | <dd>By default, Berkeley DB creates its database environment shared regions in |
| 85 | filesystem backed memory. Some systems do not distinguish between |
| 86 | regular filesystem pages and memory-mapped pages backed by the |
| 87 | filesystem, when selecting dirty pages to be flushed back to disk. For |
| 88 | this reason, dirtying pages in the Berkeley DB cache may cause intense |
| 89 | filesystem activity, typically when the filesystem sync thread or |
| 90 | process is run. In some cases, this can dramatically affect application |
| 91 | throughput. The workaround to this problem is to create the shared |
| 92 | regions in system shared memory (<a href="../api_reference/C/envopen.html#envopen_DB_SYSTEM_MEM" class="olink">DB_SYSTEM_MEM</a>) or application |
| 93 | private memory (<a href="../api_reference/C/envopen.html#envopen_DB_PRIVATE" class="olink">DB_PRIVATE</a>), or, in cases where this behavior |
| 94 | is configurable, to turn off the operating system's flushing of |
| 95 | memory-mapped pages.</dd> |
| 96 | <dt> |
| 97 | <span class="term">large key/data items</span> |
| 98 | </dt> |
| 99 | <dd>Storing large key/data items in a database can alter the performance |
| 100 | characteristics of Btree, Hash and Recno databases. The first parameter |
| 101 | to consider is the database page size. When a key/data item is too |
| 102 | large to be placed on a database page, it is stored on "overflow" pages |
| 103 | that are maintained outside of the normal database structure (typically, |
| 104 | items that are larger than one-quarter of the page size are deemed to |
| 105 | be too large). Accessing these overflow pages requires at least one |
| 106 | additional page reference over a normal access, so it is usually better |
| 107 | to increase the page size than to create a database with a large number |
| 108 | of overflow pages. Use the <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility (or the statistics |
| 109 | returned by the <a href="../api_reference/C/dbstat.html" class="olink">DB->stat()</a> method) to review the number of overflow |
| 110 | pages in the database. |
| 111 | <p>The second issue is using large key/data items instead of duplicate data |
| 112 | items. While this can offer performance gains to some applications |
| 113 | (because it is possible to retrieve several data items in a single get |
| 114 | call), once the key/data items are large enough to be pushed off-page, |
| 115 | they will slow the application down. Using duplicate data items is |
| 116 | usually the better choice in the long run.</p></dd> |
| 117 | </dl> |
| 118 | </div> |
| 119 | <p>A common question when tuning Berkeley DB applications is scalability. For |
| 120 | example, people will ask why, when adding additional threads or |
| 121 | processes to an application, the overall database throughput decreases, |
| 122 | even when all of the operations are read-only queries.</p> |
| 123 | <p>First, while read-only operations are logically concurrent, they still |
| 124 | have to acquire mutexes on internal Berkeley DB data structures. For example, |
| 125 | when searching a linked list and looking for a database page, the linked |
| 126 | list has to be locked against other threads of control attempting to add |
| 127 | or remove pages from the linked list. The more threads of control you |
| 128 | add, the more contention there will be for those shared data structure |
| 129 | resources.</p> |
| 130 | <p>Second, once contention starts happening, applications will also start |
| 131 | to see threads of control convoy behind locks (especially on |
| 132 | architectures supporting only test-and-set spin mutexes, rather than |
| 133 | blocking mutexes). On test-and-set architectures, threads of control |
| 134 | waiting for locks must attempt to acquire the mutex, sleep, check the |
| 135 | mutex again, and so on. Each failed check of the mutex and subsequent |
| 136 | sleep wastes CPU and decreases the overall throughput of the system.</p> |
| 137 | <p>Third, every time a thread acquires a shared mutex, it has to shoot down |
| 138 | other references to that memory in every other CPU on the system. Many |
| 139 | modern snoopy cache architectures have slow shoot down characteristics.</p> |
| 140 | <p>Fourth, schedulers don't care what application-specific mutexes a thread |
| 141 | of control might hold when de-scheduling a thread. If a thread of |
| 142 | control is descheduled while holding a shared data structure mutex, |
| 143 | other threads of control will be blocked until the scheduler decides to |
| 144 | run the blocking thread of control again. The more threads of control |
| 145 | that are running, the smaller their quanta of CPU time, and the more |
| 146 | likely they will be descheduled while holding a Berkeley DB mutex.</p> |
| 147 | <p>The results of adding new threads of control to an application, on the |
| 148 | application's throughput, is application and hardware specific and |
| 149 | almost entirely dependent on the application's data access pattern and |
| 150 | hardware. In general, using operating systems that support blocking |
| 151 | mutexes will often make a tremendous difference, and limiting threads |
| 152 | of control to to some small multiple of the number of CPUs is usually |
| 153 | the right choice to make.</p> |
| 154 | </div> |
| 155 | <div class="navfooter"> |
| 156 | <hr /> |
| 157 | <table width="100%" summary="Navigation footer"> |
| 158 | <tr> |
| 159 | <td width="40%" align="left"><a accesskey="p" href="am_misc_db_sql.html">Prev</a> </td> |
| 160 | <td width="20%" align="center"> |
| 161 | <a accesskey="u" href="am_misc.html">Up</a> |
| 162 | </td> |
| 163 | <td width="40%" align="right"> <a accesskey="n" href="am_misc_faq.html">Next</a></td> |
| 164 | </tr> |
| 165 | <tr> |
| 166 | <td width="40%" align="left" valign="top">Specifying a Berkeley DB schema using SQL DDL </td> |
| 167 | <td width="20%" align="center"> |
| 168 | <a accesskey="h" href="index.html">Home</a> |
| 169 | </td> |
| 170 | <td width="40%" align="right" valign="top"> Access method FAQ</td> |
| 171 | </tr> |
| 172 | </table> |
| 173 | </div> |
| 174 | </body> |
| 175 | </html> |