当前位置:网站首页>SQL common optimization

SQL common optimization

2022-07-07 20:00:00 Whiteye too white

sql Optimize

1. Small tables drive large tables

First, query the table with less data , Then execute the query of the table with more data .

2. Index building , A table does not exceed 5 An index

Avoid large memory consumption .

3. Go to the index , Try to meet the leftmost match , Avoid index invalidation

Index failure :
(1)select *
(2)>、<、!=、between
(3) In front of % Of like Inquire about .
(4)where The field value type is inconsistent with the database , There is an automatic conversion .
(5)or Connect different fields .
(6)or Connect the same field , But there is >、<、!= Non indexed query .( Only when both left and right queries are indexes .)
(7) Function or operation causes index invalidation .
(8)IS NULL Don't walk index ,IS NOT NULL Go to the index ( Table design : When not necessary , The field should not be NULL, Set the default empty string or 0)
(9) When querying the range of composite cables , The following index is invalid .
(10)IN Can walk the index , But when IN When the value range of is large, the index will be invalid .

  • ep_range_index_dive_limit This parameter affects in Use index or not ,MySQL 5.6 Default 10, MySQL.5.7 Default 200. But our code tends to be controlled in 50 Inside . A Table data is greater than B Table data , choice in Than exists High execution efficiency . contrary ,A Table data is less than B Table data , choice exists More efficient . in Execute subquery first ,exists First perform the appearance .

(11)not in Will invalidate the index , In either case not exists All ratio not in Efficient .

  • Use left join or not exists To optimize not in operation .
4. Try to separate hot and cold data

Reduce the number of query table columns , Avoid cold data filtering .

5. Adjust the order of index columns

The uniqueness is better 、 Field is short 、 The frequently used column is placed on the far left of the union index

6. Override indexes are a priority for frequent queries

Query the data together when searching .

7. Avoid implicit conversions of data types

Avoid index invalidation .

8. No use SELECT * You have to use SELECT < Field list > Inquire about

There will be redundant fields , Consume more CPU And network bandwidth Will not be able to use overlay index .

9. Avoid subqueries , The subquery can be optimized to join operation

Subqueries will generate a large number of temporary tables without indexes .

10. Avoid using JOIN Too many tables associated

The more associated tables, the larger the associated cache .

11. Used when there is obviously no duplicate value UNION ALL instead of UNION

UNION ALL No de duplication operation , Speed up query .

12. Big SQL Split , More than one species SQL Batch processing

One SQL You can only use one cpu Calculate , After splitting, multiple cpu Parallel computing .

13. WHERE Functional transformations and calculations for columns are forbidden in clauses

Avoid index invalidation .

14. Use count(*) instead of count( Name )?

Comprehensive performance :count( Non primary key columns ) < count( Primary key ) < count(1) ≈ count()
count(
) yes SQL92 The syntax for defining the number of standard statistics lines , It's not about the database ,count(*) The statistical value is NULL The line of , and
count( Name ) This column is not counted NULL Row of values .

原网站

版权声明
本文为[Whiteye too white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071749177864.html