SQL Not IN 查询正在花费时间与类似 table
SQL Not IN query is taking time with similar table
我有 2 个表假设 table_1 & table_2
table_1 有 56 列和 120 万条记录
我的查询就像
table_1 喜欢
RollNumber | Subject | G | Part | Status
------------------------------------------------
1 | 1 | 1 | 1 | 1
1 | 1 | 1 | 2 | 1
1 | 2 | 1 | 1 | 1
1 | 2 | 1 | 2 | 5
1 | 3 | 1 | 1 | 0
1 | 3 | 1 | 2 | 1
2 | 1 | 2 | 1 | 1
2 | 1 | 2 | 2 | 1
2 | 2 | 2 | 1 | 1
2 | 2 | 2 | 2 | 1
2 | 3 | 2 | 1 | 1
2 | 3 | 2 | 2 | 1
3 | 1 | 2 | 1 | 1
3 | 1 | 2 | 2 | 1
3 | 2 | 2 | 1 | 1
3 | 2 | 2 | 2 | 1
3 | 3 | 2 | 1 | 0
3 | 3 | 2 | 2 | 1
我想要来自 table_1 的所有 RollNumber(按第 2 和第 3 列分组),其中任何状态为 0,但不希望状态也为 5(或非 1)的学生
我试过了
select * from table_1 as t1
inner join table_2 as t2
on t1.column2 = t2.column2 and t1.column3 = t2.column3 and t1.column4 = t2.column4
where t1.column1 not in
(select column1 from table_1 where status = 5)
这是我qhole查询的最内层查询
我也试过 EXCEPT 子句
两个查询执行时间太长
您可以使用 EXISTS
代替 NOT IN.
这会更快,因为会有 boolean
比较而不是 string
比较。
select * from table_1 as t1
inner join table_2 as t2
on t1.column1 = t2.column1 and t1.column2 = t2.column2 and t1.column3 = t2.column3
where not EXISTS
(select 1 from table_1 where status = 5 and t1.column3 = table_1.column3)
尝试使用 NOT EXISTS
而不是 NOT IN
SELECT *
FROM table_1 AS t1
INNER JOIN table_2 AS t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.column2 AND t1.column3 = t2.column3
WHERE NOT EXISTS(
SELECT 1
FROM table_1
WHERE status=5 AND column3=t1.column3
)
从 SQL Server 2008 开始,您可以使用 count() over()
计算给定组中具有特定值的总行数。
在这种情况下,您需要计算每组 status <> 1
的数量,并且只计算属于 0
组的 select 行。
select * from (
select * ,
count(case when status <> 1 then 1 end) over(partition by RollNumber, G) c
from table_1
) t where c = 0
我有 2 个表假设 table_1 & table_2 table_1 有 56 列和 120 万条记录 我的查询就像
table_1 喜欢
RollNumber | Subject | G | Part | Status
------------------------------------------------
1 | 1 | 1 | 1 | 1
1 | 1 | 1 | 2 | 1
1 | 2 | 1 | 1 | 1
1 | 2 | 1 | 2 | 5
1 | 3 | 1 | 1 | 0
1 | 3 | 1 | 2 | 1
2 | 1 | 2 | 1 | 1
2 | 1 | 2 | 2 | 1
2 | 2 | 2 | 1 | 1
2 | 2 | 2 | 2 | 1
2 | 3 | 2 | 1 | 1
2 | 3 | 2 | 2 | 1
3 | 1 | 2 | 1 | 1
3 | 1 | 2 | 2 | 1
3 | 2 | 2 | 1 | 1
3 | 2 | 2 | 2 | 1
3 | 3 | 2 | 1 | 0
3 | 3 | 2 | 2 | 1
我想要来自 table_1 的所有 RollNumber(按第 2 和第 3 列分组),其中任何状态为 0,但不希望状态也为 5(或非 1)的学生
我试过了
select * from table_1 as t1
inner join table_2 as t2
on t1.column2 = t2.column2 and t1.column3 = t2.column3 and t1.column4 = t2.column4
where t1.column1 not in
(select column1 from table_1 where status = 5)
这是我qhole查询的最内层查询
我也试过 EXCEPT 子句
两个查询执行时间太长
您可以使用 EXISTS
代替 NOT IN.
这会更快,因为会有 boolean
比较而不是 string
比较。
select * from table_1 as t1
inner join table_2 as t2
on t1.column1 = t2.column1 and t1.column2 = t2.column2 and t1.column3 = t2.column3
where not EXISTS
(select 1 from table_1 where status = 5 and t1.column3 = table_1.column3)
尝试使用 NOT EXISTS
而不是 NOT IN
SELECT *
FROM table_1 AS t1
INNER JOIN table_2 AS t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.column2 AND t1.column3 = t2.column3
WHERE NOT EXISTS(
SELECT 1
FROM table_1
WHERE status=5 AND column3=t1.column3
)
从 SQL Server 2008 开始,您可以使用 count() over()
计算给定组中具有特定值的总行数。
在这种情况下,您需要计算每组 status <> 1
的数量,并且只计算属于 0
组的 select 行。
select * from (
select * ,
count(case when status <> 1 then 1 end) over(partition by RollNumber, G) c
from table_1
) t where c = 0