4.6. Performance TuningThe difference between being a good DBA (Database Administrator) and being a top-notch DBA is the difference between knowing how to manage your database server and knowing how your database server lives and breathes. Solving performance problems is often a matter of understanding just how MySQL works under the covers so that you can optimize application performance to take advantage of those features. MySQL presents three main potential bottlenecks for any connection. The first possibility is the network connection between the client and the server. Second is the processing time needed for activities like building keys. Finally, disk I/O can be a problem. MySQL provides variables that enable you to match MySQL's operations to your application environment. You can set each of these variables using the -O option to mysqld.[5] For example, you set back_log to 15 by adding the option -O back_log=15 to the options for mysqld. The following is a list of useful variables.
4.6.1. The MySQL Data StructureMySQL stores each table as a set of three files. For example, a medium-sized table called mytable may look like this: -rw-rw---- 1 root root 1034155 Jun 3 17:08 mytable.ISD -rw-rw---- 1 root root 50176 Jun 3 17:08 mytable.ISM -rw-rw---- 1 root root 9114 Jun 3 14:24 mytable.frm The ISD file contains the actual data. The ISM file contains information about the keys and other internal data that enables MySQL to find data in the ISD file quickly. The frm file contains the structure of the table itself. The ISM file is most important to the performance of MySQL. It is so important, in fact, that an entire utility, isamchk , is devoted to it. Running isamchk -d will display information about a table: # isamchk -d mytable ISAM file: mytable Data records: 1973 Deleted blocks: 0 Recordlength: 343 Record format: Packed table description: Key Start Len Index Type 1 2 50 unique text packed stripped The important field to notice here is the "Deleted blocks" field. If this value is too high, then the file is wasting a lot of space. Fortunately, you can recover this space. The following command will examine the table and recreate it, removing most errors and eliminating unnecessary space: isamchk -r mytable You can obtain additional speed enhancements by running isamchk -a on the table. This command analyzes the distribution of data in a table. You should run it after you insert or delete numerous records from the table. 4.6.1.1. Repairing damaged tablesDue to server crashes or other acts of nature, a table in your database may become corrupted. When this happens, isamchk provides several different levels of repair: isamchk mytable
This command will repair most common problems with tables. Adding the -i and -v options will provide extra output about what is wrong. You can use more than one -v for extra information. isamchk -rq mytable This command will perform a quick check -- and repair, if necessary -- of only the ISM file. It will not check for corruption of the ISD file. isamchk -e mytable Using this option, you can perform a full check and repair of everything, eliminating any possible corruption. This sort of check will naturally take much longer than a regular check. The command will exit upon encountering the first severe error. If you want to continue reparations even after severe corruption is encountered, you can pass it the -v option. This option will guarantee the resulting table to be clean of corruption, but you may lose some data in the process.
4.6.1.2. Removing and replacing keysKeys can sometimes get in the way of database performance. If, for instance, you want to insert a large data set into a table, having MySQL index the keys after every insert can be very inefficient. In addition, if you have a table with corrupt keys, blindly repairing that table with isamchk could delete some of the data associated with the key. In these situations, it can be helpful to temporarily remove the keys from a table and then replace them when the troublesome work is finished. The following command will remove the key information from a table: isamchk -rq -k0 When you are ready to put them back in, issue this command to replace the keys: isamchk -rq
The isamchk command provides so many capabilities it can be hard to sift through them all. However, there are some basic guidelines to follow:
4.6.2. TroubleshootingEven in the best of products, problems occur. Fortunately, many problems you might run into have happened to others. The following is a collection of frequently encountered trouble spots dealing with MySQL administration:
Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|