【发布时间】:2011-05-02 15:43:52
【问题描述】:
我正在尝试优化生产中需要很长时间的查询。目标是根据匹配的字段值条件查找重复记录,然后将其删除。当前查询通过 t1.col1 = t2.col1 上的内部连接使用自连接,然后使用 where 子句检查值。
select * from table t1
inner join table t2 on t1.col1 = t2.col1
where t1.col2 = t2.col2 ...
有什么更好的方法来做到这一点?还是基于索引都一样?也许
select * from table t1, table t2
where t1.col1 = t2.col1, t2.col2 = t2.col2 ...
此表有 100m+ 行。
MS SQL、SQL Server 2008 企业版
select distinct t2.id
from table1 t1 with (nolock)
inner join table1 t2 with (nolock) on t1.ckid=t2.ckid
left join table2 t3 on t1.cid = t3.cid and t1.typeid = t3.typeid
where
t2.id > @Max_id and
t2.timestamp > t1.timestamp and
t2.rid = 2 and
isnull(t1.col1,'') = isnull(t2.col1,'') and
isnull(t1.cid,-1) = isnull(t2.cid,-1) and
isnull(t1.rid,-1) = isnull(t2.rid,-1)and
isnull(t1.typeid,-1) = isnull(t2.typeid,-1) and
isnull(t1.cktypeid,-1) = isnull(t2.cktypeid,-1) and
isnull(t1.oid,'') = isnull(t2.oid,'') and
isnull(t1.stypeid,-1) = isnull(t2.stypeid,-1)
and (
(
t3.uniqueoid = 1
)
or
(
t3.uniqueoid is null and
isnull(t1.col1,'') = isnull(t2.col1,'') and
isnull(t1.col2,'') = isnull(t2.col2,'') and
isnull(t1.rdid,-1) = isnull(t2.rdid,-1) and
isnull(t1.stid,-1) = isnull(t2.stid,-1) and
isnull(t1.huaid,-1) = isnull(t2.huaid,-1) and
isnull(t1.lpid,-1) = isnull(t2.lpid,-1) and
isnull(t1.col3,-1) = isnull(t2.col3,-1)
)
)
【问题讨论】:
-
MS SQL?如果有,是哪个版本的 SQL Server?
-
@Bruno 抱歉,我已经更新了问题和标签
-
@Mitch 不,这是一张加入同一张桌子的桌子。怎么不是自加入?
-
@Mitch 不是都叫
table -
当然,调用表“table”可能会产生语法错误。而“create table table ...”听起来很傻,就像老利比的广告一样。但这只是一个例子。
标签: sql sql-server-2008