6

I'm using MiniProfiler to profile my sql commands.

One issue I'm dealing with now is repeated INSERT statements generated by linq.

I've converted them into a SqlBulkCopy command, however now it doesn't appear to record it in the sql view in MiniProfiler.

Would there even be an associated command string for a SqlBulkCopy?

Is it possible to get the bulk copy to appear in the list of sql commands?

Can I at least make it counted in the % sql bit?


I'm aware I could use MiniProfiler.Current.Step("Doing Bulk Copy") but that wouldn't count as SQL, and wouldn't show in the listing with any detail.


Current code below:

public static void BulkInsertAll<T>(this DataContext dc, IEnumerable<T> entities)
{
    var conn = (dc.Connection as ProfiledDbConnection).InnerConnection as SqlConnection;
    conn.Open();

    Type t = typeof(T);

    var tableAttribute = (TableAttribute)t.GetCustomAttributes(
        typeof(TableAttribute), false).Single();
    var bulkCopy = new SqlBulkCopy(conn)
    {
        DestinationTableName = tableAttribute.Name
    };

    //....

    bulkCopy.WriteToServer(table);
}
George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • The data is being sent as special TDS packets, but I don't know whether the `BULK INSERT` is being initiated using SQL. It shows up in SQL Profiler as SQL, but that may be fake. – usr Sep 16 '13 at 17:54
  • 2
    I'm working on [MiniProfiler 3.0](https://github.com/MiniProfiler/dotnet) that adds a more generic `CustomTiming` class, allowing you to profile the "bulk copy" and have it show up in the UI the same way "sql" does. Once it's out, I'll answer with an example. – Jarrod Dixon Sep 18 '13 at 21:58
  • @JarrodDixon: Great! Any idea on timescale (beyond 6-8 weeks)? :) – George Duckett Sep 19 '13 at 09:03
  • This weekend, I hope... going to start testing it internally today. – Jarrod Dixon Sep 19 '13 at 15:11
  • @JarrodDixon: Cool, looks like I timed my question pretty well! – George Duckett Sep 19 '13 at 15:18
  • @JarrodDixon: Sorry to pester, but is there any news on this? I see it's getting updated on Git, but doesn't appear as an update on NuGet. – George Duckett Nov 08 '13 at 08:52
  • We're still dog-fooding it internally before releasing it on nuget; feel free to [build it from git](https://github.com/MiniProfiler/dotnet) in the interim. – Jarrod Dixon Nov 10 '13 at 01:01

1 Answers1

5

You should be able to use CustomTimings to profile these. These are included in the new v3 version that is now available on nuget.

You can see some example usages of CustomTiming in the sample project where this is used to record http and redis events.

An example of how you could use it with SqlBulkCopy:

string sql = GetBulkCopySql(); // what should show up for the SqlBulkCopy event?
using (MiniProfiler.Current.CustomTiming("SqlBulkCopy", sql)) 
{
  RunSqlBulkCopy(); // run the actual SqlBulkCopy operation
}
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174