27,580
社区成员
发帖
与我相关
我的任务
分享
insert szy_ok
select distinct a.gdsid,a.gdsmc,a.xh,a.gys,fmid
from CITY b,szy_jg1 a
where a.gdsid=b.gdsid AND a.gys=b.gys
and b.city = '900'
and b.fmid in ('0001','0002','0003') and b.flg1 <>'X'
and not exists (select 1 from szy_ok c where c.gdsid = b.gdsid and c.gys = b.gys and c.rtptype = b.fmid)
union
select distinct a.gdsid,a.gdsmc,a.xh,a.gys,fmid
from LABEL b,szy_jg1 a
where a.gdsid=b.gdsid AND a.gys=b.gys
and b.city = '900'
and b.fmid in ('0001','0002','0003') and b.flg1 <>'X'
and not exists (select 1 from szy_ok c where c.gdsid = b.gdsid and c.gys = b.gys and c.rtptype = b.fmid)
--IO消耗
Table: CITY scan count 403720, logical reads: (regular=1697756 apf=0 total=1697756), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_jg1 scan count 1, logical reads: (regular=27183 apf=0 total=27183), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_ok scan count 149415, logical reads: (regular=23128183 apf=0 total=23128183), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: LABEL scan count 1, logical reads: (regular=279 apf=4 total=283), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_jg1 scan count 5117, logical reads: (regular=15998 apf=0 total=15998), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_ok scan count 5117, logical reads: (regular=793135 apf=0 total=793135), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
SQL Server cpu time: 201200 ms. SQL Server elapsed time: 207066 ms.
--表数据
sp_spaceused CITY --4824054
sp_spaceused szy_jg1 --403720
sp_spaceused szy_ok --2379
sp_spaceused LABEL --7232
--执行计划
QUERY PLAN FOR STATEMENT 1 (at line 2).
STEP 1
The type of query is INSERT.
The update mode is direct.
FROM TABLE
szy_jg1
a
Nested iteration.
Table Scan.
Forward scan.
Positioning at start of table.
Using I/O Size 4 Kbytes for data pages.
With LRU Buffer Replacement Strategy for data pages.
FROM TABLE
mkt.dbo.CITY
b
Nested iteration.
Index : BA_GDSRTP_CITY
Forward scan.
Positioning by key.
Keys are:
city ASC
gys ASC
gdsid ASC
Run subquery 1 (at nesting level 1).
Using I/O Size 4 Kbytes for index leaf pages.
With LRU Buffer Replacement Strategy for index leaf pages.
Using I/O Size 4 Kbytes for data pages.
With LRU Buffer Replacement Strategy for data pages.
TO TABLE
Worktable1.
STEP 1
The type of query is INSERT.
The update mode is direct.
FROM TABLE
mkt.dbo.LABEL
b
Nested iteration.
Table Scan.
Forward scan.
Positioning at start of table.
Run subquery 1 (at nesting level 1).
Using I/O Size 4 Kbytes for data pages.
With LRU Buffer Replacement Strategy for data pages.
FROM TABLE
szy_jg1
a
Nested iteration.
Index : szy_jg
Forward scan.
Positioning by key.
Keys are:
gys ASC
gdsid ASC
Using I/O Size 4 Kbytes for index leaf pages.
With LRU Buffer Replacement Strategy for index leaf pages.
Using I/O Size 4 Kbytes for data pages.
With LRU Buffer Replacement Strategy for data pages.
TO TABLE
Worktable1.
STEP 1
The type of query is SELECT.
This step involves sorting.
FROM TABLE
Worktable1.
Using GETSORTED
Table Scan.
Forward scan.
Positioning at start of table.
Using I/O Size 4 Kbytes for data pages.
With MRU Buffer Replacement Strategy for data pages.
STEP 1
NESTING LEVEL 1 SUBQUERIES FOR STATEMENT 1.
QUERY PLAN FOR SUBQUERY 1 (at nesting level 1 and at line 7).
Correlated Subquery.
Subquery under an EXISTS predicate.
STEP 1
The type of query is SELECT.
Evaluate Ungrouped ANY AGGREGATE.
FROM TABLE
szy_ok
c
EXISTS TABLE : nested iteration.
Table Scan.
Forward scan.
Positioning at start of table.
Using I/O Size 4 Kbytes for data pages.
With LRU Buffer Replacement Strategy for data pages.
END OF QUERY PLAN FOR SUBQUERY 1.
STEP 2
NESTING LEVEL 1 SUBQUERIES FOR STATEMENT 1.
QUERY PLAN FOR SUBQUERY 1 (at nesting level 1 and at line 14).
Correlated Subquery.
Subquery under an EXISTS predicate.
STEP 1
The type of query is SELECT.
Evaluate Ungrouped ANY AGGREGATE.
FROM TABLE
szy_ok
c
EXISTS TABLE : nested iteration.
Table Scan.
Forward scan.
Positioning at start of table.
Using I/O Size 4 Kbytes for data pages.
With LRU Buffer Replacement Strategy for data pages.
END OF QUERY PLAN FOR SUBQUERY 1.
Table: CITY scan count 403720, logical reads: (regular=1697756 apf=0 total=1697756), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_jg1 scan count 1, logical reads: (regular=27183 apf=0 total=27183), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_ok scan count 149415, logical reads: (regular=301748 apf=0 total=301748), physical reads: (regular=0 apf=0 total=0), apf IOs used=0Table: Worktable1 scan count 0, logical reads: (regular=188099 apf=0 total=188099), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Total writes for this command: 0
Execution Time 162.
SQL Server cpu time: 16200 ms. SQL Server elapsed time: 33166 ms.
insert szy_ok
select distinct * from
(
select a.gdsid,a.gdsmc,a.xh,a.gys,b.fmid
from CITY b,(select distinct gdsid,gdsmc,xh,gys from szy_jg1) a --city表建立复合索引(gdsid,gys,city,fmid,flg1)
where a.gdsid=b.gdsid AND a.gys=b.gys
and b.city = '900'
and b.fmid in ('0001','0002','0003') and b.flg1 <>'X'
union all --这儿想办法用union all,是哪个表的数据导致了重复?先把那张表DISTINCT后再JOIN(如果是szy_jg1就好了)
select a.gdsid,a.gdsmc,a.xh,a.gys,b.fmid
from LABEL b,(select distinct gdsid,gdsmc,xh,gys from szy_jg1) a
where a.gdsid=b.gdsid AND a.gys=b.gys
and b.city = '900'
and b.fmid in ('0001','0002','0003') and b.flg1 <>'X'
) t
where not exists (select 1 from szy_ok c where c.gdsid = t.gdsid and c.gys = t.gys and c.rtptype = t.fmid) --szy_ok表加复合索引(gdsid,gys,fmid)
--语句1:
set statistics io on
set statistics time on
select distinct a.gdsid,a.gdsmc,a.xh,a.barcode,b.xsj,b.fmid,b.lastmodified,a.pp,a.ppmc,a.syb,a.sybmc,a.tjlb,a.spzmc,a.gys,a.gysmc, a.prop1,a.prop2,a.prop3,a.prop4,a.prop5,a.prop6,a.cd,a.grade,a.size,a.unit,a.pricer,a.prop7,a.prop8,a.prop9,a.prop10,'N' ,0,0,0
from mkt.dbo.BA_GDSRTP_CITY b,szy_jg1 a
where a.gdsid=b.gdsid AND a.gys=b.gys
and b.city = '9000000'
and b.fmid in ('0001','0002','0003') and b.flg1 <>'X'
and not exists (select 1 from szy_ok c where c.gdsid = b.gdsid and c.gys = b.gys and c.rtptype = b.fmid)
Table: BA_GDSRTP_CITY scan count 403720, logical reads: (regular=1697756 apf=0 total=1697756), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_jg1 scan count 1, logical reads: (regular=27183 apf=0 total=27183), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_ok scan count 149415, logical reads: (regular=23128183 apf=0 total=23128183), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: Worktable1 scan count 0, logical reads: (regular=188099 apf=0 total=188099), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Total writes for this command: 0
Execution Time 2012.
SQL Server cpu time: 201200 ms. SQL Server elapsed time: 210696 ms.
--语句2:
set statistics io on
set statistics time on
select distinct a.gdsid,a.gdsmc,a.xh,a.barcode,b.xsj,b.fmid,b.lastmodified,a.pp,a.ppmc,a.syb,a.sybmc,a.tjlb,a.spzmc,a.gys,a.gysmc, a.prop1,a.prop2,a.prop3,a.prop4,a.prop5,a.prop6,a.cd,a.grade,a.size,a.unit,a.pricer,a.prop7,a.prop8,a.prop9,a.prop10,'N' ,0,0,0
from mkt.dbo.BA_GDSRTP_CITY b,szy_jg1 a
where a.gdsid=b.gdsid AND a.gys=b.gys
and b.city = '9000000'
and b.fmid in ('0001','0002','0003') and b.flg1 <>'X'
--and not exists (select 1 from szy_ok c where c.gdsid = b.gdsid and c.gys = b.gys and c.rtptype = b.fmid)
Table: BA_GDSRTP_CITY scan count 403720, logical reads: (regular=1697756 apf=0 total=1697756), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: szy_jg1 scan count 1, logical reads: (regular=27183 apf=0 total=27183), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Table: Worktable1 scan count 0, logical reads: (regular=188603 apf=0 total=188603), physical reads: (regular=0 apf=0 total=0), apf IOs used=0
Total writes for this command: 0
Execution Time 130.
SQL Server cpu time: 13000 ms. SQL Server elapsed time: 32836 ms.