Upload
ram-n-sangwan
View
53
Download
2
Embed Size (px)
Citation preview
MySQL DBA Training Session 20. Optimizing MySQL ServerRAM N SANGWAN
WWW.RNSANGWAN.COM
YOUTUBE CHANNEL HTTP://YOUTUBE.COM/USER/THESKILLPEDIA
WANT TO LEARN OR TEACH JOIN WWW.THESKILLPEDIA.COM
Agenda• Introduction to Status Variables
• Interpreting mysqld Server Information
• Accessing Server System Variables
• Setting System Variables
• Static Vs Dynamic Variables
• Global Variables
• Session Variables
• Display Variables
• Accessing Server Status Variables
• Understanding the Values of StatusVariables
• Measuring Server Load
• Process List
• Tuning Memory Parameters
• Overall mysqld options
• Configure table caching
Who am I?
• Corporate Trainer
• More than 25 Years of Experience.
• More than 50 Technologies
• Managing Alliance Softech Pvt Ltd as Managing Director
• Running More than 300 Live Websites
• Major Technologies
◦ RDBMS : MySQL, Sybase (Now SAP ASE), DB2, Oracle, SQL Server, SAP HANA
◦ Linux : Virtualization, High Availability, Deployment
◦ PHP, Perl, Python
◦ Storage Technologies
◦ Many More…
WWW.RNSANGWAN.COM 3
Introduction
• You have to have a understanding of the status command and the globalvariables that are associated.
• The status command produces over 250 variables.
Interpreting mysqld Server Information
SHOW VARIABLES
◦ displays server system variables.
◦ These indicate such things as directory locations, server capabilities, andsizes of caches and buffers.
◦ You can set system variables to control how the server operates.
◦ They can be set at server startup, and many of them can be changed whilethe server is running.
Interpreting mysqld Server Information
SHOW STATUS
◦ displays server status variables that indicate the extent and types of activitiesthe server is performing.
◦ These variables provide information such as how long the server has beenrunning, number of queries processed, amount of network traffic, andstatistics about the query cache.
◦ You can use status information to assess how much of a load your server isprocessing and how well it is handling the load.
◦ This information provides useful feedback for assessing whether systemvariables should be changed to improve server performance.
Accessing Server System Variables
• To display these variables, use the SHOW VARIABLES statement:
mysql> SHOW VARIABLES;
• To display only those variables with names that match a given pattern, add aLIKE pattern-matching clause.
• The pattern is not case sensitive and may contain the '%' and '_' wildcardpattern metacharacters:
mysql> SHOW VARIABLES LIKE '%buffer_size';
• If the pattern contains no metacharacters, the statement displays only thenamed variable:
mysql> SHOW VARIABLES LIKE 'datadir';
Accessing Server System Variables
• System variables may be displayed in other ways as well.
• mysqladmin variables provides command-line access to the complete list ofsystem variables.
• Both clients implement this capability by sending a SHOW VARIABLESstatement to the server and displaying the results.
Setting System Variables
• System variables can be set at server startup using options on the commandline or in option files.
• For example, on a Unix machine, you can put the following lines in the/etc/my.cnf option file to specify a data directory of /var/mysql/data and a keycache size of 64MB:
[mysqld]
datadir = /var/mysql/data
key_buffer_size = 64M
Static Vs Dynamic Variables
• Some server system variables are static and can only be set at startup time.
• For example, you can specify the data directory by means of a datadir startup option, but youcannot tell a server that is running to use a different data directory.
• Other variables are dynamic and can be changed while the server is running:
mysql> SET GLOBAL key_buffer_size = 128*1024*1024;
mysql> SET @@global.key_buffer_size = 128*1024*1024;
• With a SET statement, you cannot use a suffix of K, M, or G to indicate units for the value, butyou can use an expression
• To set global variables, you must have the SUPER privilege.
Global Variables
• An example of the type of variable that has both forms is storage_engine,which controls the default storage engine used for CREATE TABLEstatements that do not specify a storage engine explicitly.
• The global storage_engine value is used to set the session storage_enginevariable for each client when the client connects, but the client may change itssession variable value to use a different default storage engine.
Session Variables
• For example, the default storage engine may be set either globally or only for the currentconnection using the following statements:
mysql> SET GLOBAL storage_engine = MyISAM;
mysql> SET @@global.storage_engine = MyISAM;
mysql> SET SESSION storage_engine = InnoDB;
mysql> SET @@session.storage_engine = InnoDB;
• LOCAL is a synonym for SESSION.
• Also, if you do not indicate explicitly whether to set the global or session version of a variable,MySQL sets the session variable.
Display Variables
• To explicitly display global or session variable values, use SHOW GLOBAL VARIABLES or SHOW SESSION VARIABLES.
• Without GLOBAL or SESSION, the SHOW VARIABLES statement displays session values.
• It's also possible to use SELECT to display the values of individual global or session values:
mysql> SELECT @@global.storage_engine, @@session.storage_engine;
Accessing Server Status Variables
• The server makes the current values of these variables available through theSHOW STATUS:
mysql> SHOW STATUS;
• To display only those variables with names that match a given pattern, add aLIKE pattern-matching clause. :
mysql> SHOW STATUS LIKE 'qcache%';
• Status variables may be obtained in other ways as well. mysqladminextended-status provides command-line access to the complete list of statusvariables, and mysqladmin status displays a brief summary.
Understanding the Values of Status Variables
• Several status variables provide information about how many connections theserver is handling, including the number of successful and unsuccessfulconnection attempts, and also whether successful connections terminatenormally or abnormally.◦ The total number of connection attempts: Connections
◦ The number of unsuccessful connection attempts: Aborted_connects
◦ The number of successful connection attempts: Connections - Aborted_connects
◦ The number of successful connections that terminated abnormally: Aborted_clients
◦ The successful connections terminated normally: Connections - Aborted_connects -Aborted_clients
◦ The number of clients currently connected to the server: Threads_connected
Measuring Server Load
• Several status variables displayed by SHOW STATUS provide loadinformation. For example,
• Questions indicates the number of queries the server has processed and
• Uptime indicates the number of seconds the server has been running.
• Combining these, the ratio Questions/ Uptime tells you how many queries persecond the server has processed.
Measuring Server Load
• slow_queries indicates the number of queries that take a long time toprocess.
• Ideally, its value should increase slowly or not at all.
• If it increases quickly, you might have a problem with certain queries.
• The slow query log shows the text of slow queries and provides informationabout how long they took.
• Restart the server with the slow query log enabled, let it run for a while, andthen take a look at the log to see which queries turn up there.
• You can use this log to identify queries that might need optimizing.
Process List
• SHOW PROCESSLIST displays information about the activity of eachcurrently connected client.
• The SHOW PROCESSLIST statement always shows your own queries.
• If you have the PROCESS privilege, it also shows queries being run by otheraccounts.
• To get a concise report of the server's load status from within the mysql clientprogram, use its STATUS (or \s) command to display a general snapshot ofthe current connection state.
• The last part of the output provides some information about the server load:
mysql> STATUS;
• The final part of the output also can be obtained by issuing a mysqladminstatus command.
Tuning Memory Parameters
• A small system may use options with small values:
[mysqld]
key_buffer_size = 16K
table_cache = 4
sort_buffer_size = 64K
• For a larger system, you can increase the values, and also allocate memory to the query cache:
[mysqld]
key_buffer_size = 256M
table_cache = 256
sort_buffer_size = 1M
query_cache_type = ON
query_cache_size = 16M
Overall mysqld options
memlock
• Locks the mysqld daemon into server memory, it can improve performancebut if the server runs out of memory then mysqld daemon will crash.
sort_buffer_size
• Determines the amount of memory allocated for SQL sorts, if this space isexceeded then the server will use hard disk space resulting in worseperformance.
Overall mysqld options Contd..
thread_cache_size
• mysqld creates a cache of unused connection thread rather than destroyingthreads and creating new ones, increase this variable if you have a lot ofusers logging in and out of the MySQL server.
thread_concurrency
• only use on Solaris systems, this should be set to twice the number of CPU's.
tmp_table_size
• both this and max_heap_table_size are used to determine the maximum sizeallowed of an in-memory temporary table before it is converted to a MyISAMtable, the smallest value of either one is used.
Configure table caching
• MySQL has at least one file per table : the FRM file
table_open_cache
◦ not too small, not too big
◦ opened_tables / sec
table_definition_cache
◦ do not forget to increase
◦ opened_table_definitions / sec
table_cache_instances = 8 or 16
innodb_open_files
mdl_hash_instances = 256