views:

70

answers:

2

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)

A: 

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!

Frank Kalis
yes I know that 200.000 rows is a small table. it is a sample. you can multiply to 100.
masoud ramezani
in my application i can search for that 5000 record but it is time wasting therefore i don't search my data and pass all of data identities to IN clause.
masoud ramezani
+1  A: 

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.

Nick Craver
thanks Nick. I create desired identities in my application by returning a comma separated string.
masoud ramezani
Then pass this string to the DELETE procedure, split it inside the sProc with a table-valued UDF and join that with the table you want to delete from.
Frank Kalis
@masoud ramezani - Are you not hitting a string length limit with this?
Nick Craver