MySQL Server optimization method (2) bitsCN.com
If the mysqld server is currently running, connect to the server and run the following command to view the actually used system variables:
Mysql> show variables;
You can also use the following statement to view the statistical results and status reports of the running system:
Mysql> show status;
You can also use
Mysqladmin to get:
Shell> mysqladmin variables
Shell> mysqladmin extended-status
MySQL uses highly scalable algorithms. Therefore, it can run with only a small amount of memory. However, giving MySQL more memory can usually achieve better performance.
When adjusting the MySQL server, the two most important variables are key_buffer_size and table_cache. Before trying to modify other variables, check that the two variables have been properly set.
The following example shows how to configure some typical variable values at different runtime. These examples use the mysqld_safe script and -- var_name = value syntax to set the value of the variable var_name to value. This syntax can be used after MySQL 4.0. in the old version of MySQL, the following differences are taken into account:
Use the safe_mysqld script instead of mysqld_safe.
Use the -- set-variable = var_name = value or-O var_name = value syntax to set the variable.
If the variable name ends with _ size, _ size must be removed. For example, an old variable named sort_buffer_size is sort_buffer, and the old variable named read_buffer_size is record_buffer. Use mysqld -- help to check the variables that can be identified by the current server version.
If you have at least MB of memory and a large number of data tables, you can set the maximum performance when you want to connect to a moderate number of clients:
Shell> mysqld_safe -- key_buffer_size = 64 M -- table_cache = 256.
-- Sort_buffer_size = 4 M -- read_buffer_size = 1 M &
If there is only MB of memory and only a small table, but a large number of sorting is required, you can set it as follows:
Shell> mysqld_safe -- key_buffer_size = 16 M -- sort_buffer_size = 1 M
If a large number of concurrent connections exist, the swap problem may occur unless mysqld has been set to use only a small amount of memory for each connection. Mysqld provides better performance when there is enough memory for each connection.
If there is only a small amount of memory and there is a large number of connections, you can set it as follows:
Shell> mysqld_safe -- key_buffer_size = 512 K -- sort_buffer_size = 100 K
-- Read_buffer_size = 100 K &
Even like this:
Shell> mysqld_safe -- key_buffer_size = 512 K -- sort_buffer_size = 16 K
-- Table_cache = 32 -- read_buffer_size = 8 K
-- Net_buffer_length = 1 K &
If the group by or order by operation is performed on a label much larger than the available memory, it is better to increase the value of read_rnd_buffer_size to accelerate the read data after the sorting operation.
After MySQL is installed, there will be some different 'My. cnf 'example File: 'My-huge. cnf ', 'My-large. cnf ', 'My-medium. cnf 'and 'My-small. cnf '. They can be used as the blueprint for optimizing the system.
Note: If the parameter is specified for mysqld or mysqld_safe through the command line, it is only valid when the server is started. To make these options valid when the server is started, you can put them in the configuration file.
To view the effect of parameter changes, you can use the following method (ignore -- verbose before MySQL 4.1 ):
Shell> mysqld -- key_buffer_size = 32 M -- verbose -- help
This variable will be listed near the end of the result. Make sure that the -- verbose and -- help options are placed at the end. Otherwise, the effects of other options after them in the results listed on the command line will not be reflected.
For more information about how to adjust the InnoDB storage engine, see "16.12 InnoDB Performance Tuning Tips ".
3. control query optimization performance
The task of querying the optimization program is to find the best way to execute the SQL query. Because the performance difference between the "good" and "bad" methods may be an order of magnitude (that is, the second is relative to the hour, or even the day ), most of the query optimization programs in MySQL search for possible optimization methods to find the best method for execution. In connection query, the possible methods for MySQL optimization program search increase exponentially as the number of referenced tables in the query increases. If the number of tables is small (usually less than 7-10), this is basically not a problem. However, when a large query is submitted, the main performance bottleneck of the server is easily spent on optimizing the query.
MySQL 5.0.1 introduces a more flexible method that allows users to control the number of optimal search methods in the query optimization program. Generally, the fewer methods to optimize the program search, the less time it takes to compile the query. On the other hand, the optimization program may ignore some methods, so it may miss the best optimization method.
The following two system variables can be used to control the number of optimization methods evaluated by the optimization program:
The variable optimizer_prune_level tells the optimizer to ignore a certain number of methods based on estimating the number of records in each table to be accessed. Our experience shows that this "learning guess" method rarely misses the best method, because it may dramatically reduce compilation time. This is why this option is enabled by default (optimizer_prune_level = 1 ). However, if you are sure that the optimizer_prune_level = 0, you can disable this option. However, it may take longer to compile the query. Although this test method is used, the optimization program will still investigate the exponential method.
Variable optimizer_search_depth
Tell the Optimizer "future" whether an incomplete method needs to be expanded further for each sequential investigation. The smaller the value of optimizer_search_depth, the less compilation time the query may be. For example, it may take several hours or even a few days for a query of 12-13 or more tables to compile. if the value of optimizer_search_depth is similar to the number of tables. Similarly, if the value of optimizer_search_depth is 3 or 4, it may take less than a few minutes for the compiler to complete the compilation. If you cannot determine the value of optimizer_search_depth, set it to 0 and let the optimizer decide automatically.
BitsCN.com