Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Compare Prices
Logo Design Custom
Holiday Gift Ideas
Find Software
Memory Upgrades
GPS
Auto Insurance Quote
Imprinted Promotions
Promotional Pens
Computer Deals
Promotional Gifts
Desktop Computers
Build a Server Rack
KVM Switches




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Eco Innovation: Good for Business, Good for the Environment
A complete solution to help you optimize and refresh your datacenter while properly recycling equipment and eliminating eWaste, including money-saving promotions to lower hardware acquisition costs. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »
Related Articles
Optimizing MySQL: Queries and Indexes
Optimizing MySQL: Hardware and the Mysqld Variables

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MySQL
June 15, 2004
Optimizing the mysqld variables
By Ian Gilfillan

Time for an update

In 2001, I wrote an article entitled Optimizing MySQL: Hardware and the Mysqld Variables. That was almost three years ago, when MySQL 4 was but a twinkling in Monty Widenius' eye. With MySQL 4 now stable, and MySQL 4.1 and 5.0 already out in alpha, it is time for an update to that article. This month we look at the mysqld variables, focusing on the key ones to tweak to get your system working optimally. I assume you know all about the my.cnf file, and how to set variables. If not, read the earlier article.

key_buffer_size

The key_buffer_size is probably the most useful single variable to tweak. The larger you set it, the more of your MyISAM table indexes you store in memory. With most queries making use of an index, and memory being an order of magnitude faster than disk, the importance of this variable cannot be overestimated.

On dedicated MySQL servers, the rule-of-thumb is to aim to set the key_buffer_size to at least a quarter, but no more than half, of the total amount of memory on the server. Ideally, it will be large enough to contain all the indexes (the total size of all .MYI files on the server). If you are unable to make it large enough for this, the best way to fine-tune the setting is to compare the key_reads and the key_read_requests status variables. The latter is the total number of requests making use of an index, while the former is the total number of those requests that had to read from disk. You want at least 100 requests to every request from disk, preferably a lot more. Have a look at scenario 1, with the same query run a few seconds apart.

Scenario 1

mysql> SHOW STATUS LIKE '%key_read%';
+-------------------+------------+
| Variable_name     | Value      |
+-------------------+------------+
| Key_read_requests | 3606100254 |
| Key_reads         | 2594030    |
+-------------------+------------+

mysql> SHOW STATUS LIKE '%key_read%';
+-------------------+------------+
| Variable_name     | Value      |
+-------------------+------------+
| Key_read_requests | 3606102741 |
| Key_reads         | 2594030    |
+-------------------+------------+

This is a healthy ratio, around 1400 to 1. Of the 2500 index requests between the two samples, none required the disk. On this server, the key_buffer is set to 768M, while the total memory available is 3GB.

Scenario 2

mysql> SHOW STATUS LIKE '%key_read%';
+-------------------+-----------+
| Variable_name     | Value     |
+-------------------+-----------+
| Key_read_requests | 609601541 |
| Key_reads         | 46729832  |
+-------------------+-----------+

In scenario 2, it is shocking, about 13 to 1. On this server, the key_buffer_size was set to 16MB out of 64MB. If you are in a similar situation, it is clear what your next hardware upgrade should be. RAM is always the primary hardware upgrade you can do to improve your system.

The Query Cache variables

MySQL 4 added an extremely useful tool for getting some extra from a database server - the query cache. I have recently written a dedicated article on the topic, which you can read here.

The table cache

The table_cache remains a useful variable to tweak. Each time MySQL accesses a table, it places it in the cache. If your system accesses many tables, it is faster to have these in the cache. One thing to note is that you may have more open tables than there are database tables on the server. MySQL, being multi-threaded, may be running many queries on the table at one time, and each of these will open a table. A good way to see whether your system needs to increase this is to examine the value of open_tables at peak times. If you find it stays at the same value as your table_cache value, and then the number of opened_tables starts rapidly increasing, you should increase the table_cache if you have enough memory. Look at these three scenarios, during peak times.

Scenario 1

