A、B两表,找出ID字段中,存在A表,但是不存在B表的数据。A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引。
使用 not in ,容易理解,效率低 ~执行时间为:1.395秒~
1 select distinct A.ID from A where A.ID not in (select ID from B)
使用 left join...on... , "B.ID
isnull" 表示左连接之后在B.ID 字段为 null的记录 ~执行时间:0.739秒~
1 select A.ID from A left join B on A.ID=B.ID where B.ID is null
逻辑相对复杂,但是速度最快 ~执行时间: 0.570秒~
1 select * from B
2 where (select count(1) as num from A where A.ID = B.ID) = 0
A、B两表,找出ID字段中,存在A表,但是不存在B表的数据。A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引。方法一 使用 not in ,容易理解,效率低 ~执行时间为:1.395秒~1 select distinct A.ID from A where A.ID not in (select ID from B)方法二 使用
[size=medium]
转自:[url]http://www.oschina.net/question/89964_65912[/url]
left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.
换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例如: A.aID = B.bID) (A....
(转)A、B两表,找出ID字段中,
存在
A表,但是不
存在
B表的
数据
。A表总共13w
数据
,去重后大约3W条
数据
,B表有2W条
数据
,且B表的ID字段有索引。
使用 not in ,容易理解,效率低 ~执行时间为:1.395秒~
1 select distinct A.ID from A where A.ID not in (select ID from B)
Linq
中包含
查询
//
Linq
代码:
T_WxMaterials.Where(n=>T_VideoMsgs.Select(m=>m.MediaID).Contains(n.MediaID)) //生成的sql语句:
SELECT [t0].[ID], [t0].[Type], [t0].[Title], [t0].[Cnt], [t0].[MediaID], [t0].[LocalPath], [t
1)instr()函数的格式 (俗称:字符查找函数)
格式一:instr( string1, string2 ) / instr(源字符串, 目标字符串)
格式二:instr( string1, string2 [, start_position [, nth_appearance ] ] ) / instr(源字符串, 目标字符串, 起始位置, 匹配序号)
解析:string2 的值...
A、B两表,找出ID字段中,
存在
A表,但是不
存在
B表的
数据
。A表总共13w
数据
,去重后大约3W条
数据
,B表有2W条
数据
,且B表的ID字段有索引。
使用 not in ,容易理解,效率低 执行时间为:1.395秒
1 select distinct A.ID from A where A.ID not in (select ID from B)
使用 left join…on…...
1.首先了解 on 、where 的执行顺序以及效率?
from a join b 与 from a, b 产生的临时表结果集 。
都是执行笛卡尔积即(select * from a cross join b )两表的行乘积数。
on :与取得结果集同步进行
数据
刷选及过滤。
where : 获得结果集之后,才进行
数据
刷选及过滤。
执行顺序:on在上游,where在中游,having在下游
A、B两张表,找出ID字段中,
存在
A表,但是不
存在
B表的
数据
,A表总共13W
数据
,去重后大约3万条
数据
,B表有2W条
数据
,且B表的ID有索引。
使用not in,容易理解,效率低。select distinct a.id from a where a.id not in(select id from b)方法二
使用left join … on ….,’b.id is null’,表示