SQL statement optimization--with MySQL as an example

Source: Internet
Author: User
Tags mysql index

This article refers to the following article:

1: "True and dry" MySQL Index and optimization combat

2:mysql Statement Execution Process

Several methods of 3:sql optimization

I have divided the SQL statement optimization into three aspects,(this does not include the optimization of business logic and the use of caching ): index optimization, use of keywords, effective evasion.

1: Index optimization can be divided into index hit invalid optimization and index hit inefficient optimization;

1): Invalid index hit condition:

A: If it is a single-column index, it is best not to appear null; It is said to be allowed to be null, which may result in a set of results that does not conform to expectations;

B: Index leading blur such as: like "%xx";

C: Negative condition query cannot use index: Optimization strategy: Optimize the negative condition to in query; Negative conditions are:! =, <>, not in, not exists, not like;

D: The index column participates in the calculation;

E: The index column to do the forced conversion processing;

2): Index hit inefficient condition:

A: From efficiency: union > in > or; recommended in;

B: Union index the leftmost prefix principle;

C: For order by to take advantage of the validity of the index:

2: Use of keywords:

A: optimize in with exists;  (Well, here I have a little question about optimizing the negative to conditional queries to in and the Union, or to in;) It says to use exists to optimize in;  So the question is, do you want to use the exists to optimize it? )

B: Use InStr () function to optimize like;

InStr (A, B) > 0 <==> find Like ("%b%") from column A;

InStr (A, B) = 0 <==> Find not-like ("%b%") from column A;

InStr (A, B) = 1 <==> find like ("b%") from column A;

C: More than three tables is best not to join.

Fields that require joins, data types must be consistent, and multiple table associated queries ensure that the associated fields need to be indexed

3: effective evasion:

A: use of *;

Try to use all column names instead of *;

B:union and UNION All

If the use of Union is determined, no duplicate records will be generated;  Then the Union is used; Union is more efficient than union all

C: If you know that only one result is returned, limit 1 can improve efficiency.

D: Using deferred correlation

E: Use numeric fields as much as possible:

SQL statement optimization--with MySQL as an example

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.