求代替not in的语句

快跑稻草人 2016-12-23 06:25:28
我有两个表A,B,其中表B数据量非常大,这两个表中都有字段 UserID和Type

我需要查询A表中这两个字段都没有在B中这两个字段中出现的行

比如说

Select * from A where UserID not in (select UserID from B)


但是这两个条件必须同时满足,比如说A中的UserID 和 Type 在B表中不存在同一行就行

如果B表中有一行存在A表中的UserId 但不存在Type,这样的也不符合

请问应该如何构造语句
...全文
604 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
gw6328 2016-12-30
  • 打赏
  • 举报
回复
没有用,这个是设计的问题。你想,你要查询出不在一个表里的东西,你会把这个表查找完的。 还有exists not in 这些性能基本上没有太大差异。不要误解网上的一些说明把not in改exists就好了,人家说的没有错,是说的叫你少用 not in这种方式而是常用 in,exists这种方式,如果你改的方式不对起不到效果的。
吉普赛的歌 版主 2016-12-26
  • 打赏
  • 举报
回复
引用 21 楼 ljaahh 的回复:
[quote=引用 19 楼 yenange 的回复:] [quote=引用 16 楼 ljaahh 的回复:] [quote=引用 14 楼 yenange 的回复:] [quote=引用 12 楼 ljaahh 的回复:] [quote=引用 7 楼 yenange 的回复:]
create index ix_a_userid_type on a(userId, type)
create index ix_b_userid_type on b(userId, type)
加两个索引再说吧。 此外, 你说表大, 具体有多大, 贴出来看看:
exec sp_spaceused 'a'
exec sp_spaceused 'b'
[/quote] 到底多少数据?第 2 个问题你还没有回答?[/quote] 60多w条,如果用not in需要20秒[/quote] 用 not exist 呢? 还有上面的索引你建了没有?[/quote] 这个数据不重要,但是常用,所以不想建立索引,not exists是一样的[/quote] 为什么不建立索引? 又要马儿跑, 又要马儿不吃草? 优化, 总要遵循一定的规则。索引占不了你多少空间。
快跑稻草人 2016-12-26
  • 打赏
  • 举报
回复
引用 19 楼 yenange 的回复:
[quote=引用 16 楼 ljaahh 的回复:] [quote=引用 14 楼 yenange 的回复:] [quote=引用 12 楼 ljaahh 的回复:] [quote=引用 7 楼 yenange 的回复:]
create index ix_a_userid_type on a(userId, type)
create index ix_b_userid_type on b(userId, type)
加两个索引再说吧。 此外, 你说表大, 具体有多大, 贴出来看看:
exec sp_spaceused 'a'
exec sp_spaceused 'b'
[/quote] 到底多少数据?第 2 个问题你还没有回答?[/quote] 60多w条,如果用not in需要20秒[/quote] 用 not exist 呢? 还有上面的索引你建了没有?[/quote] 这个数据不重要,但是常用,所以不想建立索引,not exists是一样的
0与1之间 2016-12-26
  • 打赏
  • 举报
回复
那你可以考虑用EXISTS查询数据建个临时表,再与表关联查询删除不要的数据
吉普赛的歌 版主 2016-12-26
  • 打赏
  • 举报
回复
引用 16 楼 ljaahh 的回复:
[quote=引用 14 楼 yenange 的回复:] [quote=引用 12 楼 ljaahh 的回复:] [quote=引用 7 楼 yenange 的回复:]
create index ix_a_userid_type on a(userId, type)
create index ix_b_userid_type on b(userId, type)
加两个索引再说吧。 此外, 你说表大, 具体有多大, 贴出来看看:
exec sp_spaceused 'a'
exec sp_spaceused 'b'
[/quote] 到底多少数据?第 2 个问题你还没有回答?[/quote] 60多w条,如果用not in需要20秒[/quote] 用 not exist 呢? 还有上面的索引你建了没有?
shoppo0505 2016-12-26
  • 打赏
  • 举报
回复
建议先select into到一个临时表中,然后删除不需要的数据。
卖水果的net 版主 2016-12-25
  • 打赏
  • 举报
