Top 10 Optimization Tips for MySQL database _ MySQL

Source: Internet
Author: User
Top 10 optimization techniques for MySQL database bitsCN.com

1. optimize your MySQL Query cache

You can enable the high-speed query cache for queries on the MySQL server. It is one of the most effective ways to improve performance by quietly processing the database engine in the background. When the same query is executed multiple times, if the result is extracted from the cache, it is quite fast.

But the main problem is that it is so easy to hide that most of our programmers will ignore it. In some processing tasks, we can actually prevent Query caching.

1. // query cache does NOT work

2. $ r = mysql_query ("SELECT username FROM user WHERE signup_date> = CURDATE ()");

3.

4. // query cache works!

5. $ today = date ("Y-m-d ");

6. $ r = mysql_query ("SELECT username FROM user WHERE signup_date >=$ today ");

7.

8. // query cache does NOT work

9. $ r = mysql_query ("SELECT username FROM user WHERE signup_date> = CURDATE ()");

10.

11. // query cache works!

12. $ today = date ("Y-m-d ");

13. $ r = mysql_query ("SELECT username FROM user WHERE signup_date >=$ today ");

2. use EXPLAIN to make your SELECT query clearer

Using the EXPLAIN keyword is another MySQL optimization technique that helps you understand what MySQL is performing for query operations. this helps you find the bottleneck, it also shows where the query or table structure is faulty.

The EXPLAIN query results can tell you how indexes are being referenced, and how tables are scanned and sorted.

Implement a SELECT query (preferably a complicated one with the joins method). add your keyword explanation in it. here we can use phpMyAdmin, it will tell you the results in the table. For example, if I forget to add a column to an index when executing joins, the explanation can help me locate the problem.

After adding the index to group_id field

3. use LIMIT 1 to get a unique row

Sometimes, when you want to query a table, you know that you only need to read a row. You may go to a very unique record, or just check the number of any existing records. they all satisfy your WHERE clause.

In this case, adding a LIMIT 1 will make your query more effective. In this way, the database engine will only stop scanning after 1, instead of scanning the entire table or index. ,

1. // do I have any users from Alabama?

2. // what NOT to do:

3. $ r = mysql_query ("SELECT * FROM user WHERE state = Alabama ");

4. if (mysql_num_rows ($ r)> 0 ){

5 .//...

6 .}

7. // much better:

8. $ r = mysql_query ("SELECT 1 FROM user WHERE state = Alabama LIMIT 1 ");

9. if (mysql_num_rows ($ r)> 0 ){

10 .//...

11 .}

4. search fields in the index

An index is not only a primary key or a unique key. If you want to search for any column in the table, you should always point to the index.

5. ensure that the connected indexes are of the same type

If the application contains multiple connection queries, make sure that the linked columns are indexed on both tables. This affects how MySQL optimizes the internal join operation.

In addition, the columns to be added must be of the same type. For example, if you add a DECIMAL column and an int column to another table, MySQL cannot use at least one of the indexes. Even if the character encoding must be of the same string type.

1. // looking for companies in my state

2. $ r = mysql_query ("SELECT company_name FROM users

3. left join companies ON (users. state = companbitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.