So I'm trying to get a comparison benchmark between CosmosDB and SQL Server. Presently, I've overloaded a SQL Server database by pumping a table full of test records, all 250 GB of it (yes, id is the primary key and has a clustered index). Now, I'm attempting to delete millions of records, as fast as possible, even hundreds of millions if I can (so far I've only been able to do about 10M an hour using a single thread).
First, I made a connection pool, like so:
List<SqlConnection> connectionPool = new List<SqlConnection>();
for (int i = 0; i < connectionPoolSize; i++)
connectionPool.Add(new SqlConnection(myConnString));
Later, I created a list of tasks and fed each of them a connection. I attempted to get around table locking by keeping the number of records each would delete at 4999. Here we go with the tasks (I debugged through my other code, trust that I'm giving a valid range of row ids & a separate range to each task - I omitted that code):
for (int i = 0; i < connectionPool.Count; i++)
{
Task task = new Task(() => DeleteRows(lowId, highId, dbConnection));
task.Start();
myTaskList.Add(task);
}
Later, I attempted to wait for any thrown exceptions. However, each task is showing as ran to completion, the handler below is not being triggered:
try {
Task.WaitAll( taskList.ToArray());
foreach (Task task in myTaskList)
{
var result = task.Status;
}
}
catch (AggregateException e)
{
Console.WriteLine(e.ToString());
}
And here's the delete method I'm calling in each task. It works when I single thread it, and the only exception I've ever seen is timeouts:
static void DeleteRows(long lowId, long highId, SqlConnection connection)
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
SqlTransaction transaction = connection.BeginTransaction("Transaction");
var cmd = new SqlCommand(
"delete from myTable where id > " + lowId + " and id < " + highId,
connection,
transaction)
{
CommandTimeout = connection.ConnectionTimeout
};
cmd.CommandTimeout = 300;
cmd.ExecuteNonQuery();
transaction.Commit();
}
The transaction addition to the above is new. I have been casting around at this point. Either way, it's not working.