简体   繁体   中英

Performance for delete query in sql

I have a table with 200,000 records. 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. there are 2 approach:

1- I insert 20,000 identity as SelectedID that contains identities of desired 5000 records.

2- I insert only identities of that 5000 records.

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. So if you have say a List<int> containing the IDs then this:

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 :

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.

Also, 200.000 rows is a small table. Really!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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