这个SQL语句,怎么写性能最高?
select top 1 table1.col1 ,table1.colo2
from table1 ,table2
where
'name1' NOT IN (SELECT userid FROM table1 where ...)
AND 'name2' NOT IN (SELECT userid FROM table1 where ...)
AND 'name3' NOT IN (SELECT userid FROM table1 where ...)
AND 'name4' NOT IN (SELECT userid FROM table1 where ...)
这个语句的效率实在太低,怎么能把集合先算出来。也就是把SELECT userid FROM table1 where ...先算出来为一个常量。然后再进行查询
如:
SELECT userid FROM table1 where ... 作为 Arr
然后
select top 1 table1.col1 ,table1.colo2
from table1 ,table2
where
'name1' NOT IN Arr
and 'name2' NOT IN Arr
and 'name3' NOT IN Arr
and 'name4' NOT IN Arr
另外能不能实现类似 'name1','name2','name3','name4' not in Arr(并在一起)这样的写法?