Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing SQL STATISTICS TIME and IO into a table

Is there a way to capture STATISTICS IO and TIME within T-SQL, for logging into a table?

like image 986
TonyP Avatar asked Jun 05 '10 11:06

TonyP


People also ask

How do I show statistics in SQL?

Connect to a SQL Server instance in SSMS and expand the particular database. Expand the object ( for example, HumanResources. Employee), and we can view all available statistics under the STATISTICS tab. We can get details about any particular statistics as well.

How do you find the time taken to execute a SQL query?

Using Client StatisticsGo to Menu >> Query >> Select Include client Statistics. Execute your query. In the results panel, you can see a new tab Client Statistics. Go to the Client Statistics tab to see the execution time.

Can you do statistics with SQL?

SQL Server statistics are one of the key inputs for the query optimizer during generating a query plan. Statistics are used by the optimizer to estimate how many rows will return from a query so it calculates the cost of a query plan using this estimation.

How do I find SQL Server statistics io?

In SQL Server, you can use the SET STATISTICS IO statement to generate detailed information about the amount of disk activity generated by a T-SQL statement. In graphical tools like SSMS and Azure Data Studio, you can view this information in the Messages tab.


2 Answers

Sort of.

The same statistics as those given by SET STATISTICS TIME are captured by the Query Statistics DMV: sys.dm_exec_query_stats.

DMVs can be queried from T-SQL, just like normal views.

However, the SET STATISTICS IO are only captured as aggregate values (last_logical_reads, last_physical_read) per execution, without the differentiation per-rowset given by SET STATISTICS IO.

Overall though, the DMVs can serve the same purpose as SET STATISTICS IO.

like image 114
Remus Rusanu Avatar answered Sep 28 '22 03:09

Remus Rusanu


No, not using SET STATISTICS IO ON.

But then you don't need to; run SQL Profiler and start a trace to output to a file. Include Reads and Duration.

like image 42
Mitch Wheat Avatar answered Sep 28 '22 03:09

Mitch Wheat