0

I'm running queries from Visual Studio 2015 using SqlCommand class. I need to know the time elapsed to run the query and the size of the result set. I know I can get that info with SQL Server Management Studio, but I don't how to get it from Visual Studio.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
A. Acosta
  • 55
  • 3
  • 9
  • Possible duplicate of [Calculate execution time of a SQL query?](http://stackoverflow.com/questions/595762/calculate-execution-time-of-a-sql-query) – MethodMan Aug 17 '16 at 18:10
  • Did you try handling event info on the connection? http://stackoverflow.com/a/1880507/2707705 – Biscuits Aug 17 '16 at 18:12

3 Answers3

2

You can use a StopWatch to time things:

StopWatch sw = new StopWatch();
sw.Start();
// Run query here
sw.Stop();

// Check sw.ElapsedMilliseconds or some other property you prefer

To get the size of the result set, you can count the iterations of datareader.Read() or other alternatives.

itsme86
  • 19,266
  • 4
  • 41
  • 57
0

First, go into the SQL Server Object Explorer in Visual Studio (View > SQL Server Object Explorer). Then select to query the database of your choosing. Right click > New Query. Before running your query, select 'Include Actual Execution Plan' near the top of the page: Picture of button near top of page

You can then run the query, and at the bottom of the page you will see multiple tabs like Results, Message, and Execution Plan. Select Execution Plan and you'll be able to see the cost of your query in a percentage, but much more information can be found by hovering over the cost or the clustered index scan portions.

I hope this helps!

hmiedema9
  • 948
  • 4
  • 17
0

You can use SMO. Please try code below.

Server myServer = new Server("ServerName");
myServer.ConnectionContext.LoginSecure = true;
myServer.ConnectionContext.Connect();

Database db = myServer.Databases["dbName"];

Table myTable = db.Tables["TableName", "SchemaName"];

Statistic stat = default(Statistic);
stat = new Statistic(myTable, "StatisticName");
StatisticColumn statcol = default(StatisticColumn);
statcol = new StatisticColumn(stat, "FieldName");
stat.StatisticColumns.Add(statcol);
stat.Refresh();
var x = stat.LastUpdated;

You can find other properties as well. You also can Update statistics or even Create statistics and many more.

To find all statistics for a table, use below code, so you can loop through all statistics and get information for each of them:

var allStat = myTable.Statistics;

Take a look at below link for more information:

Statistic Class on MSDN

FLICKER
  • 6,439
  • 4
  • 45
  • 75