mysql> SHOW STATUS LIKE 'open%tables%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables   | 98    |
| Opened_tables | 1513  |
+---------------+-------+

The table_cache is set to 512, and the server has been running for a long time. If the server is taking strain elsewhere, the table_cache setting could probably be reduced safely.

Scenario 2

mysql> SHOW STATUS LIKE 'open%tables%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables   | 64    |
| Opened_tables | 517   |
+---------------+-------+

The table_cache is set to 64, and the server has been running for a long time. Even though open_tables is at its maximum, the number of open_tables is very low considering that the server has been up for ages. There is probably not much benefit in upping the table_cache. This example came from a development server.

Scenario 3

mysql> SHOW STATUS LIKE 'open%tables%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables   | 64    |
| Opened_tables | 13918 |
+---------------+-------+

The table_cache is set to 64, and the server has been running for a short time. This time the table_cache is clearly set too low. The open_tables is running at maximum, and the number of opened_tables is already high. If you have the memory, up the table_cache.

sort_buffer

The sort_buffer is very useful for speeding up myisamchk operations (which is why it is set much higher for that purpose in the default configuration files), but it can also be useful everyday when performing large numbers of sorts. It defaults to 2M in the my-huge.cnf sample file, but I have successfully upped it to 9MB on a 3GB server running quite a few sorts.

read_rnd_buffer_size

The read_rnd_buffer_size is used after a sort, when reading rows in sorted order. If you use many queries with ORDER BY, upping this can improve performance. Remember that, unlike key_buffer_size and table_cache, this buffer is allocated for each thread. This variable was renamed from record_rnd_buffer in MySQL 4.0.3. It defaults to the same size as the read_buffer_size, which defaults to 128KB. A rule-of-thumb is to allocate 1KB for each 1MB of memory on the server, for example 3MB on a machine with 3GB memory.

tmp_table_size

This variable determines the maximum size for a temporary table in memory. If the table becomes too large, a MYISAM table is created on disk. Try to avoid temporary tables by optimizing the queries where possible, but where this is not possible, try to ensure temporary tables are always stored in memory. Watching the processlist for queries with temporary tables that take too long to resolve can give you an early warning that tmp_table_size needs to be upped. Be aware that memory is also allocated per-thread. An example where upping this worked for more was a server where I upped this from 32MB (the default) to 64MB with immediate effect. The quicker resolution of queries resulted in less threads being active at any one time, with all-round benefits for the server, and available memory.

innodb_buffer_pool_size

While the key_buffer_size is the variable to target for MyISAM tables, for InnoDB tables, it is innodb_buffer_pool_size. Again, you want this as high as possible to minimize slow disk usage. On a dedicated MySQL server running InnoDB tables, you can set this up to 80% of the total available memory.

innodb_additional_mem_pool_size

This variable stores the internal data structure. Make sure it is big enough to store data about all your InnoDB tables (you will see warnings in the error log if the server is using OS memory instead).

Conclusion

There are many MySQL variables, but the ones I've discussed here are usually key to target if your database is underperforming. Always look at optimizing your queries first though - the most dramatic benefits usually come from proper indexing and carefully written queries. Once you are confident in your queries, then it is time to increase the memory allocated to the variables, and purchase more RAM if necessary. The effect on many underperforming servers I have encountered has been startling!

» See All Articles by Columnist Ian Gilfillan

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MySQL Archives

Webcast: Five Virtualization Trends to Watch. Produced for HP, Citrix, and Intel.
Quest Whitepaper: Improving Oracle Database Performance Using Real-Time Visual Diagnostics
What's The Future Of IT? Find Out By Reading "IT in 2018" Now. Free Registration Required.
HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.


Latest Forum Threads
MySQL Forum
Topic By Replies Updated
Transfer information from an access database to MySQl database sculptor44 2 May 14th, 09:50 PM
MERGE STORAGE ENGINE Vs UNION ALL dbnewbie 0 May 14th, 12:16 AM
database sculptor44 0 May 5th, 09:10 PM
arrangeable Database sculptor44 0 May 5th, 08:53 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES