1=1 usage and performance
Fuse: Found some where and JOIN on statements with 1=1 conditions in the optimized stored procedure .
Why do you use the WHERE 1=1
to facilitate the stitching of SQL conditions :
Instance:
DECLARE @SQL1 NVARCHAR (max), @SQL2 NVARCHAR (max), @CLASSID intset @CLASSID =2select @SQL1 = ' SELECT * from w_question WHERE 1 = 1 ' IF ISNULL (@CLASSID, 0) <>0select @[email protected]+ ' and classid= ' +convert (NVARCHAR), @CLASSID)
The final splicing statement is:
SELECT * from W_question WHERE 1=1 and classid=2
If not the conditions of the 1=1 will appear:
SELECT * from W_question WHERE and classid=2
Or:
SELECT * from W_question WHERE
Such an SQL statement, causing an error
WHERE 1=1 has an impact on performance
because the query parser filtered the 1=1 directly in the algebraic tree optimization phase. , so 1=1 has no effect on performance.
Instance:
SELECT * from W_questionselect * from W_question WHERE 1=1
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/E1/wKiom1WsY_nAgFmoAAF-PJhDBG4676.jpg "title=" Picture 1.png "alt=" Wkiom1wsy_nagfmoaaf-pjhdbg4676.jpg "/>
This article is from the "Sukun" blog, make sure to keep this source http://sukunwu.blog.51cto.com/10453116/1676267
1=1 Usage and performance