Home > Database > Mysql Tutorial > body text

10 little-known SQL statement optimizations

angryTom
Release: 2019-11-27 13:50:43
forward
2476 people have browsed it

10 little-known SQL statement optimizations

1. Some common SQL practices

(1) Negative conditional queries cannot use indexes

select * from order where status!=0 and stauts!=1
Copy after login

not in/not exists is not a good habit

Recommended "mysql video tutorial"

can be optimized as in query:

select * from order where status in(2,3)
Copy after login

(2) Leading fuzzy query cannot use index

select * from order where desc like '%XX'
Copy after login

but non-leading fuzzy query can:

select * from order where desc like 'XX%'
Copy after login

( 3) It is not appropriate to use indexes for fields with little data differentiation

select * from user where sex=1
Copy after login

Reason: There are only male and female genders, and very little data is filtered out each time, so it is not appropriate to use indexes.

Experience, you can use the index when you can filter 80% of the data. For order status, if there are few status values, it is not appropriate to use an index. If there are many status values ​​and a large amount of data can be filtered, an index should be established.

(4) Calculation on attributes cannot hit the index

select * from order where YEAR(date) < = &#39;2017&#39;
Copy after login

Even if an index is established on date, the entire table will be scanned, which can be optimized for value calculation:

select * from order where date < = CURDATE()
Copy after login
Copy after login

Or:

select * from order where date < = &#39;2017-01-01&#39;
Copy after login

2. Not a well-known SQL practice

(5) If most of the business is a single query, the performance of using Hash index is better, such as User Center

select * from user where uid=?
select * from user where login_name=?
Copy after login

Reason:

The time complexity of the B-Tree index is O(log(n))

The time complexity of the Hash index is O(1)

(6) Columns that are allowed to be null have potential pitfalls in queries

Single column indexes do not store null values, and composite indexes do not store all null values. If a column is allowed to be null, you may get " "Unexpected" result set

select * from user where name != &#39;shenjian&#39;
Copy after login

If name is allowed to be null, the index does not store null values, and these records will not be included in the result set.

So, please use not null constraints and default values.

(7) The leftmost prefix of the composite index is not the where order of the value SQL statement must be consistent with the composite index

The user center has established a composite index of (login_name, passwd)

select * from user where login_name=? and passwd=?
select * from user where passwd=? and login_name=?
Copy after login

can hit the index

select * from user where login_name=?
Copy after login
Copy after login

can also hit the index, which satisfies the leftmost prefix of the composite index

select * from user where passwd=?
Copy after login

cannot hit the index, and does not satisfy the leftmost prefix of the composite index

( 8) Use ENUM instead of string

ENUM saves TINYINT. Don’t include strings like "China", "Beijing" and "Technology Department" in the enumeration. The string space is large and the efficiency is poor. Low.

3. Niche but useful SQL practices

(9) If it is clear that only one result will be returned, limit 1 can improve efficiency

select * from user where login_name=?
Copy after login
Copy after login

Can be optimized to:

select * from user where login_name=? limit 1
Copy after login

Reason:

You know there is only one result, but the database does not know it. Tell it clearly and let it actively stop the cursor movement

(10) Putting calculations in the business layer instead of the database layer, in addition to saving data CPU, also has unexpected query cache optimization effects

select * from order where date < = CURDATE()
Copy after login
Copy after login

This is not a good SQL practice, it should be optimized as:

$curDate = date(&#39;Y-m-d&#39;);
$res = mysql_query(
    &#39;select * from order where date < = $curDate&#39;);
Copy after login

Reason:

Release the CPU of the database

Multiple calls, the incoming SQL is the same, the query cache can be used

(11) Forced type conversion will affect the entire table Scan

select * from user where phone=13800001234
Copy after login

Do you think it will hit the phone index? This is a big mistake. How can I change this statement?

Finally, one more thing, do not use select *, only return the required columns, which can greatly save the amount of data transmission and the memory usage of the database.

This article comes from the php Chinese website, mysql tutorial column, welcome to learn!

The above is the detailed content of 10 little-known SQL statement optimizations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!