回复
引用 17 楼 ljaahh 的回复:
[quote=引用 6 楼 wmxcn2000 的回复:]
-- 试试这个
SELECT * 
FROM TABLE_A AS A 
LEFT JOIN  TABLE_B  B ON  A.USERID= B.USERID AND A.TYPE=B.TYPE
WHERE  B.USERID IS NULL
这条语句不管用[/quote] 贴你的表结构,和你想要的结果。
快跑稻草人 2016-12-25
  • 打赏
  • 举报
回复
引用 6 楼 wmxcn2000 的回复:
-- 试试这个
SELECT * 
FROM TABLE_A AS A 
LEFT JOIN  TABLE_B  B ON  A.USERID= B.USERID AND A.TYPE=B.TYPE
WHERE  B.USERID IS NULL
这条语句不管用
快跑稻草人 2016-12-25
  • 打赏
  • 举报
回复
引用 14 楼 yenange 的回复:
[quote=引用 12 楼 ljaahh 的回复:] [quote=引用 7 楼 yenange 的回复:]
create index ix_a_userid_type on a(userId, type)
create index ix_b_userid_type on b(userId, type)
加两个索引再说吧。 此外, 你说表大, 具体有多大, 贴出来看看:
exec sp_spaceused 'a'
exec sp_spaceused 'b'
[/quote] 到底多少数据?第 2 个问题你还没有回答?[/quote] 60多w条,如果用not in需要20秒
LongRui888 2016-12-24
  • 打赏
  • 举报
回复
引用 12 楼 ljaahh 的回复:
[quote=引用 7 楼 yenange 的回复:]
create index ix_a_userid_type on a(userId, type)
create index ix_b_userid_type on b(userId, type)
加两个索引再说吧。 此外, 你说表大, 具体有多大, 贴出来看看:
exec sp_spaceused 'a'
exec sp_spaceused 'b'
引用 10 楼 yupeigu 的回复:
[quote=引用 9 楼 ljaahh 的回复:] [quote=引用 8 楼 yupeigu 的回复:] [quote=引用 4 楼 ljaahh 的回复:] [quote=引用 2 楼 yupeigu 的回复:] Select * from A where not exists (select 1 from B where a.UserID = b.UserID and a.type = b.type )
而且由于数据太多,这样查询要20多秒,太慢了[/quote] 你的语句能贴个详细的出来吗? 另外,正在情况下20秒查询出来的数据有多少条[/quote] 只有1-2条也是20度秒,我的语句就是你说的这个 not exists,只是B表数据量过大,而且有一个字段每一行都有大量数据[/quote] 1-2条,那还是很有希望能优化到非常快的速度的,可以建索引试试,不过索引是依赖于你的 where条件里的字段的,如果你有大量的查询条件,最好选择一个 常用的查询条件字段做 索引,然后再试试,还有B表的 userid和type都要建索引。 最好把执行计划贴出来[/quote] 查询条件就2个,如果按照7楼的建立索引,查询sql是一样的吧,会不会需要很大的空间[/quote] 不会需要很大的空间,索引本质上就是空间换时间的一种优化方法
专注or全面 2016-12-24
  • 打赏
  • 举报
回复
估计是没有索引
吉普赛的歌 版主 2016-12-24
  • 打赏
  • 举报
回复
引用 12 楼 ljaahh 的回复:
[quote=引用 7 楼 yenange 的回复:]
create index ix_a_userid_type on a(userId, type)
create index ix_b_userid_type on b(userId, type)
加两个索引再说吧。 此外, 你说表大, 具体有多大, 贴出来看看:
exec sp_spaceused 'a'
exec sp_spaceused 'b'
[/quote] 到底多少数据?第 2 个问题你还没有回答?
快跑稻草人 2016-12-23
  • 打赏
  • 举报
