简体   繁体   English

在SQL中删除查询的性能

[英]Performance for delete query in sql

I have a table with 200,000 records. 我有一张有200,000条记录的表。 I want delete some data like below : 我想删除一些数据,如下所示:

DELETE FROM Table Where IdColumn IN ( SelectedID )

SelectedID fill with my desirable data identities that contains 5000 records. SelectedID用包含5000条记录的所需数据身份填充。 there are 2 approach: 有两种方法:

1- I insert 20,000 identity as SelectedID that contains identities of desired 5000 records. 1-我插入20,000个身份作为SelectedID,其中包含所需的5000条记录的身份。

2- I insert only identities of that 5000 records. 2-我只插入那5000条记录的身份。

question is that what is the difference between two approach? 问题是两种方法有什么区别? (performance) (性能)

The performance of a large IN clause is horrible, this is mainly due to query compile time. 大的IN子句的性能令人恐惧,这主要是由于查询编译时间。 So if you have say a List<int> containing the IDs then this: 因此,如果您说一个包含ID的List<int> ,则此操作:

List<int> myIDs = GetIDs(); //20,000
var inList = myIDs.Distinct(); //5,000 distinct IDs
//pass inList to SQL

would be much faster than: 将比:

List<int> inList = GetIDS(); //20,000
//pass inList to SQL

If joining is an alternative, if you can get the list of IDs you want to delete by querying, it's better to do a subquery with that, something like this, using a query in the Where : 如果可以选择加入,则可以通过查询获取要删除的ID列表,最好使用Where的查询对子查询进行类似的子查询,例如:

DELETE FROM Table 
Where IdColumn IN (Select ID
                   From OtherTable
                   Where Name Like '%DeleteMe%')

I'm not sure if your list comes from an external source and can't be determined like this...but if it can, your delete will be extremely faster. 我不确定您的列表是否来自外部资源,并且无法像这样确定...但是,如果可以,则删除速度会非常快。

I'm not really sure, if I understand you completely, but I would go for #2 as it seems to be a waste of resources to generate 20.000 rows of which you only need 5.000 instead of inserting only the needed 5.000. 我不太确定,如果我能完全理解您的意思,但是我会选择#2,因为生成20.000行似乎很浪费资源,其中只需要5.000行而不是只插入所需的5.000行。

Also, 200.000 rows is a small table. 同样,200.000行是一个小表。 Really! 真!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM