MySQL Optimization tips

Source: Internet
Author: User

Defragmentation:

MySQL data is stored sequentially on disk, if the data table has frequent update changes, then the data will form a lot of fragments, slow speed and unfavorable to the index;

There are two ways of optimizing fragmentation:

ALTER TABLE user engine InnoDB; in fact, the user this table was originally InnoDB, this sentence does not seem to make any sense, but MySQL will be re-structured data

Optimize table user; can also be repaired;

Fragmentation optimization is a matter of CPU and memory, preferably performed at night;

Unconventional min Max optimization

Select min (age) from table;

Regardless of how much data the table has, this statement is very fast, because MySQL is stored within the relevant values, through the explain can be seen by MySQL has been optimized;

Change the statement a little bit;

Select min (age) from table where Status=1;

Viewing by explain is a full table scan;

MySQL to a row of the check out to see if the status is 1, if it is, that compared to the last age to look at the small, a bit like bubble sort;

There is a special optimization method, in the context of age index;

Select min from table use index (age) where Status=1 limit 1;

Force MySQL to use the age index because the index is ordered, so the first one is the minimum value, and Max has the same

Count non-conventional optimizations

Select COUNT (*) from table;

By explain you can see that MySQL has been optimized to the solid level; very fast.

But:

Select COUNT (*) from table where id>100;

The speed drops rapidly, explain has seen the use of full-table scan, starting from 100 to scan the full table

However, this statement is fast and only uses the first 100 scans:

Select COUNT (*) from table where id<100;

So the optimization method is to use mathematical addition and subtraction:

(SELECT COUNT (*) from table)-(SELECT COUNT (*) from table where id<100)

Union optimization:

When connecting query results, use union all as necessary, because the union all does not filter data, high efficiency, and the Union to filter the data, the cost is great;

MySQL Optimization tips

Related Article

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.