MySQL Performance Optimization
MySQL is a rock solid, lighting fast database server which has been designed for two factors speed and performance. It is the Ferrari of databases: Light weight, fast and Built for the high speed tracks!
I still hear an awful lot of stories from owners whose databases are running two slow. In my experience, the three main places to look for problems are:
1) Faulty Database Design
2) Bad Queries
3) Server related factors
Faulty Database Design
Proper database design is the single most important factor for the ensuring performance and maintainability of the database. Here is what you need to answer when designing a table: “Can I reduce the size of data that each row will have?” Here is what you can do:
- Use unsigned numeric values when the application will not store negative numbers. Like the â€œquantity orderedâ€ of an item in an ecommerce application is never going to be -$125.
- Use Variable length values instead of fixed length value i.e. use varchar instead of char
- Do not use unnecessarily large field sizes. For most ecommerce application â€œunsigned smallintâ€ is more than enough to store inventory count. A field described as â€œunsigned smallintâ€ can store a max value of 65535.
- Donâ€™t ignore normalization; its helps prevent unnecessary repetition of data. The part B of this is, donâ€™t overuse normalization. If the table will not grow in size significantly, there is no point in normalization. For example, if the user table has just 20 rows (i.e. 20 employees in a company), all attempts of normalization are wasted.
- Use Index Keys. Donâ€™t decide keys by â€œThe customer id has to be indexed in the order tableâ€. If the order table is being searched 90% of the times by â€œorder dateâ€, it makes more sense to index â€œorder dateâ€.
Remember, how a table will be used should determine how it is designed. Spending time here will save years of frustration.
It sounds too good to be true but you wont believe the number of developers out there who completely suck at writing queries. There are two types of bad queries:
a) Unnecessary Queries: These are the queries that shouldnâ€™t have been made in the first place. The only way to avoid this is asking, â€œDo I really need this data?â€
b) Inefficient Queries: These are the queries that do not use the underlying table structure or MySQL functions in the correct way.
Here is a starting point to start looking at problem areas:
- Unnecessary usage of â€œSelect * fromâ€œstatements when the entire processing is being done on a single column. The more data is fetched from the server the more work MySQL has to do and more bandwidth it takes.
- Using sub-query instead of a join. On a properly designed database, joins are incredibly fast. Using sub-queries just shows a lack of knowledge.
- Improper use of Keys. This is especially valid for range checks. Remember to use the â€œExplainâ€ statement to check the usage of keys and then use the â€œuse keyâ€ statement in your â€œwhereâ€ clauses to force key usage.
Everything done correctly, there still may be some server factors that may be causing the system to be slow. These are: Hardware related and Server configuration related.
Here is what you can do about the hardware:
- The more RAM is on the system the better it is. MySQL frequently fetches data from the RAM and more the RAM is on the system, the better it is.
- Buy the fastest possible RAM! A slower RAM is just irony.
- Once you are settled with the RAM size and speed, look for processing speed. MySQL can use multiple processors.
Once you are satisfied with the hardware, there are a set of variables in â€œmy.cnfâ€ that you must look at:
- key_buffer_size: This describes the memory available to store the index keys. The default is 8 MB but you can set it to 25% of the RAM
- query_cache_size: This value is by default 0. if you have a lot of repeating queries like in reporting applications etc, make sure you set this value high.
- table_open_cache: This determines the number of table descriptors that MySQL will keep in the cache. The default value is 64. But, if you have 100 users accessing a table concurrently then this value should atleast be 100. You also have to take into considerations joins etc. Thus, this value should also be kept high.
I hope this article will take one step further in unlocking the mystery of slow servers and help solve some of the problems.
About our MySQL related services
We offer professional mysql database design, implementation and performance tuning services through http://www.script2please.com
- AI & MI
- Big Data
- Buy Hours
- Data Analytics
- Data Security
- Dedicated Hiring
- Digital Buzz
- Digital Payment
- Enterprise Mobility
- Flexi Hiring Solutions
- Flexible Operation
- Hire Experts
- Home Automation
- Industry News
- Inside Indus Net
- Marketing Automation
- Mobile App
- Mobile Game
- Modern Lifestyle
- MSME Industry
- MSME News
- News @ Indus Net
- Social Media
- Time Management