我在两个不同的数据库中有两个表
Database1 - Table1
Database2 - Table2
Table1 Columns: NimID,IDDate,Station
Table2 Columns: XilID,XilDate
Table1 Table2
NimID IDDate Station XilID XilDate
234 2011-04-21 HYD 234 2011-04-21
237 2011-04-21 CHN 208 2011-04-21
208 2011-04-21 HYD 209 2011-04-15
209 2011-04-15 DEL 218 2011-05-28
212 2011-03-11
我想知道Table1中有多少ID不在Table2中,其中IDDate = XilDate ='2011-04-21'group by Table1.Station。 我在下面进行了查询
select x.Station as Station,
count(distinct x.NimID) as Difference
from (
select a.NimID,
a.IDDate,
a.Station
from database1.dbo.table1 a
where left(cast(a.Date as date),11)='2011-04-21'
) as X, (
select b.XilID,
b.XILDate
from database2.dbo.Table2 b
where b.XilDate='2011-04-21'
) as Y
where x.NimID not in (y.XilID)
group by x.Station
但是这个查询会永远运行.. 请记住,这些表来自位于同一服务器上的不同数据库,Table1包含10,000,000条记录,Table2包含大约13,000,000条记录 如果错误请更正我的查询或建议我更快的方式
由于
答案 0 :(得分:1)
尽量避免从datetime转换为varchar。
WHERE a.Date >= '2011-04-21'
AND a.Date < (CAST('2011-04-21' AS datetime) + 1)
答案 1 :(得分:1)
DECLARE @date datetime;
SET @date = '20110421';
SELECT
Station,
Diff = COUNT(*)
FROM (
SELECT
a.NimID,
a.IDDate,
a.Station
FROM database1.dbo.table1 a
LEFT JOIN database2.dbo.table2 b ON a.NimID = b.XilID AND b.XilDate = @date
WHERE b.XilID IS NULL
AND a.IDDate >= @date
AND a.IDDate < DATEADD(day, 1, @date)
) s
GROUP BY Station
<强>更新强>
实际上,上述解决方案可以在没有子选择的情况下重写。 subselect是尝试一些想法的结果,我最终放弃了,但是subselect仍然有一些未知的原因。这是一个没有子选择的相同解决方案:
DECLARE @date datetime;
SET @date = '20110421';
SELECT
a.Station,
Diff = COUNT(*)
FROM database1.dbo.table1 a
LEFT JOIN database2.dbo.table2 b ON a.NimID = b.XilID AND b.XilDate = @date
WHERE b.XilID IS NULL
AND a.IDDate >= @date
AND a.IDDate < DATEADD(day, 1, @date)
GROUP BY a.Station
答案 2 :(得分:0)
尝试以下操作 - 请注意,您似乎正在尝试连接两个表来执行“not in”,这会导致生成速度非常慢且结果集非常错误。
此外,如果IDDate是DATETIME列,那么您最好执行范围检查,例如(a.IDDate >= '2011-04-21' AND a.IDDate < '2011-04-22')
。考虑一下 - 如果它是yyyy-MM-dd格式的文本列,那么范围检查也会起作用 - 如果它是具有混合格式日期的文本列,那么请忘记我提到它。
select x.Station as Station,
count(distinct x.NimID) as Difference
from (
select a.NimID,
a.IDDate,
a.Station
from database1.dbo.table1 a
where left(cast(a.IDDate as date),11)='2011-04-21'
) as X
where x.NimID not in (
select b.XilID
from database2.dbo.Table2 b
where b.XilDate='2011-04-21'
)
group by x.Station