Solution to the full table scan problem caused by the WHERE variable is null in SQL

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

Copy codeThe Code is as follows:
SET @ SQL = 'select * FROM Comment with (nolock) WHERE 1 = 1
And (@ ProjectIds Is Null or ProjectId = @ ProjectIds)
And (@ Scores is null or Score = @ Scores )'


I remember that when I was doing Oracle development, this method would scan the entire table and could not use the index. I don't know if it is the same in SQL Server, so I made a simple test.
1. Create a table structure and index for testing:
Copy codeThe Code is as follows:
Create table aaa (id int IDENTITY, name varchar (12), age INT)
Go
Create index idx_age ON aaa (age)
GO

2. Insert 10 thousand pieces of test data:
Copy codeThe Code is as follows:
DECLARE @ I INT;
SET @ I = 0;
WHILE @ I <10000
BEGIN
Insert into aaa (name, age) VALUES (CAST (@ I as varchar), @ I)
SET @ I = @ I + 1;
END
GO

3. enable the execution plan display first:
In the SQL Server Management Studio query window, right-click any position in the window and select "include actual execution plan ":

4. Start the test and use the following SQL to perform the test:
Copy codeThe Code is as follows:
DECLARE @ I INT;
SET @ I = 100
SELECT * FROM aaa WHERE (@ I is null or age = @ I)
SELECT * FROM aaa WHERE (age = @ I OR @ I IS NULL)
SELECT * FROM aaa WHERE age = isnull (@ I, age)
SELECT * FROM aaa WHERE age = @ I

The test results are as follows:

You can see that even if @ I has a value, no matter whether @ I is null is placed in front or behind, the index of age cannot be used. In addition, age = ISNULL (@ I, age) you cannot use the index.

The final conclusion IS that SQL Server, like ORACLE, will scan the entire table if the variable IS NULL IS added to the condition.

We recommend that you change the SQL statement:
Copy codeThe Code is as follows:
DECLARE @ I INT;
SET @ I = 100

DECLARE @ SQL NVARCHAR (MAX)
SET @ SQL = 'select * FROM aaa'
IF @ I IS NOT NULL
SET @ SQL = @ SQL + 'where age = @ I'
EXEC sp_executesql @ SQL, n' @ I int', @ I

Of course, if there is only one condition, you can design two SQL statements, for example:
Copy codeThe Code is as follows:
DECLARE @ I INT;
SET @ I = 100
IF @ I IS NOT NULL
SELECT * FROM aaa WHERE age = @ I
ELSE
SELECT * FROM aaa

However, if there are more conditions, the number of SQL statements increases. Therefore, we recommend that you use the EXEC solution.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.