sql - 创建 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;
谢谢!
瓦尔德
解决方案
对于您的查询版本:
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;
当然,如果您有在表上设置值的标识列或触发器,则逻辑会更加复杂。
推荐阅读
- jasmine - Protractor - 套件日志在 Jasmine 记者中被分成多个套件
- oauth-2.0 - OAuth 2 Java 休息 API
- scala - Scala映射显式类型
- javascript - AngularJS ng-repeat 没有从 crontoller 获取数据
- hyperledger-fabric - 在不同的机器中连接 2 个现有的组织 - Hyperledger Fabric
- android - Livedata 观察者的多个实例
- asp.net - 使用 vb.net 在浏览器中弹出窗口
- regex - regexp_replace() - 匹配但不在行尾替换
- reactjs - 向父 DOM 元素添加/删除类 React js
- tfs - 如何将过去迭代的排序顺序更改为 TFS 积压/董事会中的降序?