Many people do not know how SQL statements are executed in SQL Server. They worry that SQL statements they write will be misunderstood by SQL Server.
Many people do not know how SQL statements are executed in SQL server. They are worried that the SQL statements they write will be misunderstood by SQL SERVER.
For example:
Select * from table1 where name = ''zhangsan'' and tID> 10000
And execution:
Select * from table1 where tID> 10000 and name = ''zhangsan''
Some people do not know whether the execution efficiency of the preceding two statements is the same, because the two statements are indeed different from the statement sequence. If tID is an aggregate index, then, the next sentence can only be searched from the 10000 records in the table; in the previous sentence, you need to first look up several names = ''zhangsan'' from the entire table, and then propose the query results based on the constraints tID> 10000.
In fact, such a worry is unnecessary. SQL SERVER has a "query analysis optimizer", which can calculate the search conditions in the where clause and determine which index can narrow the search space of the table scan, that is, it can achieve automatic optimization.
Although the query optimizer can automatically optimize queries based on the where clause, it is still necessary for you to understand the working principle of the "query optimizer". If not, sometimes the query optimizer does not perform quick queries according to your intention.
In the query and analysis phase, the query optimizer checks each stage of the query and determines whether the data volume to be scanned is useful. If a phase can be used as a scan parameter (SARG), it is called an Optimized phase and the required data can be quickly obtained using the index.
Definition of SARG: An operation used to restrict search, because it usually refers to a specific match, a matching within a worthy range or an AND connection between two or more conditions. The format is as follows:
Column name Operator <常数 或 变量>
Or
<常数 或 变量> Operator column name
The column name can appear on one side of the operator, while the constant or variable appears on the other side of the operator. For example:
Name = 'zhang san'
Price> 5000
5000 <价格
Name = 'zhang san' and price> 5000
If an expression cannot meet the form of SARG, it cannot limit the search range, that is, SQL SERVER must determine whether each row meets all the conditions in the WHERE clause. Therefore, an index is useless for expressions that do not meet the SARG format.
After introducing SARG, let's summarize the experiences of using SARG and drawing different conclusions from some materials in practice:
1. Whether the Like statement belongs to the SARG depends on the type of the wildcard used.
For example, name like 'zhang % ', which belongs to SARG
However, name like '% Zhang' does not belong to SARG.
The reason is that the wildcard % is enabled in the string so that the index cannot be used.
2. or may cause full table scan.
Name = 'zhang san' and price> 5000 symbol SARG, but: Name = 'zhang san' or price> 5000 does not match SARG. Using or will cause a full table scan.
3. statements not in the SARG format caused by non-operators and functions
The most typical case for statements that do NOT meet the requirements of the SARG format is to include non-operator statements, such as: NOT ,! =, <> ,! <,!> , Not exists, not in, not like, and other functions. Below are several examples that do not meet the SARG format:
ABS (price) <5000
Name like '% 3'
Some expressions, such:
WHERE price * 2> 5000
SQL server will also regard it as SARG, and SQL SERVER will convert this formula:
WHERE price> 2500/2
However, we do not recommend this because sometimes SQL SERVER cannot guarantee that this conversion is completely equivalent to the original expression.
4. The role of IN is equal to OR.
Statement:
Select * from table1 where tid in (2, 3)
And
Select * from table1 where tid = 2 or tid = 3
If they are the same, they will cause a full table scan. If there is an index on the tid, its index will also become invalid.