代码如下:
SET @sql = 'SELECT * FROM Comment with(nolock) WHERE 1=1
And (@ProjectIds Is Null or ProjectId = @ProjectIds)
And (@scores is null or score =@scores)'
And (@ProjectIds Is Null or ProjectId = @ProjectIds)
And (@scores is null or score =@scores)'
印象中记得,以前在做Oracle开发时,这种写法是会导致全表扫描的,用不上索引,不知道sql Server里是否也是一样呢,于是做一个简单的测试
1、建立测试用的表结构和索引:
代码如下:
CREATE TABLE aaa(id int IDENTITY,NAME VARCHAR(12),age INT)
go
CREATE INDEX idx_age ON aaa (age)
GO
go
CREATE INDEX idx_age ON aaa (age)
GO
2、插入1万条测试数据:
代码如下:
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
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、先开启执行计划显示:
在sql Server Management Studio的查询窗口里,右击窗口任意位置,选择“包含实际的执行计划”:
4、开始测试,用下面的sql进行测试:
代码如下:
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
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
测试结果如下:
可以看到,即使@i有值,不管@i IS NULL是放在前面还是放在后面,都无法用到age的索引,另外age=ISNULL(@i,age)也用不上索引
最终结论,sql Server跟ORACLE一样,如果条件里加了 变量 IS NULL,都会导致全表扫描。
建议sql改成:
代码如下:
DECLARE @i INT;
SET @i=100
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
当然,如果只有一个条件,可以设计成2条sql,比如:
代码如下:
DECLARE @i INT;
SET @i=100
IF @i IS NOT NULL
SELECT * FROM aaa WHERE age = @i
ELSE
SELECT * FROM aaa
SET @i=100
IF @i IS NOT NULL
SELECT * FROM aaa WHERE age = @i
ELSE
SELECT * FROM aaa
但是,如果条件多了,sql数目也变得更多,所以建议用EXEC的方案