首页 > 解决方案 > 创建 SQL 索引以提高速度

问题描述

我正在从具有一transaction_id列和一last_modified_date列的表中删除重复项(请参见查询下方)。这个想法是我应该有一个记录transaction_id,所以我需要删除重复项,保留给定的最后修改记录transaction_id

查询有效,但速度很慢。

问题是:我应该创建什么索引来加快查询执行时间?

With CTE_Duplicates as
(
   select 
       transaction_id, 
       row_number() over (partition by transaction_id order by last_modified_date desc) rownumber 
   from 
       TRANSACTIONS 
)  
delete from CTE_Duplicates 
where rownumber != 1;  

谢谢!

瓦尔德

标签: sqlsql-serverperformanceindexing

解决方案


对于您的查询版本:

With CTE_Duplicates as (
    select t.*,
           row_number() over (partition by transaction_id order by last_modified_date desc) as rownumber
    from TRANSACTIONS
   )
delete from CTE_Duplicates
    where rownumber > 1;

你想要一个关于(transaction_id, last_modified_date desc). 但是,使用相同的索引,将查询表述为:

delete t from transactions t
    where t.last_modified_date = (select max(t2.last_modified_date)
                                  from transactions t2
                                  where t2.transaction_id = t.transaction_id
                                 );

话虽如此,如果要删除许多行(“许多”甚至可能是百分之几),您的查询将非常昂贵。在这种情况下,临时表解决方案可能会更好:

select t.*
into temp_transactions
from transactions t
where t.last_modified_date = (select max(t2.last_modified_date)
                              from transactions t2
                              where t2.transaction_id = t.transaction_id
                             );

truncation table temp_transactions;  -- backup first!

insert into transactions
    select *
    from temp_transactions;

当然,如果您有在表上设置值的标识列或触发器,则逻辑会更加复杂。


推荐阅读