sql - 检查重复项时的最佳自连接技术

标签 sql sql-server-2008

我正在尝试优化一个需要很长时间的生产查询。目标是根据匹配的字段值条件查找重复记录,然后将其删除。当前查询在 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) 
            )
    )

最佳答案

为什么自加入:这是一个综合问题。

希望你在 col1, col2, ... 上有一个索引

--DELETE table
--WHERE KeyCol NOT IN (
select
    MIN(KeyCol) AS RowToKeep,
    col1, col2, 
from
    table
GROUP BY
    col12, col2
HAVING
   COUNT(*) > 1
--)

但是,这需要一些时间。有一个 look at bulk delete techniques

关于sql - 检查重复项时的最佳自连接技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5859191/

相关文章:

php - PHP 中 $mysqli->real_escape_string 的正确用法

sql - 对两列的任意组合的唯一约束

sql - 这个SQL怎么会错呢?我没看到什么?

sql-server - SQL Server 存储过程中的 CASE 语句出现问题

sql - 从 SQL Server 2000 升级到 SQL Server 2008 R2 并运行旧版 DTS 包

SQL查询最大日期和其他表中的一些字段

SQL表继承导致基表中的重复记录,即使设置了主键约束

sql - 将文本解析为多列

sql - 无法重命名临时表的列

sql-server-2008 - Flask-SQLAlchemy .count() 和 .all() 的不同记录数