mysql full table scan scene

Full table scan is the process of searching for each record in a database table, until all records that match a given criteria returned. Usually in a database, the index for non-table queries are generally referred to as full table scan; however, sometimes we even add the index, but when we write SQL statements unreasonable time will cause a full table scan.
The following is the full table scan often cause SQL statements and responses:
1. Use null as the judgment conditions
such as: select account from member where nickname = null;
recommended field as much as possible in the design of the field's default value is set to 0, change Account WHERE = Nickname is 0 SELECT;
2. left like% XXX% Fuzzy queries
such as: select account from member where nickname like '% XXX%' or select account from member where nickname like ' % XXX'
recommended select account from member where nickname like 'XXX%', if you have to use to do the query, we need to assess the consequences of the current table full table scan result; Liu @ Katong cool heard
3. use or as the connection conditions
such as: select account from member where id = 1 or id = 2;
recommended union all, changed from Member Account WHERE ID SELECT. 1 = All SELECT Account from Union Member WHERE ID = 2;
4. when in use (Not in)
Such as: select account from member where id in (1,2,3)
if the data is continuous, may instead select account where id between 1 and 3 ; when there is less data may also refer to the use of union;
or: select account from member where id in (select accountid from department where id = 3), can be changed from Member WHERE ID select account exsits (SELECT WHERE ID = accountId from Department. 3)
not in may correspond not exists;
5. when not in use
, such as select account the above mentioned id not in the WHERE (, 2, 3)
6. use! = Or <> when
recommended <, <=, =,> ,> =, between like;
also cause token with an operation of the index field 7.
The select account where salary * 0.8 = 1000 or select account where sustring ( nickname, 1,3) = 'zhangxiaolong' ;
8. use count (*)
as select count (*) from member;
recommended COUNT SELECT (. 1) from Member;
9. The use as a query parameter when
Such as select account from member where nickname = @name
Because the SQL statement is not executed to determine the parameters at the time of compilation, which will not be able to query data through the index, so try to avoid; LIU Katong heard @ cool

When non-standard wording caused a full table scan will result in additional consumption of CPU and memory, and even cause the server to crash. You will inevitably encounter some beginners in teamwork, in addition to proper arrangement of tasks, a senior engineer to do well Code Review. Otherwise, when we have huge amounts of data, non-standard statement will bring very serious consequences, must be careful, be careful
----------------
Disclaimer: This article is CSDN bloggers " xiaowu_913 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/xiaowu_913/article/details/72844883

Guess you like

Origin www.cnblogs.com/aligege/p/11594161.html