home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / databases / sybase-faq / part9 < prev   
Encoding:
Internet Message Format  |  2004-04-21  |  22.6 KB

  1. Path: senator-bedfellow.mit.edu!dreaderd!not-for-mail
  2. Message-ID: <databases/sybase-faq/part9_1082468590@rtfm.mit.edu>
  3. Supersedes: <databases/sybase-faq/part9_1074677126@rtfm.mit.edu>
  4. Expires: 2 Aug 2004 13:43:10 GMT
  5. References: <databases/sybase-faq/part1_1082468590@rtfm.mit.edu>
  6. X-Last-Updated: 2003/03/02
  7. From: dowen@midsomer.org (David Owen)
  8. Newsgroups: comp.databases.sybase,comp.answers,news.answers
  9. Subject: Sybase FAQ: 9/19 - ASE Admin (6 of 7)
  10. Reply-To: dowen@midsomer.org (David Owen)
  11. Followup-To: comp.databases.sybase
  12. Distribution: world
  13. Organization: Midsomer Consultants Inc.
  14. Approved: news-answers-request@MIT.EDU
  15. Keywords: FAQ, DATABASES, SYBASE, ASA, ASE, REP
  16. Originator: faqserv@penguin-lust.MIT.EDU
  17. Date: 20 Apr 2004 13:45:07 GMT
  18. Lines: 531
  19. NNTP-Posting-Host: penguin-lust.mit.edu
  20. X-Trace: 1082468707 senator-bedfellow.mit.edu 567 18.181.0.29
  21. Xref: senator-bedfellow.mit.edu comp.databases.sybase:106207 comp.answers:56953 news.answers:270293
  22.  
  23. Archive-name: databases/sybase-faq/part9
  24. URL: http://www.isug.com/Sybase_FAQ
  25. Version: 1.7
  26. Maintainer: David Owen
  27. Last-modified: 2003/03/02
  28. Posting-Frequency: posted every 3rd month
  29.    A how-to-find-the-FAQ article is posted on the intervening months.
  30.  
  31. 1.5.7: How much memory to configure?
  32.  
  33. -------------------------------------------------------------------------------
  34.  
  35. System 10 and below.
  36.  
  37. Overview
  38.  
  39. At some point you'll wonder if your ASE has been configured with sufficient
  40. memory. We hope that it's not during some crisis but that's probably when it'll
  41. happen.
  42.  
  43. The most important thing in setting up memory for a ASE is that it has to be
  44. large enough to accommodate:
  45.  
  46.   * concurrent user connections
  47.   * active procedures
  48.   * and concurrent open databases.
  49.  
  50. By not setting the ASE up correctly it will affect the performance of it. A
  51. delicate balance needs to be struck where your ASE is large enough to
  52. accommodate the users but not too large where it adversely affects the CPU
  53. Server (such as causing swapping).
  54.  
  55. Assumptions made of the reader:
  56.  
  57.   * The reader has some experience administering ASEs.
  58.   * All queries have been tuned and that there are no unnecessary table scans.
  59.  
  60. Preface
  61.  
  62. As the ASE starts up, it pre-allocates its structures to support the
  63. configuration. The memory that remains after the pre-allocation phase is the 
  64. available cache.
  65.  
  66. The available cache is partitioned into two pieces:
  67.  
  68.  1. buffer cache - data pages to be sent to a user connection or flushed to
  69.     disk.
  70.  2. procedure cache - where query plans live.
  71.  
  72. The idea is to determine if the buffer cache and the procedure cache are of
  73. adequate size. As a DBA you can use dbcc memusage to ascertain this.
  74.  
  75. The information provided from a dbcc memusage, daunting at first, but taken in
  76. sections, is easy to understand and provides the DBA with the vital information
  77. that is necessary to determine if more memory is required and where it is
  78. required.
  79.  
  80. If the procedure cache is too small, user connections will get sporadic 701's:
  81.  
  82.     There is insufficient system memory to run this query.
  83.    
  84. If the buffer cache is too small, response time may be poor or spiky.
  85.  
  86. The following text describes how to interpret the output of dbcc memusage and
  87. to correlate this back to the fundamental question:
  88.  
  89.     Does my ASE have enough memory?
  90.    
  91. Definitions
  92.  
  93. Before delving into the world of dbcc memusage some definitions to get us
  94. through.
  95.  
  96. Buffer Cache (also referred to as the Data Cache)
  97.     Area of memory where ASE stores the most recently used data pages and index
  98.     pages in 2K page units. If ASE finds a data page or index page in the
  99.     buffer cache, it doesn't need to perform a physical I/O (it is reported as
  100.     a logical I/O). If a user connection selects data from a database, the ASE
  101.     loads the 2K data page(s) here and then hands the information off to the
  102.     user connection. If a user connection updates data, these pages are
  103.     altered, and then they are flushed out to disk by the ASE.
  104.    
  105.        
  106.         This is a bit simplistic but it'll do. Read on for more info though.
  107.        
  108.         The cache is maintained as a doubly linked list. The head of the list
  109.         is where the most recently used pages are placed. Naturally towards the
  110.         tail of the chain are the least recently used pages. If a page is
  111.         requested and it is found on the chain, it is moved back to the front
  112.         of the chain and the information is relayed, thus saving a physical I/
  113.         O.
  114.        
  115.         But wait! this recycling is not done forever. When a checkpoint occurs
  116.         any dirty pages are flushed. Also, the parameter cbufwashsize
  117.         determines how many times a page containing data can be recycled before
  118.         it has to be flushed out to disk. For OAM and index pages the following
  119.         parameters apply coamtrips and cindextrips respectively.
  120.        
  121. Procedure Cache
  122.     Area of memory where ASE stores the most recently used query plans of
  123.     stored procedures and triggers. This procedure cache is also used by the
  124.     Server when a procedure is being created and when a query is being
  125.     compiled. Just like the buffer cache, if SQL Server finds a procedure or a
  126.     compilation already in this cache, it doesn't need to read it from the
  127.     disk.
  128.    
  129.     The size of procedure cache is determined by the percentage of remaining
  130.     memory configured for this Server parameter after ASE memory needs are met.
  131.    
  132. Available Cache
  133.  
  134. When the ASE starts up it pre-allocates its data structures to support the
  135. current configuration. For example, based on the number of user connections, 
  136. additional netmem, open databases and so forth the dataserver pre-allocates how
  137. much memory it requires to support these configured items.
  138.  
  139. What remains after the pre-allocation is the available cache. The available
  140. cache is divided into buffer cache and procedure cache. The sp_configure
  141. "procedure cache" parameter determines the percentage breakdown. A value of 20
  142. would read as follows:
  143.  
  144.     20% of the available cache is dedicated to the procedure cache and 80% is
  145.     dedicated to the buffer cache.
  146.    
  147. Your pal: dbcc memusage
  148.  
  149. dbcc memusage takes a snapshot of your ASE's current memory usage and reports
  150. this vital information back to you. The information returned provides
  151. information regarding the use of your procedure cache and how much of the 
  152. buffer cache you are currently using.
  153.  
  154. An important piece of information is the size of the largest query plan. We'll
  155. talk about that more below.
  156.  
  157. It is best to run dbcc memusage after your ASE has reached a working set. For
  158. example, at the end of the day or during lunch time.
  159.  
  160.     Running dbcc memusage will freeze the dataserver while it does its work.
  161.     The more memory you have configured for the ASE the longer it'll take. Our
  162.     experience is that for a ASE with 300MB it'll take about four minutes to
  163.     execute. During this time, nothing else will execute: no user queries, no
  164.     sp_who's...
  165.    
  166. In order to run dbcc memusage you must have sa privileges. Here's a sample
  167. execution for discussion purposes:
  168.  
  169. 1> /* send the output to the screen instead of errorlog */
  170. 2> dbcc traceon(3604)
  171. 3> go
  172. 1> dbcc memusage
  173. 2> go
  174. Memory Usage:
  175.  
  176.                             Meg.         2K Blks           Bytes
  177.  
  178.       Configured Memory:300.0000          153600        314572800
  179.  
  180.               Code size:  2.6375            1351         2765600
  181.       Kernel Structures: 77.6262           39745        81396975
  182.       Server Structures: 54.4032           27855        57045920
  183.              Page Cache:129.5992           66355        135894640
  184.            Proc Buffers:  1.1571             593         1213340
  185.            Proc Headers: 25.0840           12843        26302464
  186.  
  187. Number of page buffers:    63856
  188. Number of proc buffers:    15964
  189.  
  190. Buffer Cache, Top 20:
  191.  
  192.            DB Id         Object Id      Index Id        2K Buffers
  193.  
  194.                6         927446498             0            9424
  195.                6         507969006             0            7799
  196.                6         959446612             0            7563
  197.                6         116351649             0            7428
  198.                6        2135014687             5            2972
  199.                6         607445358             0            2780
  200.                6         507969006             2            2334
  201.                6        2135014687             0            2047
  202.                6         506589013             0            1766
  203.                6        1022066847             0            1160
  204.                6         116351649           255             987
  205.                6         927446498             8             897
  206.                6         927446498            10             733
  207.                6         959446612             7             722
  208.                6         506589013             1             687
  209.                6         971918604             0             686
  210.                6         116351649             6             387
  211.  
  212. Procedure Cache, Top 20:
  213.  
  214. Database Id: 6
  215. Object Id: 1652357121
  216. Object Name: lp_cm_case_list
  217. Version: 1
  218. Uid: 1
  219. Type: stored procedure
  220. Number of trees: 0
  221. Size of trees: 0.000000 Mb, 0.000000 bytes, 0 pages
  222. Number of plans: 16
  223. Size of plans: 0.323364 Mb, 339072.000000 bytes, 176 pages
  224. ----
  225. Database Id: 6
  226. Object Id: 1668357178
  227. Object Name: lp_cm_subcase_list
  228. Version: 1
  229. Uid: 1
  230. Type: stored procedure
  231. Number of trees: 0
  232. Size of trees: 0.000000 Mb, 0.000000 bytes, 0 pages
  233. Number of plans: 10
  234. Size of plans: 0.202827 Mb, 212680.000000 bytes, 110 pages
  235. ----
  236. Database Id: 6
  237. Object Id: 132351706
  238. Object Name: csp_get_case
  239. Version: 1
  240. Uid: 1
  241. Type: stored procedure
  242. Number of trees: 0
  243. Size of trees: 0.000000 Mb, 0.000000 bytes, 0 pages
  244. Number of plans: 9
  245. Size of plans: 0.149792 Mb, 157068.000000 bytes, 81 pages
  246. ----
  247. Database Id: 6
  248. Object Id: 1858261845
  249. Object Name: lp_get_last_caller_new
  250. Version: 1
  251. Uid: 1
  252. Type: stored procedure
  253. Number of trees: 0
  254. Size of trees: 0.000000 Mb, 0.000000 bytes, 0 pages
  255. Number of plans: 2
  256. Size of plans: 0.054710 Mb, 57368.000000 bytes, 30 pages
  257. ...
  258.  
  259. 1> /* redirect output back to the errorlog */
  260. 2> dbcc traceoff(3604)
  261. 3> go
  262.  
  263. Dissecting memusage output
  264.  
  265. The output may appear overwhelming but it's actually pretty easy to parse.
  266. Let's look at each section.
  267.  
  268. Memory Usage
  269.  
  270. This section provides a breakdown of the memory configured for the ASE.
  271.  
  272. Memory Usage:
  273.  
  274.                             Meg.         2K Blks           Bytes
  275.  
  276.       Configured Memory:300.0000          153600        314572800
  277.  
  278.               Code size:  2.6375            1351         2765600
  279.       Kernel Structures: 77.6262           39745        81396975
  280.       Server Structures: 54.4032           27855        57045920
  281.              Page Cache:129.5992           66355        135894640
  282.            Proc Buffers:  1.1571             593         1213340
  283.            Proc Headers: 25.0840           12843        26302464
  284.  
  285. Number of page buffers:    63856
  286. Number of proc buffers:    15964
  287.  
  288.    
  289.     The Configured Memory does not equal the sum of the individual components.
  290.     It does in the sybooks example but in practice it doesn't always. This is
  291.     not critical and it is simply being noted here.
  292.    
  293. The Kernel Structures and Server structures are of mild interest. They can be
  294. used to cross-check that the pre-allocation is what you believe it to be. The
  295. salient line items are Number of page buffers and Number of proc buffers.
  296.  
  297. The Number of proc buffers translates directly to the number of 2K pages
  298. available for the procedure cache.
  299.  
  300. The Number of page buffers is the number of 2K pages available for the buffer
  301. cache.
  302.  
  303. As a side note and not trying to muddle things, these last two pieces of
  304. information can also be obtained from the errorlog:
  305.  
  306.     ... Number of buffers in buffer cache: 63856.
  307.     ... Number of proc buffers allocated: 15964.
  308.  
  309. In our example, we have 15,964 2K pages (~32MB) for the procedure cache and
  310. 63,856 2K pages (~126MB) for the buffer cache.
  311.  
  312. Buffer Cache
  313.  
  314. The buffer cache contains the data pages that the ASE will be either flushing
  315. to disk or transmitting to a user connection.
  316.  
  317. If this area is too small, the ASE must flush 2K pages sooner than might be
  318. necessary to satisfy a user connection's request.
  319.  
  320. For example, in most database applications there are small edit tables that are
  321. used frequently by the application. These tables will populate the buffer cache
  322. and normally will remain resident during the entire life of the ASE. This is
  323. good because a user connection may request validation and the ASE will find the
  324. data page(s) resident in memory. If however there is insufficient memory
  325. configured, then these small tables will be flushed out of the buffer cache in
  326. order to satisfy another query. The next time a validation is requested, the
  327. tables will have to be re-read from disk in order to satisfy the request. Your
  328. performance will degrade.
  329.  
  330. Memory access is easily an order of magnitude faster than performing a physical
  331. I/O.
  332.  
  333. In this example we know from the previous section that we have 63,856 2K pages
  334. (or buffers) available in the buffer cache. The question to answer is, "do we
  335. have sufficient buffer cache configured?"
  336.  
  337. The following is the output of the dbcc memusage regarding the buffer cache:
  338.  
  339. Buffer Cache, Top 20:
  340.  
  341.            DB Id         Object Id      Index Id        2K Buffers
  342.  
  343.                6         927446498             0            9424
  344.                6         507969006             0            7799
  345.                6         959446612             0            7563
  346.                6         116351649             0            7428
  347.                6        2135014687             5            2972
  348.                6         607445358             0            2780
  349.                6         507969006             2            2334
  350.                6        2135014687             0            2047
  351.                6         506589013             0            1766
  352.                6        1022066847             0            1160
  353.                6         116351649           255             987
  354.                6         927446498             8             897
  355.                6         927446498            10             733
  356.                6         959446612             7             722
  357.                6         506589013             1             687
  358.                6         971918604             0             686
  359.                6         116351649             6             387
  360.          Index Legend          
  361.                         +-----------------------------+                        
  362.                         |       |                     |                        
  363.                         |-------+---------------------|                        
  364.                         | Value |     Definition      |                        
  365.                         |-------+---------------------|                        
  366.                         | 0     | Table data          |                        
  367.                         |-------+---------------------|                        
  368.                         | 1     | Clustered index     |                        
  369.                         |-------+---------------------|                        
  370.                         | 2-250 | Nonclustered        |                        
  371.                         |       | indexes             |                        
  372.                         |-------+---------------------|                        
  373.                         | 255   | Text pages          |                        
  374.                         +-----------------------------+                        
  375.  
  376.   * To translate the DB Id use select db_name(#) to map back to the database
  377.     name.
  378.   * To translate the Object Id, use the respective database and use the select
  379.     object_name(#) command.
  380.  
  381. It's obvious that the first 10 items take up the largest portion of the buffer
  382. cache. Sum these values and compare the result to the amount of buffer cache
  383. configured.
  384.  
  385. Summing the 10 items nets a result of 45,263 2K data pages. Comparing that to
  386. the number of pages configured, 63,856, we see that this ASE has sufficient
  387. memory configured.
  388.  
  389. When do I need more Buffer Cache?
  390.  
  391. I follow the following rules of thumb to determine when I need more buffer
  392. cache:
  393.  
  394.   * If the sum of all the entries reported is equal to the number of pages
  395.     configured and all entries are relatively the same size. Crank it up.
  396.   * Note the natural groupings that occur in the example. If the difference
  397.     between any of the groups is greater than an order of magnitude I'd be
  398.     suspicious. But only if the sum of the larger groups is very close to the
  399.     number of pages configured.
  400.  
  401. Procedure Cache
  402.  
  403. If the procedure cache is not of sufficient size you may get sporadic 701
  404. errors:
  405.  
  406.     There is insufficient system memory to run this query.
  407.    
  408. In order to calculate the correct procedure cache one needs to apply the
  409. following formula (found in ASE Troubleshooting Guide - Chapter 2, Procedure
  410. Cache Sizing):
  411.  
  412.     proc cache size = max(# of concurrent users) * (size of the largest plan) *
  413.     1.25
  414.    
  415.         The flaw with the above formula is that if 10% of the users are
  416.         executing the largest plan, then you'll overshoot. If you have distinct
  417.         classes of connections whose largest plans are mutually exclusive then
  418.         you need to account for that:
  419.        
  420.             ttl proc cache = proc cache size * x% + proc cache size * y% ...
  421.            
  422. The max(# of concurrent users) is not the number of user connections configured
  423. but rather the actual number of connections during the peak period.
  424.  
  425. To compute the size of the largest [query] plan take the results from the dbcc
  426. memusage's, Procedure Cache section and apply the following formula:
  427.  
  428.     query plan size = [size of plans in bytes] / [number of plans]
  429.    
  430. We can compute the size of the query plan for lp_cm_case_list by using the
  431. output of the dbcc memusage:
  432.  
  433. ...
  434. Database Id: 6
  435. Object Id: 1652357121
  436. Object Name: lp_cm_case_list
  437. Version: 1
  438. Uid: 1
  439. Type: stored procedure
  440. Number of trees: 0
  441. Size of trees: 0.000000 Mb, 0.000000 bytes, 0 pages
  442. Number of plans: 16
  443. Size of plans: 0.323364 Mb, 339072.000000 bytes, 176 pages
  444. ----
  445. ...
  446.  
  447. Entering the respective numbers, the query plan size for lp_cm_case_list is
  448. 21K:
  449.  
  450.     query plan size = 339072 / 16
  451.     query plan size = 21192 bytes or 21K
  452.    
  453. The formula would be applied to all objects found in the procedure cache and
  454. the largest value would be plugged into the procedure cache size formula:
  455.  
  456.          Query Plan Sizes         
  457.                       +--------------------------------+                       
  458.                       |                        |       |                       
  459.                       |------------------------+-------|                       
  460.                       |                        | Query |                       
  461.                       |         Object         | Plan  |                       
  462.                       |                        | Size  |                       
  463.                       |------------------------+-------|                       
  464.                       | lp_cm_case_list        | 21K   |                       
  465.                       |------------------------+-------|                       
  466.                       | lp_cm_subcase_list     | 21K   |                       
  467.                       |------------------------+-------|                       
  468.                       | csp_get_case           | 19K   |                       
  469.                       |------------------------+-------|                       
  470.                       | lp_get_last_caller_new | 28K   |                       
  471.                       +--------------------------------+                       
  472.  
  473. The size of the largest [query] plan is 28K.
  474.  
  475. Entering these values into the formula:
  476.  
  477.     proc cache size = max(# of concurrent users) * (size of the largest plan) *
  478.     1.25
  479.     proc cache size = 491 connections * 28K * 1.25
  480.     proc cache size = 17,185 2K pages required
  481.    
  482. Our example ASE has 15,964 2K pages configured but 17,185 2K pages are
  483. required. This ASE can benefit by having more procedure cache configured.
  484.  
  485. This can be done one of two ways:
  486.  
  487.  1. If you have some headroom in your buffer cache, then sp_configure
  488.     "procedure cache" to increase the ratio of procedure cache to buffer cache
  489.     or
  490.    
  491.         procedure cache =
  492.             [ proposed procedure cache ] /
  493.             ( [ current procedure cache ] + [ current buffer cache ] )
  494.        
  495.         The new procedure cache would be 22%:
  496.        
  497.             procedure cache = 17,185 / ( 15,964 + 63,856 )
  498.             procedure cache = .2152 or 22%
  499.            
  500.  2. If the buffer cache cannot be shrunken, then sp_configure "memory" to
  501.     increase the total memory:
  502.    
  503.         mem size =
  504.             ([ proposed procedure cache ]) /
  505.             ([ current procedure cache ] / [ current configured memory ])
  506.        
  507.         The new memory size would be 165,399 2K pages, assuming that the
  508.         procedure cache is unchanged:
  509.        
  510.             mem size = 17,185 / ( 15,964 / 153,600 )
  511.             mem size = 165,399 2K pages
  512.            
  513. Back to top
  514.  
  515. -------------------------------------------------------------------------------
  516.  
  517. 1.5.8: Why should I use stored procedures?
  518.  
  519. -------------------------------------------------------------------------------
  520.  
  521. There are many advantages to using stored procedures (unfortunately they do not
  522. handle the text/image types):
  523.  
  524.   * Security - you can revoke access to the base tables and only allow users to
  525.     access and manipulate the data via the stored procedures.
  526.   * Performance - stored procedures are parsed and a query plan is compiled.
  527.     This information is stored in the system tables and it only has to be done
  528.     once.
  529.   * Network - if you have users who are on a WAN (slow connection) having
  530.     stored procedures will improve throughput because less bytes need to flow
  531.     down the wire from the client to ASE.
  532.   * Tuning - if you have all your SQL code housed in the database, then it's
  533.     easy to tune the stored procedure without affecting the clients (unless of
  534.     course the parameter change).
  535.   * Modularity - during application development, the application designer can
  536.     concentrate on the front-end and the DB designer can concentrate on the
  537.     ASE.
  538.   * Network latency - a client on a LAN may seem slower if it is sending large
  539.     numbers of separate requests to a database server, bundling them into one
  540.     procedure call may improve responsiveness. Also, servers handling large
  541.     numbers of small requests can spend a surprising amount of CPU time
  542.     performing network IO.
  543.   * Minimise blocks and deadlocks - it is a lot easier to handle a deadlock if
  544.     the entire transaction is performed in one database request, also locks
  545.     will be held for a shorter time, improving concurrency and potentially
  546.     reducing the number of deadlocks. Further, it is easier to ensure that all
  547.     tables are accessed in a consistent order if code is stored centrally
  548.     rather than dispersed among a number of apps.
  549.  
  550. Back to top
  551.  
  552. -------------------------------------------------------------------------------
  553.  
  554.