SQL optimization lecture (7)-reasonable use of variables

SQL column

Summary of SQL basics

SQL advanced knowledge summary

Novice friends may rarely use variables. In fact, variables are often used in the data query process and can effectively improve the query speed.

1
What is a variable

A variable is actually a variable parameter that we define, and its basic syntax is as follows:

-Define a variable named @I and specify its type as an integer

DECLARE @I VARCHAR(20)

- Assign a value to the variable @I

SET @I='SQL database development'

--Output the value of @I

SELECT @I

Result: SQL database development

  • Among them, the DECLARE @ part is a fixed wording, @I is the name of a variable, and the variable must have a defined type, which is generally defined as a character type, an integer type, and a time type.

  • The assignment part SET is also written in a fixed way, that is, to assign a value to the variable @I, and the value on the right of = is the assignment content.

  • After the variable is defined, it can be brought into the query statement. Each time you only need to modify the assignment part, the query statement will query the corresponding result according to the assignment content.

2
Why use variables

After using variables, if the same query statement only has different assignments, the first execution plan can be reused to achieve the effect of one parsing and multiple reuse, reducing the parsing of the execution plan will increase the query speed accordingly. Let's look at the following example:

SELECT * FROM T1 WHERE ORDER_ID='112';

SELECT * FROM T1 WHERE ORDER_ID='113';

If these two query statements are executed separately, the query optimizer considers it to be a different SQL statement and needs to be parsed twice.

We use variables to modify it

DECLARE @ORDER_ID VARCHAR(20)

SET @ORDER_ID='112'

SELECT * FROM T1 WHERE ORDER_ID=@ORDER_ID;

After execution, you only need to modify the value of @ORDER_ID to '113', and you can reuse the execution plan above.

Since the above statement is relatively simple, the effect may not be visible, but if you encounter a more complex query statement, variable query can often play a very good effect.

3
When should you/should not use variables

  • Variables can be used in common online queries once, and the variables can be passed to the database as parameters, so that a query can be implemented and the execution plan can be reused.

  • If you query a certain statement alone for a long time, such as more than half an hour, this kind of use of variables has no effect.

4 There are two sides to
variable peeking
things. Variables can improve query efficiency for common queries. But there are exceptions, such as when the field in the WHERE condition is "slanted field".

"Tilt field" means that most of the values ​​in this column are the same. For example, in the census form, in the column of "ethnicity", more than 90% are Han nationality. So if a SQL statement wants to query the population of the 30-year-old Han nationality, the column "nationality" must be placed in the WHERE condition. At this time, if you use bind variables @NATION, there will be a big problem.

If the first value passed by @NATION is "Han nationality", then the entire execution plan will inevitably choose a table scan.

DECLARE @NATION VARCHAR(50)

SET @NATION='Han Nationality'

SELECT * FROM People WHERE AGE=30 AND NATION=@NATION;

When the second value is passed in as "She", the proportion of "She" in the table may be only one in ten thousand under normal circumstances, and index search should be used.

DECLARE @NATION VARCHAR(50)

SET @NATION='She Nationality'

SELECT * FROM People WHERE AGE=30 AND NATION=@NATION;

Since the execution plan of the "Han nationality" analyzed for the first time is reused, the table scan method will also be used for the second time. This problem is the famous "variable peeking", and it is recommended not to use bind variables for "skew fields".

Today’s lesson will stop here. If you don’t understand anything about the variables, you can leave a message below and I will reply one by one.

Guess you like

Origin blog.51cto.com/15057820/2656475