Right now I have an application that processes records in a DB one record at a time.
var records = from q in db.table
join j in db.OtherTable
where q.processed == 0 //Sample
foreach(var record in records)
{
//Do some stuff....
db.ExecuteCommand("Update table set record.processed = 1 where id = record.ID");
}
At the end of the foreach it updates the record as processed. Obviously because LINQ does not do a great job at updates I just do an executeCommand. Now it is a requirement that this application run quickly. Right now that "Do some stuff" takes less than a second running through thousands of rows, it even inserts a record into a table using SqlBulkCopy.
The problem is that when I put in the code to update that the record is processed, it slows it down significantly. It has to update the database thousands of times. What would be a faster way to update all of the records that I selected (and only the records I selected) without too much of a loss of speed. (Note: it's okay if after the whole process is done (after the foreach) if that it takes a bit of time).
You could generate an IN
statement based on the IDs in your list:
string ids = string.Join(", ", records.Select(r => r.RecordID));
string sql = string.Format("Update table set record.processed = 1 WHERE RecordID IN ({0}) ", ids)
db.ExecuteCommand(sql);
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.