日期:2014-05-17 浏览次数:20482 次
--TA表 表中数据有160万条记录 create table TA (BH VARCHAR(10) NULL, HW VARCHAR(10) NULL, SL INT ) --TB表 表中数据有10万条记录 CREATE TABLE TB (RQ VARCHAR(10 NULL), BH VARCHAR(10) NULL, HW VARCHAR(10) NULL, SL INT )
SELECT BH,HW,SL FROM TA WHERE not exists (select 1 from TB where TB.BH+TB.HW=TA.BH+TA.BH);
------解决方案--------------------
bh+hw 是用不到索引的
改成下面的会好些,表a b 上面要有索引。
SELECT BH,HW,SL FROM TA WHERE not exists (select 1 from TB where TB.BH=TB.HW and TA.BH=TA.BH);
------解决方案--------------------
select BH,HW,SL from TA where not exists (select 1 from TB where TB.BH+TB.WH=TA.BH+TA.WH)
------解决方案--------------------
数据量这么大,怀疑SQL SERVER根本不会用到索引。 基本照着HASH去了,可以贴一下执行计划吗?
------解决方案--------------------
SELECT TA.BH,TA.HW,TA.SL FROM TA,TB WHERE
CHARINDEX(TA.BH+TA.BH,TB.BH+TB.HW)<=0
------解决方案--------------------
--if object_id('TA') is not null -- drop table TA --Go --create table TA --( --BH VARCHAR(10) NULL, --HW VARCHAR(10) NULL, --SL INT --) --if object_id('TB') is not null -- drop table TB --Go --CREATE TABLE TB --( --RQ VARCHAR(10) NULL, --BH VARCHAR(10) NULL, --HW VARCHAR(10) NULL, --SL INT --) IF OBJECT_ID('tempdb..#tmp_TA') IS NOT NULL DROP TABLE #tmp_TA SELECT *,BH+HW AS IDEN INTO #tmp_TA FROM TA IF OBJECT_ID('tempdb..#tmp_TB') IS NOT NULL DROP TABLE #tmp_TB SELECT *,BH+HW AS IDEN INTO #tmp_TB FROM TB CREATE NONCLUSTERED INDEX #tmp_TA_IDEN ON #tmp_TA(IDEN) CREATE NONCLUSTERED INDEX #tmp_TB_IDEN ON #tmp_TB(IDEN) SELECT BH,HW,SL FROM #tmp_TA AS a WHERE NOT EXISTS(SELECT 1 FROM #tmp_TB AS x WHERE a.IDEN=x.IDEN)