回复
引用 7 楼 yenange 的回复:
create index ix_a_userid_type on a(userId, type)
create index ix_b_userid_type on b(userId, type)
加两个索引再说吧。 此外, 你说表大, 具体有多大, 贴出来看看:
exec sp_spaceused 'a'
exec sp_spaceused 'b'
引用 10 楼 yupeigu 的回复:
[quote=引用 9 楼 ljaahh 的回复:] [quote=引用 8 楼 yupeigu 的回复:] [quote=引用 4 楼 ljaahh 的回复:] [quote=引用 2 楼 yupeigu 的回复:] Select * from A where not exists (select 1 from B where a.UserID = b.UserID and a.type = b.type )
而且由于数据太多,这样查询要20多秒,太慢了[/quote] 你的语句能贴个详细的出来吗? 另外,正在情况下20秒查询出来的数据有多少条[/quote] 只有1-2条也是20度秒,我的语句就是你说的这个 not exists,只是B表数据量过大,而且有一个字段每一行都有大量数据[/quote] 1-2条,那还是很有希望能优化到非常快的速度的,可以建索引试试,不过索引是依赖于你的 where条件里的字段的,如果你有大量的查询条件,最好选择一个 常用的查询条件字段做 索引,然后再试试,还有B表的 userid和type都要建索引。 最好把执行计划贴出来[/quote] 查询条件就2个,如果按照7楼的建立索引,查询sql是一样的吧,会不会需要很大的空间
LongRui888 2016-12-23
  • 打赏
  • 举报
回复
引用 3 楼 ljaahh 的回复:
[quote=引用 2 楼 yupeigu 的回复:] Select * from A where not exists (select 1 from B where a.UserID = b.UserID and a.type = b.type )
如果我要删除呢,删除符合条件的行?谢谢[/quote] 一样的,把select * 改成 delete 就ok了
LongRui888 2016-12-23
  • 打赏
  • 举报
回复
引用 9 楼 ljaahh 的回复:
[quote=引用 8 楼 yupeigu 的回复:] [quote=引用 4 楼 ljaahh 的回复:] [quote=引用 2 楼 yupeigu 的回复:] Select * from A where not exists (select 1 from B where a.UserID = b.UserID and a.type = b.type )
而且由于数据太多,这样查询要20多秒,太慢了[/quote] 你的语句能贴个详细的出来吗? 另外,正在情况下20秒查询出来的数据有多少条[/quote] 只有1-2条也是20度秒,我的语句就是你说的这个 not exists,只是B表数据量过大,而且有一个字段每一行都有大量数据[/quote] 1-2条,那还是很有希望能优化到非常快的速度的,可以建索引试试,不过索引是依赖于你的 where条件里的字段的,如果你有大量的查询条件,最好选择一个 常用的查询条件字段做 索引,然后再试试,还有B表的 userid和type都要建索引。 最好把执行计划贴出来
快跑稻草人 2016-12-23
  • 打赏
  • 举报
回复
引用 8 楼 yupeigu 的回复:
[quote=引用 4 楼 ljaahh 的回复:] [quote=引用 2 楼 yupeigu 的回复:] Select * from A where not exists (select 1 from B where a.UserID = b.UserID and a.type = b.type )
而且由于数据太多,这样查询要20多秒,太慢了[/quote] 你的语句能贴个详细的出来吗? 另外,正在情况下20秒查询出来的数据有多少条[/quote] 只有1-2条也是20度秒,我的语句就是你说的这个 not exists,只是B表数据量过大,而且有一个字段每一行都有大量数据
LongRui888 2016-12-23
  • 打赏
  • 举报
回复
引用 4 楼 ljaahh 的回复:
[quote=引用 2 楼 yupeigu 的回复:] Select * from A where not exists (select 1 from B where a.UserID = b.UserID and a.type = b.type )
而且由于数据太多,这样查询要20多秒,太慢了[/quote] 你的语句能贴个详细的出来吗? 另外,正在情况下20秒查询出来的数据有多少条
吉普赛的歌 版主 2016-12-23
  • 打赏
  • 举报
回复
create index ix_a_userid_type on a(userId, type)
create index ix_b_userid_type on b(userId, type)
加两个索引再说吧。 此外, 你说表大, 具体有多大, 贴出来看看:
exec sp_spaceused 'a'
exec sp_spaceused 'b'
卖水果的net 版主 2016-12-23
  • 打赏
  • 举报
回复
-- 试试这个
SELECT * 
FROM TABLE_A AS A 
LEFT JOIN  TABLE_B  B ON  A.USERID= B.USERID AND A.TYPE=B.TYPE
WHERE  B.USERID IS NULL
快跑稻草人 2016-12-23
  • 打赏
  • 举报
回复
引用 1 楼 RINK_1 的回复:
SELECT * FROM TABLE_A AS A WHERE NOT EXISTS (SELECT 1 FROM TABLE_B WHERE A.USERID=USERID AND A.TYPE=TYPE)
这样特别慢,要20多秒
加载更多回复(4)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