MySQL database foundation optimization (1)

When querying the data, it is found that the query time is very long, and you need to optimize SQL. First check the SQL execution plan and optimize it according to the SQL execution plan.

1. Index establishment to prevent index failure

    Specifically, Baidu!

    In particular, do not perform functions, arithmetic operations, or other expression operations on the left side of the "=" in the where clause, otherwise the system may not be able to use the index correctly

2. Paging query efficiency

  select * from t_user where  age >18 order by age desc limit 1000,10;

  If the amount of data in the t_user table is too large, even querying only 10 items is very slow. You can transfer the id from the previous query result as a condition, such as:

 select * from t_user where  age >18 and id>#{id} order by age desc limit 1000,10;

3. Use of in

    If the amount of data is large, replace in with exists!

4. Try to replace subqueries with join

5. Leftmost principle

     First understand the sql execution order. For example, a sql has 5 steps. Try to minimize the range when the first step is filtered.

 

to be continued......

 

Published 23 original articles · Like 3 · Visitors 6858

Guess you like

Origin blog.csdn.net/weixin_41834814/article/details/90737071