We have an Azure SQL database which is on the S1 pricing tier. Our site is extremely heavily cached, so database hits are absolutely minimal. Average DTU usage is only ~1.5%, this is great as our DB costs are a fraction of what they used to be on our old website (£20p/m vs £400 p/m!)
On the site however, we do have small scripts that require insertion of ~100k records or so (user notifications for when someone performs an action such as creates a new tutorial).
When this is triggered, DTU's spike at 100% for around 3-5 minutes.
The script is simply a loop which calls an insert:
using(var db = new DBContext())
{
foreach(var userID in userIDs)
{
db.ExecuteCommand(
"INSERT INTO UserNotifications " +
"(ForUserID, Date, ForObjectTypeID, ForObjectID, TypeID, Count, MetaData1)
VALUES ({0}, {1}, NULL, {2}, {3}, {4}, {5}, {6})",
userID, DateTime.Now.ToUniversalTime(), forObjectID, (byte)type, 1, metaData1.Value
);
}
}
- Is there a faster way to do inserts than this?
- Additionally, what would be the best way to slow down execution of this script so DTU usage doesn't choke everything up?