I'm trying to improve this query perfromance. I have to update multiple rows in once and this take too long for me. For example 4k rows for update takes 15 seconds.
My code:
using (var db = new DbConnection())
{
int counter = 0;
foreach (var item in itemsToUpdate)
{
if (!db.IsTransactionOpen)
{
db.OpenTransaction();
}
DbQuery q = db.CreateQuery();
q += "UPDATE Product SET Name = @Name WHERE Id = @Id";
q.AddParameter("@Name", item.Name);
q.AddParameter("@Id", item.Id);
db.ExecuteQuery();
counter++;
if (counter > 100)
{
db.CommintTransaction();
}
}
if (!db.IsTransactionOpen)
{
db.CommintTransaction();
}
}
I'm not satisfied with that update time. There is any way to speed up this? I used simplified update query in example, I tested without transaction and it takes much more time.
Easiest way is to send the data in JSON and join it in a single SQL statement, eg with a nvarchar(max) parameter set to
run